Category: Docker
Docker Cheat Sheet
This Docker cheat sheet provides a complete reference for working with containers, images, volumes, networks, Dockerfiles, Docker Compose, and orchestration.
1. Running Containers
# Run a container with limited CPU
docker run --cpus=.5 ubuntu
# Run a container with limited memory
docker run --memory=100m ubuntu
# Run interactively with a shell
docker run -it ubuntu /bin/bash
# Detached mode (background)
docker run -d ubuntu
# Run a web container and publish host port 8080 to container port 80
docker run -d -p 8080:80 httpd
# View running processes inside the container
docker exec <container-id> ps -eaf
# Open an interactive shell
docker exec -it <container-id> /bin/bash
2. Docker Storage Location
Docker stores local container data in:
/var/lib/docker
- aufs / overlay2 – storage layers
- containers – container metadata
- images – image layers
- volumes – named volume data
3. Docker Volumes
Create a volume
docker volume create volume_name
docker volume ls
docker volume inspect volume_name
Mount a named volume
docker run -v volume_name:/var/lib/mysql mysql
Note: If the volume does not exist, Docker will create it automatically.
Bind mount (preferred)
docker run --mount type=bind,source=/data/mysql,target=/var/lib/mysql mysql
4. Networking Basics
# Map host port 8080 to container port 80
docker run -d -p 8080:80 httpd
# Get a container IP on the bridge network
docker inspect -f "{{ .NetworkSettings.Networks.bridge.IPAddress }}" <container-id>
5. Storage Drivers
| Storage Driver | Supported Filesystems |
|---|---|
| overlay2 / overlay | ext4, xfs (ftype=1) |
| aufs | ext4, xfs |
| devicemapper | direct-lvm |
| btrfs | btrfs |
| zfs | zfs |
| vfs | any |
docker info
6. Inspect Containers, Images & Networks
# Full metadata
docker inspect <id>
# Image layer history
docker history <image>
7. Image Management & Disk Usage
# List images
docker images
# Remove image
docker rmi <id>
# Remove unused images
docker image prune -a
# Disk usage summary
docker system df
8. Building Docker Images
Example Dockerfile
FROM ubuntu
RUN apt-get update && apt-get -y install python3 python3-pip
RUN pip3 install flask flask-mysql
COPY app.py /opt/source-code
ENTRYPOINT flask_app=/opt/source-code/app.py flask run --host=0.0.0.0
Build commands
# Build using Dockerfile in current dir
docker build . -t simple-webapp
# Using a custom Dockerfile
docker build -f Dockerfile2 -t myorg/app2 .
9. Docker Compose
docker-compose.yml example
version: '3'
services:
web:
image: "simple-webapp"
ports:
- "5000:5000"
redis:
image: "redis:alpine"
Start everything:
docker-compose up
Python app Dockerfile for Compose
FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
10. Multi-Container Stack Example
# Redis
docker run -d --name=redis redis
# PostgreSQL
docker run -d --name=db postgres:9.4
# Voting app
docker run -d --name=vote -p 5000:80 voting-app
# Result app
docker run -d --name=result -p 5001:80 result-app
# Worker
docker run -d --name=worker worker
Legacy container linking (not used anymore)
docker run -d --name=vote --link redis:redis -p 5000:80 voting-app
docker run -d --name=result --link db:db -p 5001:80 result-app
docker run -d --name=worker --link db:db --link redis:redis worker
11. Cleanup Commands
# Remove all containers
docker rm -f $(docker ps -a -q)
# Remove selected containers
docker container rm <id1> <id2>
# Remove unused images
docker image prune -a
12. Docker Swarm (Legacy — Rarely Used Today)
Docker Swarm was Docker’s native orchestrator. While simple and fast to set up, it is now considered a legacy technology. Today, nearly all container orchestration is performed using Kubernetes, including lightweight versions like K3s, MicroK8s, and K3d.
Why Docker Swarm Is No Longer Common
- Minimal development in recent years with few updates.
- Lacks modern orchestration features such as CRDs, Operators, service mesh, and advanced autoscaling.
- No ecosystem compared to Kubernetes (no Helm, no Ingress controllers, no admission controllers).
- Poor scalability vs Kubernetes; struggles beyond moderate cluster sizes.
- Nearly zero enterprise adoption as all major clouds support Kubernetes instead.
Swarm still works for very small clusters, but it is not recommended for new deployments.
Legacy Swarm Commands (For Reference Only)
# Initialize a swarm
docker swarm init
# Rebuild a failed manager
docker swarm init --force-new-cluster
# Promote worker to manager
docker node promote <node>
# Drain node
docker node update --availability drain <node>
# Create replicated service
docker service create --replicas=3 -p 8080:80 my-web-server
# Global service (one per node)
docker service create --mode global my-agent
# Update service replicas
docker service update --replicas=4 web-server
13. Overlay Networking (Swarm or Legacy Use)
# No network
docker run ubuntu --network=none
# Host networking
docker run ubuntu --network=host
# Create overlay network
docker network create --driver overlay --subnet 10.0.9.0/24 my-overlay-network
# Attach service to overlay
docker service create --replicas 2 --network my-overlay-network nginx
