Kritim Yantra
Apr 14, 2025
A Dockerfile is a script that contains instructions to build a Docker image. It automates the process of setting up environments, installing dependencies, and configuring applications.
In this guide, you’ll learn:
✔ Dockerfile syntax & best practices
✔ How to optimize Docker images
✔ Real-world Dockerfile examples
A Dockerfile is a text file that defines:
ubuntu
, python
). ✅ Reproducibility – Same image every time.
✅ Automation – No manual setup needed.
✅ Version Control – Track changes in Git.
A simple Dockerfile
looks like this:
# Step 1: Choose a 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 -r requirements.txt
# Step 4: Copy the rest of the app
COPY . .
# Step 5: Define the startup command
CMD ["python", "app.py"]
Instruction | Description | Example |
---|---|---|
FROM |
Base image (ubuntu , nginx , python ) |
FROM ubuntu:20.04 |
WORKDIR |
Sets working directory | WORKDIR /app |
COPY |
Copies files from host to container | COPY . /app |
RUN |
Executes commands during build | RUN apt-get update |
CMD |
Default command when container starts | CMD ["npm", "start"] |
EXPOSE |
Declares which ports to expose | EXPOSE 80 |
ENV |
Sets environment variables | ENV DB_HOST=mysql |
ARG |
Build-time variables | ARG VERSION=1.0 |
.dockerignore
Prevents unnecessary files (like node_modules
, .git
) from being copied.
# .dockerignore
node_modules/
.git/
.env
Docker caches layers, so structure commands from least to most frequently changed:
# Bad (Cache breaks if any file changes)
COPY . .
RUN pip install -r requirements.txt
# Good (Only reinstalls if requirements.txt changes)
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
ubuntu
(heavy) → Use alpine
, slim
variants. python:3.9-slim
instead of python:3.9
.RUN
CommandsReduces layers and image size.
# Bad (Creates multiple layers)
RUN apt-get update
RUN apt-get install -y curl
# Good (Single layer)
RUN apt-get update && apt-get install -y curl
Reduces final image size by discarding build dependencies.
# Stage 1: Build the app
FROM node:16 as builder
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
# Stage 2: Run the app (only keep production files)
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
Before: 1.2GB
After: 50MB (only includes nginx
and built files)
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"]
FROM node:16 as build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
FROM mysql:8.0
ENV MYSQL_ROOT_PASSWORD=my-secret-pw
COPY init.sql /docker-entrypoint-initdb.d/
EXPOSE 3306
.dockerignore
, multi-stage builds, and small base images. Got questions? Ask in the comments! 🚀
Happy Dockerizing! 🐳
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google