A Dockerfile is a simple text file that contains the instructions Docker uses to build an image. It automates the process of setting up your application environment.
1. Common Dockerfile Instructions
| Instruction | Description |
|---|---|
FROM | Sets the base image (usually an OS or runtime). |
WORKDIR | Sets the working directory inside the container. |
COPY | Copies files from your host to the container. |
RUN | Executes commands during the build process (e.g., apt install). |
CMD | The command the container runs when it starts. |
EXPOSE | Documents which port the application listens on. |
ENV | Sets environment variables. |
2. Example: Building a Python App
Let's create a Dockerfile for a simple web application.
Dockerfile:
# 1. Start from a lightweight Python image
FROM python:3.9-slim
# 2. Set the working directory
WORKDIR /app
# 3. Copy dependencies and install
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# 4. Copy the application code
COPY . .
# 5. Expose the port the app runs on
EXPOSE 5000
# 6. Run the app
CMD ["python", "app.py"]Build the Image
Action:
docker build -t my-python-app:v1 .Result:
[+] Building 4.2s (10/10) FINISHED
=> [internal] load build definition from Dockerfile
=> [1/5] FROM docker.io/library/python:3.9-slim
=> [2/5] WORKDIR /app
=> [3/5] COPY requirements.txt .
=> [4/5] RUN pip install --no-cache-dir -r requirements.txt
=> [5/5] COPY . .
=> exporting to image
=> writing image sha256:a1b2c3d4e5f6...
=> naming to docker.io/library/my-python-app:v13. CMD vs ENTRYPOINT
This is a common source of confusion:
CMD: Sets a default command. If the user provides an argument todocker run,CMDis ignored.ENTRYPOINT: Sets the main command. Arguments fromdocker runare appended to it. It cannot be easily overridden.
4. Best Practices for Dockerfiles
- Use
.dockerignore: Exclude files likenode_modulesor.gitto keep images small. - Order Matters: Put instructions that change often (like
COPY . .) at the bottom to leverage layer caching. - Minimize Layers: Combine multiple
RUNcommands using&&if possible. - Use Specific Tags: Avoid
latest. Usepython:3.9-slimfor predictable builds.
Summary
- A Dockerfile is a recipe for an image.
- Each instruction creates a new layer.
- Caching makes builds fast—only rebuilt layers change.