🐳 Mastering the Dockerfile: The Complete Beginner’s Guide
Docker is one of the most essential tools in DevOps and cloud-native development. At the heart of Docker is the Dockerfile — a simple text file with instructions to build a Docker image.
This guide will walk you through everything you need to know about Dockerfiles, from basics to best practices.
📦 What is a Dockerfile?
A Dockerfile is like a recipe 🧑🍳.
- Each line is an instruction.
- Docker processes it top to bottom.
- The result is a Docker image, which you can run as a container.
Flow:
Dockerfile ➝ Docker Image ➝ Container
🛠️ Basic Structure of a Dockerfile
Here’s a template:
FROM <base-image> WORKDIR <directory> COPY <src> <dest> RUN <command> CMD ["executable", "param1", "param2"]
🔑 Key Instructions Explained
1. FROM
Sets the base image to start with.
FROM python:3.9-slim
👉 Every Dockerfile must start with a base image (unless you’re building completely from scratch).
2. WORKDIR
Defines the working directory inside the container.
WORKDIR /app
3. COPY
Copies files from your local machine into the container.
COPY requirements.txt . COPY . .
4. RUN
Executes commands at build time.
RUN pip install -r requirements.txt
5. CMD
Specifies the default command to run when the container starts.
CMD ["python", "app.py"]
6. ENTRYPOINT
(advanced)
Defines the main process of the container. Unlike CMD
, it cannot be overridden easily.
ENTRYPOINT ["python"] CMD ["app.py"]
👉 This runs as python app.py
.
7. EXPOSE
Documents which ports the container will use.
EXPOSE 5000
8. ENV
Sets environment variables.
ENV APP_ENV=production
9. ARG
Sets build-time variables.
ARG VERSION=1.0
📄 Full Example: Python Flask App
# Step 1: Start from base image FROM python:3.9-slim # Step 2: Set working directory WORKDIR /app # Step 3: Copy requirements and install dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Step 4: Copy the rest of the code COPY . . # Step 5: Expose port EXPOSE 5000 # Step 6: Default command CMD ["python", "app.py"]
⚡ Build and Run
# Build the image docker build -t myapp:1.0 . # Run the container docker run -p 5000:5000 myapp:1.0
Now open 👉 http://localhost:5000
📸 Visual Overview
Dockerfile ➝ Image ➝ Container
✅ Best Practices
- Use lightweight base images (e.g.,
alpine
,slim
). - Always use a .dockerignore file to avoid copying unnecessary files.
- Combine commands to reduce layers:
RUN apt-get update && apt-get install -y curl
- Pin versions of dependencies for reproducibility.
- Use multi-stage builds to keep images small:
FROM golang:1.18 as builder WORKDIR /src COPY . . RUN go build -o app FROM alpine:3.16 COPY --from=builder /src/app /app CMD ["/app"]
🚀 Wrap-up
A Dockerfile is the blueprint of your containerized application.
With a few simple instructions, you can package your code and run it anywhere.
In upcoming posts, we’ll explore multi-stage builds, caching, and security best practices for Dockerfiles.
💡 Question for you:
👉 What’s the most challenging part you faced when writing your first Dockerfile?
Drop your thoughts in the comments 👇
Top comments (0)