4.8 KiB
Executable File
What is a Dockerfile?
A Dockerfile is a simple text file containing instructions to create a Docker image. Docker images provide a consistent and reproducible environment for running applications in containers. By defining dependencies, configurations, and the operating system, Dockerfiles automate the image creation process, ensuring version-controlled and portable environments.
Key Concepts:
-
Base Image: The foundational layer of your image, typically an official operating system like Ubuntu, CentOS, or Alpine Linux.
-
Instructions: Commands such as
RUN,COPY, andCMDdefine what’s installed, how the image behaves, and which files to include.Common instructions include:
RUN: Executes commands (e.g., installing software) inside the container.COPY: Copies files from your local machine to the image.CMD: Specifies the default command to run when the container starts.
Step-by-Step Guide to Creating a Dockerfile
1. Create a File Named Dockerfile
Start by creating a file called Dockerfile in your project directory. If you name it something else, you'll need to specify the file name during the build process.
Example Dockerfile:
# Use Ubuntu 22.04 as the base image
FROM ubuntu:22.04
# Add metadata such as version information
LABEL version="0.0.1"
# Update package lists and install essential tools
RUN apt update && apt install -y bash vim curl
# Install Nginx web server
RUN apt install -y nginx
Breakdown of Instructions:
FROM ubuntu:22.04: Defines Ubuntu 22.04 as the base image.LABEL version="0.0.1": Adds metadata, such as the version label.RUN: Runs commands inside the container. In this case, it updates package lists and installsbash,vim,curl, andnginx.
2. Example Using Alpine Linux
Alpine Linux is a lightweight option that results in smaller images. Here's an example of a Dockerfile using Alpine:
# Use Alpine as the base image
FROM alpine
# Add version metadata
LABEL version="0.0.1"
# Update package lists and install essential tools
RUN apk update && apk add bash vim curl
This example is perfect for when you need a compact, minimalistic image.
3. Complex Dockerfile with a Script
In this example, you'll learn how to copy a script into the container, set a working directory, and make the script executable.
# Start with Alpine as the base image
FROM alpine
# Add metadata
LABEL version="0.0.1"
# Update package lists and install essential tools
RUN apk update && apk add bash vim curl iputils-ping
# Copy the script into the image
COPY <local-file-path> <container-destination-path>
# Set the working directory for subsequent commands
WORKDIR <container-destination-path>
# Add environment variables
ENV API_KEY="123445"
# Set user and expose ports
USER deploy
EXPOSE 3210
# Give execution permissions to the script
RUN chmod +x app.sh
# Define the default command to run
CMD ["./app.sh"]
# Alternatively, you can use ENTRYPOINT
ENTRYPOINT ["bash", "./app.sh"]
Key Concepts in This Example:
COPY: Copies a file from your local machine to the container.WORKDIR: Sets the working directory inside the container for subsequent commands.RUN chmod +x app.sh: Grants execution permissions to theapp.shscript.CMDvs.ENTRYPOINT:CMDprovides default behavior, whileENTRYPOINTis used when you want to ensure the container always runs a specific executable.
4. Build Your Docker Image
Once your Dockerfile is ready, build the Docker image using the docker build command.
docker build -t <app-name> <path-to-dockerfile>
Usage Examples:
-
To build with a
Dockerfilein the current directory:docker build -t app-test .Here, the
.indicates the current directory as the build context. -
If your file is named something other than
Dockerfile(e.g.,CustomDockerfile):docker build -t app-test -f <CustomDockerfile> . -
If you want build file without use cache
docker build -t app-test:v1 -f <Custom-Dir> . --no-cache
Explanation:
docker build: Builds a Docker image.-t <app-name>: Tags the image with a name (e.g.,app-test).<path-to-dockerfile>: Specifies the location of theDockerfile. Use.for the current directory or provide an absolute path.
Summary
A Dockerfile is a powerful tool for automating the creation of Docker images. Here’s a quick recap:
- Create a Dockerfile: Use instructions like
FROM,RUN,COPY, andCMDto define the image. - Build the Image: Run the
docker buildcommand to turn your Dockerfile into a Docker image. - Run the Container: Once the image is built, use
docker runto create and run a container based on it.