Skip to content

๐Ÿณ 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

Dockerfile
โ†“ docker build
Docker Image
โ†“ docker run
Docker Container

๐Ÿงฑ 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)
Stage 1 โ†’ Build app Stage 2 โ†’ Copy only output
โœ” 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)


# 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

FROM node:18
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
CMD ["node", "index.js"]

โœ… 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

FROM node:18
WORKDIR /app
COPY . .
RUN npm install && npm run build
CMD ["npm", "start"]

โœ… 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

FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY html/ /usr/share/nginx/html

9๏ธโƒฃ Database Containers (Best Practice)

โš  Databases usually do NOT need custom Dockerfiles Use official images.


PostgreSQL

FROM postgres:15

Run using environment variables:

  • POSTGRES_DB
  • POSTGRES_USER
  • POSTGRES_PASSWORD

MySQL

FROM mysql:8

MongoDB

FROM mongo:7

๐Ÿ”Ÿ 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

node_modules
.git
.env
dist
build

๐Ÿ” CMD vs ENTRYPOINT

CMD (Overridable)

CMD ["node", "app.js"]

ENTRYPOINT (Fixed)

ENTRYPOINT ["node"]
CMD ["app.js"]

๐Ÿง  Build-Time vs Run-Time

Phase Happens When
RUN Image build
CMD Container start

๐Ÿงช Common Dockerfile Commands

docker build -t my-app .
docker build --no-cache -t my-app .
docker run my-app

๐Ÿ 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

  1. Docker Compose (dev & prod)
  2. Docker networking deeply
  3. Docker security
  4. Kubernetes