Best Free Online Tools

๐Ÿณ Docker Cheat Sheet

Quick reference guide for Docker commands and best practices. All your notes are saved locally in your browser.

๐Ÿณ Docker Basics

Check Docker Version

Display Docker version information

docker --version
docker version  # More detailed info

Docker Info

Display system-wide information

docker info

Docker Help

Get help for Docker commands

docker --help
docker <command> --help

๐Ÿ“ฆ Working with Images

List Images

Show all Docker images on your system

docker images
docker image ls

Pull an Image

Download an image from Docker Hub

docker pull <image-name>
docker pull ubuntu:22.04
docker pull nginx:latest

Build an Image

Build an image from a Dockerfile

docker build -t <image-name>:<tag> .
docker build -t myapp:1.0 .
docker build -t myapp:latest --no-cache .

Remove Images

Delete Docker images

docker rmi <image-id>
docker rmi <image-name>:<tag>
docker image prune  # Remove unused images
docker image prune -a  # Remove all unused images

Tag an Image

Create a new tag for an existing image

docker tag <source-image> <target-image>:<tag>
docker tag myapp:latest myapp:1.0

Push an Image

Upload an image to a registry

docker push <image-name>:<tag>
docker push username/myapp:latest

๐Ÿ“ฆ Container Management

Run a Container

Create and start a new container

docker run <image-name>
docker run -d <image-name>  # Detached mode
docker run -it <image-name> bash  # Interactive mode
docker run -p 8080:80 <image-name>  # Port mapping
docker run --name mycontainer <image-name>  # Named container
docker run -v /host/path:/container/path <image-name>  # Volume mount

List Containers

Show running or all containers

docker ps  # Running containers
docker ps -a  # All containers (including stopped)
docker container ls
docker container ls -a

Stop/Start Containers

Control container lifecycle

docker stop <container-id>
docker start <container-id>
docker restart <container-id>
docker pause <container-id>
docker unpause <container-id>

Remove Containers

Delete stopped containers

docker rm <container-id>
docker rm -f <container-id>  # Force remove running container
docker container prune  # Remove all stopped containers

Container Logs

View container output

docker logs <container-id>
docker logs -f <container-id>  # Follow log output
docker logs --tail 100 <container-id>  # Last 100 lines

Execute Commands in Container

Run commands inside a running container

docker exec <container-id> <command>
docker exec -it <container-id> bash
docker exec -it <container-id> sh

Inspect Container

Display detailed information about a container

docker inspect <container-id>
docker inspect --format='{{.NetworkSettings.IPAddress}}' <container-id>

Container Stats

Display real-time resource usage statistics

docker stats
docker stats <container-id>

๐Ÿ“„ Dockerfile Commands

Basic Dockerfile

Example of a simple Dockerfile

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Multi-stage Build

Optimize image size with multi-stage builds

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

# Production stage
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package*.json ./
RUN npm install --production
EXPOSE 3000
CMD ["node", "dist/server.js"]

Common Dockerfile Instructions

Key Dockerfile commands

FROM <image>  # Base image
WORKDIR /path  # Set working directory
COPY <src> <dest>  # Copy files
ADD <src> <dest>  # Copy files (can extract archives)
RUN <command>  # Execute command during build
ENV KEY=value  # Set environment variable
EXPOSE <port>  # Document exposed ports
CMD ["executable", "param1"]  # Default container command
ENTRYPOINT ["executable"]  # Configure container executable
VOLUME /data  # Create mount point
USER username  # Set user for subsequent commands
ARG VAR=default  # Build-time variable

๐Ÿ’พ Volumes & Data Management

Create Volume

Create a named volume

docker volume create <volume-name>
docker volume create mydata

List Volumes

Show all volumes

docker volume ls

Inspect Volume

Display detailed volume information

docker volume inspect <volume-name>

Remove Volumes

Delete volumes

docker volume rm <volume-name>
docker volume prune  # Remove all unused volumes

Mount Volume

Attach a volume to a container

docker run -v <volume-name>:/container/path <image>
docker run -v mydata:/app/data nginx
docker run --mount source=mydata,target=/app/data nginx

๐ŸŒ Networking

List Networks

Show all Docker networks

docker network ls

Create Network

Create a new network

docker network create <network-name>
docker network create mynetwork
docker network create --driver bridge mynetwork

Connect Container to Network

Add a container to a network

docker network connect <network-name> <container-id>
docker run --network=<network-name> <image>

Inspect Network

Display detailed network information

docker network inspect <network-name>

Remove Network

Delete a network

docker network rm <network-name>
docker network prune  # Remove unused networks

๐ŸŽผ Docker Compose

Basic docker-compose.yml

Example compose file

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
  db:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

Compose Commands

Common docker-compose operations

docker-compose up  # Start services
docker-compose up -d  # Start in detached mode
docker-compose down  # Stop and remove containers
docker-compose ps  # List containers
docker-compose logs  # View logs
docker-compose logs -f  # Follow logs
docker-compose exec <service> <command>  # Execute command
docker-compose build  # Build images
docker-compose restart  # Restart services

Compose with Build

Build custom images with compose

version: '3.8'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
    depends_on:
      - db
  db:
    image: postgres:15
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password

๐Ÿงน System Cleanup

Prune System

Clean up unused Docker resources

docker system prune  # Remove unused data
docker system prune -a  # Remove all unused data
docker system prune --volumes  # Include volumes
docker system df  # Show disk usage

Remove All Stopped Containers

Clean up stopped containers

docker container prune
docker rm $(docker ps -a -q)  # Remove all containers

Remove All Unused Images

Delete dangling and unused images

docker image prune  # Remove dangling images
docker image prune -a  # Remove all unused images
docker rmi $(docker images -q)  # Remove all images

๐Ÿ’ก Docker Best Practices

  • โ€ขUse .dockerignore to exclude unnecessary files from your image
  • โ€ขKeep images small by using Alpine Linux or multi-stage builds
  • โ€ขDon't run containers as root - use the USER instruction
  • โ€ขUse specific image tags instead of "latest" in production
  • โ€ขLeverage build cache by ordering Dockerfile instructions properly
  • โ€ขUse health checks to ensure containers are running properly
Docker Cheat Sheet - Quick Reference Guide | Best Free Online Tools