Skip to main content

Docker Cheatsheet

A comprehensive reference guide for Docker commands and concepts. This is a quick reference cheat sheet for Docker. And you can find the most common Docker commands here.

Table of Contents


Getting Started

Basic Container Operations

Run Container in Background

docker run -d -p 80:80 docker/getting-started

Flags:

  • -d - Run container in detached mode (background)
  • -p 80:80 - Map host port 80 to container port 80
  • docker/getting-started - Image to use

Run Container in Foreground (Interactive)

docker run -it -p 8001:8080 --name my-nginx nginx

Flags:

  • -it - Interactive mode with pseudo-TTY
  • -p 8001:8080 - Map host port 8001 to container port 8080
  • --name my-nginx - Assign a custom name to the container
  • nginx - Image to use

General Commands

CommandDescription
docker psList running containers
docker ps -aList all containers (including stopped)
docker ps -sList running containers with CPU/memory stats
docker imagesList all images
docker exec -it <container> bashConnect to a running container via bash
docker logs <container>Show container's console logs
docker stop <container>Stop a container gracefully
docker restart <container>Restart a container
docker rm <container>Remove a container
docker port <container>Show container's port mapping
docker top <container>List processes running in container
docker kill <container>Force kill a container

Note: <container> parameter can be either container ID or name


Docker Containers

Starting & Stopping Containers

CommandDescription
docker start my-nginxStart a container
docker stop my-nginxStop a container gracefully
docker restart my-nginxRestart a container
docker pause my-nginxPause a container
docker unpause my-nginxUnpause a container
docker wait my-nginxBlock until container stops
docker kill my-nginxSend SIGKILL to container
docker attach my-nginxConnect to an existing container

Container Information

CommandDescription
docker psList running containers
docker ps -aList all containers
docker logs my-nginxView container logs
docker inspect my-nginxInspect container details (JSON)
docker events my-nginxView container events
docker port my-nginxShow published ports
docker top my-nginxShow running processes
docker stats my-nginxMonitor container resource usage
docker diff my-nginxShow filesystem changes

Creating Containers

docker create [options] IMAGE

Common Options:

  • -a, --attach - Attach stdout/stderr
  • -i, --interactive - Attach stdin (interactive)
  • -t, --tty - Allocate pseudo-TTY
  • --name NAME - Assign name to container
  • -p, --publish 5000:5000 - Port mapping (host:container)
  • --expose 5432 - Expose port to other containers
  • -P, --publish-all - Publish all exposed ports
  • --link container:alias - Link to another container
  • -v, --volume \pwd`:/app` - Mount volume (absolute paths needed)
  • -e, --env NAME=hello - Set environment variables

Example:

docker create --name my_redis --expose 6379 redis:3.0.2

Container Manipulation

Renaming a Container

docker rename my-nginx my-nginx-new

Removing a Container

docker rm my-nginx

Updating Container Resources

docker update --cpu-shares 512 -m 300M my-nginx

Docker Images

Image Manipulation

CommandDescription
docker imagesList all images
docker rmi nginxRemove an image
docker load < ubuntu.tar.gzLoad image from tar archive
docker load --input ubuntu.tarLoad image from tar file
docker save busybox > ubuntu.tarSave image to tar archive
docker history nginxShow image layer history
docker commit nginxCreate image from container changes
docker tag nginx eon01/nginxTag an image
docker push eon01/nginxPush image to registry

Building Images

# Various build methods
docker build .
docker build github.com/creack/docker-firefox
docker build - < Dockerfile
docker build - < context.tar.gz
docker build -t eon/my-nginx .
docker build -f myOtherDockerfile .
curl example.com/remote/Dockerfile | docker build -f - .

Docker Networking

Network Management

Removing a Network

docker network rm MyOverlayNetwork

Listing Networks

docker network ls

Inspecting a Network

docker network inspect MyOverlayNetwork

Connecting/Disconnecting Containers

# Connect running container to network
docker network connect MyOverlayNetwork nginx

# Connect at container startup
docker run -it -d --network=MyOverlayNetwork nginx

# Disconnect container from network
docker network disconnect MyOverlayNetwork nginx

Creating Networks

# Create overlay network
docker network create -d overlay MyOverlayNetwork

# Create bridge network
docker network create -d bridge MyBridgeNetwork

# Create complex overlay network with custom settings
docker network create -d overlay \
--subnet=192.168.0.0/16 \
--subnet=192.170.0.0/16 \
--gateway=192.168.0.100 \
--gateway=192.170.0.100 \
--ip-range=192.168.1.0/24 \
--aux-address="my-router=192.168.1.5" \
--aux-address="my-switch=192.168.1.6" \
--aux-address="my-printer=192.170.1.5" \
--aux-address="my-nas=192.170.1.6" \
MyOverlayNetwork

Clean Up

System Prune

Basic Cleanup

# Clean up dangling images, containers, volumes, and networks
docker system prune

Comprehensive Cleanup

# Remove stopped containers and all unused images
docker system prune -a

Container Cleanup

Stop All Running Containers

docker stop $(docker ps -a -q)

Delete Stopped Containers

docker container prune

Image Cleanup

Remove Dangling Images

# Remove untagged images not associated with containers
docker image prune

Remove All Unused Images

# Remove all images not used by existing containers
docker image prune -a

Volume Cleanup

# Remove all volumes not used by at least one container
docker volume prune

Miscellaneous

Docker Hub Operations

CommandDescription
docker search search_wordSearch Docker Hub for images
docker pull user/imageDownload image from Docker Hub
docker loginAuthenticate to Docker Hub
docker push user/imageUpload image to Docker Hub

Registry Commands

Authentication

# Login to registry
docker login
docker login localhost:8080

# Logout from registry
docker logout
docker logout localhost:8080

Image Management

# Search for images
docker search nginx
docker search nginx --stars=3 --no-trunc busybox

# Pull images
docker pull nginx
docker pull eon01/nginx localhost:5000/myadmin/nginx

# Push images
docker push eon01/nginx
docker push eon01/nginx localhost:5000/myadmin/nginx

Batch Operations

# Stop all containers
docker stop -f $(docker ps -a -q)

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

# Remove all images
docker rmi -f $(docker images -q)

Volume Management

# List volumes
docker volume ls

# Clean up unused volumes
docker volume prune

Tips

  • Use --help with any Docker command to see available options
  • Container names must be unique
  • Use -f flag for force removal when containers are running
  • Always specify versions in image tags for production use
  • Use .dockerignore files to exclude unnecessary files from build context

This cheatsheet covers the most common Docker commands and scenarios. For more detailed information, refer to the official Docker documentation.