Quick Revision Notes for Docker

Sep 20, 2025

Quick Revision Notes for Docker

Quick Revision Notes for Docker

A compact reference for writing clean, production-ready Docker setups.


What Docker Is

  • Containerization platform packaging code + dependencies
  • Ensures consistent environments across dev and production
  • Eliminates “works on my machine” issues

Core Concepts

  • Image: Immutable blueprint built from a Dockerfile
  • Container: Running instance of an Docker image
  • Dockerfile: Instructions to build an image
  • Registry: Stores images (Docker Hub, AWS ECR)

Why Use Docker

  • Environment consistency
  • Easy deployment and scaling
  • Industry standard for backend systems

Essential Dockerfile Instructions

FROM    # base image
WORKDIR # working directory
COPY    # copy files
RUN     # build-time commands
ENV     # environment variables
EXPOSE  # document port
CMD     # runtime command

Recommended Dockerfile Order

FROM node:20-alpine
WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .
CMD ["npm", "start"]

This order enables layer caching and faster rebuilds.


Multi-Stage Builds

Used to reduce image size and improve security.

FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app ./
CMD ["npm", "start"]

Environment Variables

Rule: Never store secrets in images.

Correct usage:

docker run --env-file .env image-name

Best practices:

  • Use .env.example
  • Add .env to .dockerignore
  • Inject secrets at runtime only

Build vs Run Phases

| Phase | Secrets | | ------------ | ------- | | docker build | No | | docker run | Yes |

Build = code Run = configuration


Next.js with Docker

Next.js executes server code during build. Initialize secrets lazily.

function getClient() {
  return new OpenAI({ apiKey: process.env.KEY! });
}

Avoid accessing secrets at module top level.


Prisma Docker Fix

Copy Prisma schema before installing dependencies:

COPY package*.json ./
COPY prisma ./prisma
RUN npm install

Ports and Volumes

Port mapping:

-p host:container

Example:

-p 3000:3000

Volumes persist data:

-v mongo_data:/data/db

Docker Networking

  • Containers communicate via service/container name
  • Host accesses services via localhost

Docker Compose Overview

Docker Compose runs multi-container apps from one YAML file.

Use when working with:

  • App + database
  • Local development
  • Multiple services

Basic template:

services:
  app:
    image: app-image
    ports:
      - "3000:3000"
    env_file: .env
    depends_on: [mongo]

  mongo:
    image: mongo
    volumes:
      - mongo_data:/data/db

volumes:
  mongo_data:

Docker vs Vercel

  • Vercel deploys application code
  • Docker deploys the full runtime environment

Docker is preferred for backend-heavy or multi-service systems.


Production Checklist

  • .env excluded via .dockerignore
  • .env.example committed
  • Multi-stage builds enabled
  • Secrets injected at runtime
  • No credentials inside images
@ 2026 Shailesh