MeshWorld India LogoMeshWorld.
CheatsheetDockerDevOpsContainersLinuxDeveloper ToolsCloud8 min read

Docker & Docker Compose Cheat Sheet: Images, Containers & CLI (2026)

Vishnu
By Vishnu
|Updated: Jul 30, 2026
Docker & Docker Compose Cheat Sheet: Images, Containers & CLI (2026)

Docker revolutionizes software deployment by packaging applications and their dependencies into standardized, lightweight containers. Mastering Docker CLI and Docker Compose V2 enables developers to achieve environment parity between local development and cloud production clusters.

Key Takeaways

  • Use `docker run -d -p <host>:<container>` to launch background containerized applications.
  • Leverage Docker Compose V2 (`docker compose up -d`) to orchestrate multi-container services.
  • Write multi-stage Dockerfiles to minimize production image footprints and eliminate dev dependencies.
  • Run `docker system prune -a --volumes` to clean up dangling images, unused volumes, and stopped containers.

How do you build, inspect, tag, and manage Docker images?

Docker images are read-only templates containing application source code, runtime environments, and system libraries.

Docker Image Operations

CommandAction
docker build -t myapp .Build an image from Dockerfile in current directory
docker build -t myapp:1.0 .Build an image with a specific version tag
docker imagesList all locally cached Docker images
docker rmi myappRemove a local image
docker rmi $(docker images -q)Remove all local images
docker pull nginxDownload an image from Docker Hub
docker tag myapp myapp:v1Tag an image with a new repository/version tag
docker push user/myapp:v1Upload an image to Docker Hub or remote registry
docker history nginxInspect layer creation history of an image
docker inspect nginxDisplay full JSON metadata for an image

How do you run, manage, inspect, and debug Docker containers?

Containers are isolated, runnable instances of Docker images that execute isolated processes on the host OS kernel.

Container Execution (docker run)

CommandAction
docker run nginxRun a container in the foreground
docker run -d nginxRun a container detached in the background
docker run -d -p 8080:80 nginxMap host port 8080 to container port 80
docker run -d --name web nginxAssign a custom container name
docker run -it ubuntu bashLaunch container with interactive pseudo-TTY terminal
docker run --rm nginxAutomatically remove container when it stops
docker run -e VAR=value nginxInject an environment variable
docker run --env-file .env nginxLoad environment variables from a .env file
docker run -v /host:/container nginxBind mount a local host directory into container
docker run -v myvolume:/data nginxAttach a named Docker volume
docker run --network mynet nginxConnect container to a specific network bridge
docker run --restart always nginxAutomatically restart container if it crashes
docker run -m 512m --cpus 1.5 nginxRestrict memory to 512MB and CPU to 1.5 cores

Container Lifecycle & Management

CommandAction
docker psList currently running containers
docker ps -aList all containers (running and stopped)
docker stop webGracefully stop a running container (SIGTERM)
docker kill webForcefully terminate a container immediately (SIGKILL)
docker start webStart a stopped container
docker restart webRestart a container
docker rm webRemove a stopped container
docker rm -f webForce remove a running container
docker rm $(docker ps -aq)Remove all stopped containers across system
docker rename old_name new_nameRename an existing container
docker pause web / docker unpause webPause / resume all processes inside container

Container Inspection & Debugging

CommandAction
docker logs webFetch container stdout/stderr log output
docker logs -f webFollow container logs in real time
docker logs --tail 100 webDisplay last 100 log entries
docker logs --since 1h webDisplay logs generated within the last hour
docker inspect webInspect full container state and JSON configuration
docker statsMonitor live stream of container CPU, RAM, and network I/O
docker top webDisplay active processes running inside container
docker diff webInspect modified files in container filesystem
docker port webList container port mappings
docker exec -it web bashOpen an interactive shell inside running container
docker cp web:/app/file.log ./Copy file from container to local host path
docker cp ./config.json web:/app/Copy file from local host path into container

How do you manage Docker Volumes, Networks, and Disk Cleanup?

Volumes persist application state beyond container lifecycles, while custom bridge networks facilitate inter-container communication.

Volumes & Networks

CommandAction
docker volume lsList all active Docker volumes
docker volume create mydataCreate a new named volume
docker volume inspect mydataDisplay volume mount path and metadata
docker volume rm mydataDelete a specific volume
docker volume pruneDelete all unused volumes
docker network lsList all Docker networks
docker network create mynetCreate a custom bridge network
docker network connect mynet webAttach a running container to a network
docker network disconnect mynet webDetach a container from a network
docker network rm mynetDelete a custom network
docker network pruneDelete all unused networks

System Cleanup & Pruning

CommandAction
docker system dfDisplay Docker disk space consumption
docker system pruneRemove stopped containers, dangling images, and unused networks
docker system prune -aRemove stopped containers, unused networks, and ALL unreferenced images
docker system prune -a --volumesComplete Reset: Remove all unreferenced images, containers, and volumes

How do you manage multi-container apps with Docker Compose V2?

Docker Compose orchestrates multi-container applications defined in a declarative docker-compose.yml file.

Common Compose V2 Commands

CommandAction
docker compose upBuild, create, and start services in foreground
docker compose up -dStart services in background (detached mode)
docker compose up --buildForce rebuild of custom container images before starting
docker compose downStop and remove container services and networks
docker compose down -vStop services and remove attached volumes
docker compose psList status of all service containers
docker compose logs -fFollow logs across all services
docker compose exec web bashExecute shell inside a running service container
docker compose run web bashLaunch a one-off container command

Production docker-compose.yml Template

yaml
services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgres://user:pass@db:5432/mydb
    volumes:
      - ./uploads:/app/uploads
    depends_on:
      - db
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: mydb
    volumes:
      - pgdata:/var/lib/postgresql/data
    restart: unless-stopped

volumes:
  pgdata:

What are best practices for multi-stage Dockerfiles and one-liner tricks?

Multi-stage builds decouple build-time compilation tools from runtime environments, yielding smaller, more secure container images.

Multi-Stage Dockerfile (Dockerfile)

dockerfile
# Stage 1: Build compilation
FROM node:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Stage 2: Production runtime
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package*.json ./
RUN npm ci --omit=dev
EXPOSE 3000
CMD ["node", "dist/server.js"]

Essential Docker One-Liners

bash
# Stop all currently running containers
docker stop $(docker ps -q)

# Remove all stopped containers
docker rm $(docker ps -aq)

# Get a container's IP address
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web

# Rebuild and restart a single compose service
docker compose up -d --build web

# Export an image to a tar archive
docker save myapp:v1 > myapp.tar

# Import an image from a tar archive
docker load < myapp.tar

Frequently Asked Questions

What is the difference between docker run and docker exec?

docker run creates and starts a brand-new container from an image. docker exec executes a command inside an already running container.

What is the difference between Docker Compose V1 and V2?

Docker Compose V2 is written in Go and integrated directly into the Docker CLI (docker compose), replacing the legacy Python-based standalone docker-compose binary.


Share_This Twitter / X
Vishnu
Written By

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Enjoyed this article?

Support MeshWorld and help us create more technical content