๐Ÿ”ฅSave up to $132K/month in CI costs!Try Freeโ†’
Skip to main content

Docker Cheat Sheet - Most Useful Commands

7 min read
Author: Maria Silva
Container Solutions Architect
I help teams design smarter container solutions that make DevOps workflows smoother and more reliable. โš™๏ธ

Introductionโ€‹

After working with Docker for a while, I have noted there could be hundreds of different Docker commands out there. But really, in my workflow, it's the same about 20-30 of these. Here's my personal cheat sheet of the most practical Docker commands I use regularly.

Interactive Docker Commands Explorerโ€‹

I've created this interactive tool to help you find and explore Docker commands more easily. You can filter by category, search for specific commands, and copy them directly to your clipboard:

Container Managementโ€‹

Running Containersโ€‹

The most basic Docker command is docker run. Following is how I use it in various situations:

docker run -d --name webserver nginx:latest

This command starts an Nginx container in detached mode (-d) with a given name. I use this when I need a quick web server for testing.

docker run -d -p 8080:80 nginx:latest

Maps port 8080 on your host to port 80 in the container. Useful when you need to use container services from your host machine.

docker run -d \
-e POSTGRES_PASSWORD=mysecret \
-e POSTGRES_DB=myapp \
-v postgres_data:/var/lib/postgresql/data \
postgres:13

I use this pattern for database containers where persistence and configuration matter a lot.

Container Lifecycleโ€‹

docker ps

All running containers are shown here. I am using this above command very frequently to check up on container status, ports, and names.

docker logs -f container_name

The -f flag follows the log output. Indispensable in debugging problems within running containers.

docker exec -it container_name bash

Gives you a shell in the container. I'm using this constantly to debugging and for one-off commands.

Docker cheat sheet: Image Managementโ€‹

Working with Images

docker pull nginx:latest

Pulls an image from Docker Hub. Always specify a tag to avoid getting unexpected versions.

docker build -t myapp:1.0 .

Builds an image from a Dockerfile in the current directory. The -t flag tags the image with a name and version.

docker images

Lists all local images - I use this to check available images and their sizes.

Volume Managementโ€‹

Volumes are vital in establishing persistence in data:

docker volume create mydata

Create a Persistent Volume. I use these for database data and other stateful applications.

docker run -d \
-v mydata:/data \
nginx:latest

Mounts the volume 'mydata' to /data inside the container. This is required for persistence of data.

Network Managementโ€‹

Networking is the key to Container Communication:

docker network create mynetwork

Creates an isolated network for container communication. This is useful when I want to establish multi-container applications.

docker run -d --network mynetwork nginx:latest

Connects a container to a certain network. Useful for container-to-container communication.

Docker Composeโ€‹

This makes Docker Compose very important to manage multi-container applications:

docker-compose up -d

Starts all the services as defined in docker-compose.yml. I use this daily to start my development environments.

docker-compose down

Stops and removes all containers, networks created by docker-compose up.

Cleanup Commandsโ€‹

These commands clean up the Docker environment:

docker system prune

Remove all stopped containers, unused networks, dangling images and build cache. I run this once a week because it saves me some disk space.

docker rm -v container_name

Remove a container and its associated volumes. Volumes will be deleted and volume data will be lost. Done

Troubleshootingโ€‹

These are some very helpful debugging commands:

docker stats container_name

Display live resource usage statistics. Great for debugging performance of containers.

docker inspect container_name

Displays detailed configuration information about a container. This is useful to debug networking and volume issues.

Best Practicesโ€‹

Based on my experience, here are some key practices to follow:

  1. Always tag your images: Never use 'latest' in production
  2. Use named volumes: Simplifies data management
  3. Regular cleanup: Use system prune to keep disk space free
  4. Monitor logs: Regular log checking helps catch issues early

Security and Complianceโ€‹

Security has become a crucial part of my Docker workflow, especially when working with enterprise clients. Here are some essential security-focused commands I use:

SBOM (Software Bill of Materials)โ€‹

docker sbom example-image:latest
docker sbom example-image:latest --output sbom.txt
docker sbom example-image:latest --format spdx-json

I generate these SBOMs to maintain transparency in our software supply chain. It's particularly important when working with security-conscious clients.

Vulnerability Scanningโ€‹

docker scan example-image:latest
docker scan example-image:latest --file Dockerfile
docker scan example-image:latest --severity high

I always run these scans before deploying any container to production. It's saved me from potential security issues multiple times.

Docker Hub Operationsโ€‹

These are the commands I use daily for Docker Hub interactions:

docker login
docker logout
docker search nginx

Advanced Featuresโ€‹

Resource Monitoringโ€‹

For performance troubleshooting, I rely on these monitoring commands:

docker stats container_name
docker stats $(docker ps --format={{.Names}}) # Monitor all containers

Config Contextsโ€‹

When working with multiple Docker environments:

docker context create my-remote --docker "host=ssh://user@remote-server"
docker context ls
docker context use my-remote
docker context rm old-context

Advanced Cleanupโ€‹

Here's my detailed cleanup routine that I use to manage disk space:

docker system prune --volumes  # Remove everything unused
docker image prune -a # Remove all unused images
docker volume prune # Remove all unused volumes
docker container prune # Remove all stopped containers

Quick Referenceโ€‹

Here's a comprehensive table of all the commands covered in this cheat sheet:

CategoryCommandDescription
Container Managementdocker run -d --name webserver nginx:latestRun container in detached mode
docker run -d -p 8080:80 nginx:latestRun with port mapping
docker psList running containers
docker logs -f container_nameFollow container logs
docker exec -it container_name bashAccess container shell
Image Managementdocker pull nginx:latestPull image from registry
docker build -t myapp:1.0 .Build image from Dockerfile
docker imagesList local images
Volume Managementdocker volume create mydataCreate volume
docker run -v mydata:/data nginx:latestRun with volume mount
Network Managementdocker network create mynetworkCreate network
docker run --network mynetwork nginx:latestRun with network
Docker Composedocker-compose up -dStart services
docker-compose downStop services
Cleanupdocker system pruneRemove unused resources
docker rm -v container_nameRemove container and volumes
docker system prune --volumesRemove all unused resources
docker image prune -aRemove unused images
docker volume pruneRemove unused volumes
Securitydocker sbom example-image:latestGenerate SBOM
docker scan example-image:latestScan for vulnerabilities
Docker Hubdocker loginLog into Docker Hub
docker logoutLog out from Docker Hub
docker search nginxSearch images
Advanceddocker stats container_nameMonitor container resources
docker context create my-remoteCreate new context
docker context lsList contexts
docker context use my-remoteSwitch context
docker inspect container_nameView container details

Conclusionโ€‹

These are the essentials for my day-to-day work with Docker. Of course, Docker has many more commands, but once mastered, these will cover about 90% of your needs to manage containers. Keep this cheat sheet handy; I still refer back to it fairly often when working in different environments.