๐ŸŒ Detecting your locationโ€ฆ

Docker Complete Guide 2026: Containers, Compose and Best Practices

โฑ๏ธ3 min read  ยท  442 words
Docker Complete Guide 2026: Containers, Compose and Best Practices

Docker is the standard way to build, ship, and run applications in 2026. Every cloud platform โ€” AWS, GCP, Azure โ€” runs Docker containers. This guide covers Docker from first install to multi-container apps with Docker Compose.

Install Docker

# Ubuntu / Debian
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER
newgrp docker

# Verify
docker --version
docker run hello-world

Core Concepts

  • Image: Blueprint for a container (read-only)
  • Container: Running instance of an image
  • Dockerfile: Instructions to build an image
  • Registry: Storage for images (Docker Hub, ECR, GCR)

Your First Dockerfile

# Dockerfile
FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

# Build image
docker build -t myapp:latest .

# Run container
docker run -d -p 8000:8000 --name myapp myapp:latest

# View logs
docker logs myapp

# Stop & remove
docker stop myapp && docker rm myapp

Essential Docker Commands

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# List images
docker images

# Execute command inside container
docker exec -it myapp /bin/bash

# Copy file from container
docker cp myapp:/app/output.log ./output.log

# Remove all stopped containers + unused images
docker system prune -af

Docker Volumes (Persistent Data)

# Named volume โ€” data persists between container restarts
docker run -d 
  -v postgres_data:/var/lib/postgresql/data 
  -e POSTGRES_PASSWORD=secret 
  --name postgres 
  postgres:16

# Bind mount โ€” mount host directory
docker run -d 
  -v $(pwd)/data:/app/data 
  myapp:latest

Docker Compose โ€” Multi-Container Apps

# docker-compose.yml
services:
  web:
    build: .
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql://postgres:secret@db:5432/mydb
    depends_on:
      - db
    volumes:
      - .:/app

  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: mydb
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

# Start all services
docker compose up -d

# View logs
docker compose logs -f

# Stop everything
docker compose down

# Rebuild and restart
docker compose up -d --build

Multi-Stage Builds (Smaller Images)

# Stage 1: Build
FROM node:22-alpine AS builder
WORKDIR /app
COPY package*.json .
RUN npm ci
COPY . .
RUN npm run build

# Stage 2: Production (no dev dependencies)
FROM node:22-alpine AS production
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]

Conclusion

Docker eliminates the “works on my machine” problem. Learn Dockerfile basics, master Docker Compose for local dev, and you will ship consistent apps from laptop to cloud without surprises.

MD Rafikul Islam

Written by

MD Rafikul Islam is a software developer and the editor of TechPulse. He writes about developer tooling, hardware, and the practical decisions that come up in day-to-day engineering work โ€” which laptop to buy, which framework to commit to, why a build broke at 2am. He tests the tools he writes about and says plainly when something is not worth the money. Corrections and corrections requests are welcome at rony.yf25@gmail.com.

โœ๏ธ Leave a Comment

Your email address will not be published. Required fields are marked *