Docker Cheatsheet
10xdev.blog/cheatsheets
# 1. Core Concepts
# Image: A read-only template used to create containers.
# It contains the application code, libraries, and dependencies.
# Built from a Dockerfile.

# Container: A runnable instance of an image. It's a lightweight,
# isolated environment. You can start, stop, and delete containers.

# Dockerfile: A text file with instructions on how to build a
# Docker image.

# Volume: A way to persist data generated by and used by Docker
# containers. Volumes are managed by Docker and are the preferred
# mechanism for persisting data.

# Network: Allows containers to communicate with each other and
# with the host machine or external networks.
# 2. Dockerfile Instructions
# Use an official base image
FROM node:16-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install project dependencies
RUN npm install

# Copy the rest of the application source code
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Define environment variables
ENV NODE_ENV=production

# The command to run when the container starts
CMD ["node", "server.js"]
# 3. Container Lifecycle
# Build an image from a Dockerfile
# -t gives the image a name (tag)
docker build -t my-node-app .

# Run a container from an image
# -d: run in detached mode (background)
# -p: map host port to container port (host:container)
# --name: assign a name to the container
docker run -d -p 8080:3000 --name my-app-container my-node-app

# List running containers
docker ps

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

# Stop a running container
docker stop my-app-container

# Start a stopped container
docker start my-app-container

# Remove a stopped container
docker rm my-app-container

# Force remove a running container
docker rm -f my-app-container
# 4. Image Management
# List all images on the host
docker images

# Remove an image
docker rmi my-node-app

# Pull an image from a registry (like Docker Hub)
docker pull nginx:latest

# Push an image to a registry (requires login)
# docker login
# docker tag my-node-app your-username/my-node-app
# docker push your-username/my-node-app
# 5. Inspection & Debugging
# View the logs of a container
docker logs my-app-container

# Follow the logs in real-time
docker logs -f my-app-container

# Execute a command inside a running container
# -it: interactive terminal
docker exec -it my-app-container sh

# Inspect a container to see detailed information
docker inspect my-app-container
# 6. Data & Volumes
# Create a named volume
docker volume create my-data-volume

# Run a container and mount the named volume
docker run -d --name my-db -v my-data-volume:/var/lib/mysql mysql

# Run a container and mount a host directory (bind mount)
# This is useful for development to see code changes live.
docker run -d -p 3000:3000 -v $(pwd):/app my-node-app

# List volumes
docker volume ls

# Remove a volume
docker volume rm my-data-volume
# 7. Docker Compose
# docker-compose.yml defines a multi-container application.
version: '3.8'

services:
  # The web application service
  web:
    build: . # Build from the Dockerfile in the current directory
    ports:
      - "8000:8000"
    volumes:
      - .:/app
  # The database service
  db:
    image: postgres:13
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=mydb
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pass

volumes:
  db-data: # Defines the named volume

# --- Docker Compose Commands ---
# docker-compose up -d   # Start services in detached mode
# docker-compose down    # Stop and remove containers, networks
# docker-compose logs -f # View logs
# docker-compose ps      # List services
master* 0 0