๐ณ Dockerfile โ Full Tutorial (Beginner to Advanced)¶
This tutorial explains Dockerfile from scratch, with real-world examples for:
- Golang
- Python
- JavaScript (Node.js, Express.js, React.js)
- Nginx
- Databases (PostgreSQL, MySQL, MongoDB)
- With & without multi-stage builds
No assumptions. Everything explained clearly.
๐ What is a Dockerfile?¶
A Dockerfile is a text file that contains instructions to build a Docker image.
Think of it as:
๐ Recipe โ Image โ Container
๐ง Dockerfile Build Flow¶
๐งฑ Core Dockerfile Instructions (Must Know)¶
| Instruction | Purpose |
|---|---|
| FROM | Base image |
| WORKDIR | Working directory |
| COPY | Copy files |
| RUN | Execute command at build time |
| CMD | Run command at container start |
| ENTRYPOINT | Fixed startup command |
| EXPOSE | Document port |
| ENV | Environment variable |
1๏ธโฃ Simple Dockerfile (Without Multi-Stage Build)¶
Example: Node.js App¶
Project Structure¶
node-app/
โโโ Dockerfile
โโโ package.json
โโโ server.js
````
#### Dockerfile
```Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
````
### Explanation
* `FROM` โ Node + Linux
* `WORKDIR` โ Container folder
* `RUN` โ Runs during build
* `CMD` โ Runs when container starts
---
## 2๏ธโฃ Why Multi-Stage Builds?
Problem without multi-stage:
* Large image
* Build tools included
* Slower deployment
Solution:
> **Build in one stage, run in another**
---
## 3๏ธโฃ Multi-Stage Build (Concept)
โ Smaller images
โ More secure
โ Production ready
---
## 4๏ธโฃ Golang Dockerfile
### โ Without Multi-Stage
```Dockerfile
FROM golang:1.22
WORKDIR /app
COPY . .
RUN go build -o app
CMD ["./app"]
โ Image contains Go compiler (not ideal)
โ With Multi-Stage (Recommended)¶
# Build stage
FROM golang:1.22 AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o app
# Runtime stage
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/app .
CMD ["./app"]
โ Final image is very small โ No Go compiler inside
5๏ธโฃ Python Dockerfile¶
โ Without Multi-Stage¶
FROM python:3.11
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
โ With Multi-Stage¶
FROM python:3.11 AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH
COPY . .
CMD ["python", "app.py"]
6๏ธโฃ Node.js & Express.js Dockerfile¶
โ Without Multi-Stage¶
โ With Multi-Stage (Production)¶
FROM node:18 AS builder
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
FROM node:18-slim
WORKDIR /app
COPY --from=builder /app .
CMD ["node", "index.js"]
7๏ธโฃ React.js Dockerfile¶
โ Without Multi-Stage (Not Recommended)¶
โ With Multi-Stage (Best Practice)¶
# Build stage
FROM node:18 AS builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
# Serve stage
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
โ React built once โ Served via Nginx โ Very small image
8๏ธโฃ Nginx Dockerfile¶
Custom Nginx Config¶
9๏ธโฃ Database Containers (Best Practice)¶
โ Databases usually do NOT need custom Dockerfiles Use official images.
PostgreSQL¶
Run using environment variables:
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
MySQL¶
MongoDB¶
๐ Dockerfile Best Practices¶
โ Use multi-stage builds
โ Use slim/alpine images
โ One process per container
โ Avoid root user
โ Use .dockerignore
โ Cache dependencies properly
๐ Example .dockerignore¶
๐ CMD vs ENTRYPOINT¶
CMD (Overridable)¶
ENTRYPOINT (Fixed)¶
๐ง Build-Time vs Run-Time¶
| Phase | Happens When |
|---|---|
| RUN | Image build |
| CMD | Container start |
๐งช Common Dockerfile Commands¶
๐ Final Summary¶
- Dockerfile defines how image is built
- Multi-stage builds = production standard
- Use official images for DBs
- Build heavy โ run lightweight
๐ What to Learn Next¶
- Docker Compose (dev & prod)
- Docker networking deeply
- Docker security
- Kubernetes