The Docker Command Cheatsheet Every Developer Needs on Their Desk
You know the feeling. It's late, you're in the zone, and then—what's the flag for exposing ports again? You tab over to Google, type the same query you've typed a hundred times, and break your flow.
We've all been there. Docker is incredibly powerful, but its command-line interface isn't exactly memorable. Between docker run, docker-compose, docker build, and all their various flags, there's a lot to keep in your head—or not.
This cheatsheet is the one I wish I had when I started. I've organized it by what you're actually trying to do, with copy-paste commands and brief explanations so you understand why they work.
Table of Contents
- Building Images
- Running Containers
- Docker Compose Essentials
- Debugging Running Containers
- Cleaning Up
- Networking
- Volumes and Data
- Real-World Scenarios
- FAQ & Troubleshooting
Building Images
Basic Build
docker build -t myapp:latest .
-
-ttags the image with a name and optional tag -
.is the build context (current directory)
Build with No Cache (Fresh Build)
docker build --no-cache -t myapp:latest .
Use this when your builds are behaving strangely or you want to ensure all dependencies are fresh.
Build with Multiple Tags
docker build -t myapp:latest -t myapp:v1.2.3 .
Tag with both a version and latest for easier rollbacks.
Build with Build Arguments
docker build --build-arg NODE_ENV=production -t myapp:latest .
Pass environment variables into the build process.
Build with a Specific Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .
Useful when you have different Dockerfiles for different environments.
Running Containers
Basic Run
docker run -d -p 3000:3000 --name mycontainer myapp:latest
-
-druns in detached mode (background) -
-pmaps host port to container port -
--namegives the container a friendly name
Run with Environment Variables
docker run -d -p 3000:3000 -e DATABASE_URL=postgres://localhost/db myapp:latest
Or use an env file:
docker run -d -p 3000:3000 --env-file .env myapp:latest
Run with Volume Mount (Development)
docker run -d -p 3000:3000 -v $(pwd)/src:/app/src myapp:latest
Mount your local code into the container for hot reloading.
Run Interactively (Shell Access)
docker run -it --rm myapp:latest /bin/bash
-
-ikeeps STDIN open -
-tallocates a pseudo-TTY -
--rmremoves the container when you exit
Run with Resource Limits
docker run -d --memory=512m --cpus=0.5 myapp:latest
Prevent runaway containers from eating all your resources.
Docker Compose Essentials
Start Services
docker-compose up -d
The -d flag runs in detached mode.
Rebuild and Start
docker-compose up -d --build
Force a rebuild of all services.
Stop Services
docker-compose down
Stop and remove containers, networks, and the default network.
Stop and Remove Volumes
docker-compose down -v
Use when you want a completely fresh start.
View Logs
docker-compose logs -f
The -f flag follows log output in real-time.
Execute Command in Running Service
docker-compose exec app /bin/bash
Get a shell inside a running service container.
Debugging Running Containers
View Container Logs
docker logs -f mycontainer
Follow logs in real-time. Add --tail 100 to see only recent entries:
docker logs -f --tail 100 mycontainer
Execute Command in Running Container
docker exec -it mycontainer /bin/bash
Get an interactive shell inside a running container.
Inspect Container Details
docker inspect mycontainer
See all the nitty-gritty details: IP address, mounts, environment variables, etc.
View Container Resource Usage
docker stats mycontainer
See CPU, memory, and network usage in real-time.
Copy Files from Container
docker cp mycontainer:/app/logs ./logs
Pull files out of a container for local inspection.
Copy Files to Container
docker cp ./config.json mycontainer:/app/config.json
Push files into a running container (useful for quick config changes).
Cleaning Up
Remove Stopped Containers
docker container prune
Remove Unused Images
docker image prune
Remove ALL Unused Resources
docker system prune
This removes stopped containers, unused networks, and dangling images.
Remove Everything (Including Volumes)
docker system prune -a --volumes
Warning: This is nuclear. It removes all unused images, containers, networks, and volumes.
Find Large Images to Remove
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | sort -k3 -h
Sort images by size to find disk hogs.
Networking
List Networks
docker network ls
Create a Network
docker network create mynetwork
Connect Container to Network
docker network connect mynetwork mycontainer
Run Container on Specific Network
docker run -d --network mynetwork --name app myapp:latest
Containers on the same network can communicate by container name.
Volumes and Data
List Volumes
docker volume ls
Create a Named Volume
docker volume create mydata
Use a Named Volume
docker run -d -v mydata:/app/data myapp:latest
Named volumes persist even after the container is removed.
Inspect a Volume
docker volume inspect mydata
Find where the data is actually stored on your host.
Real-World Scenarios
Scenario 1: Quick Database for Local Development
docker run -d \
--name postgres-dev \
-e POSTGRES_PASSWORD=devpass \
-e POSTGRES_DB=myapp \
-p 5432:5432 \
-v postgres-dev-data:/var/lib/postgresql/data \
postgres:15
You now have a PostgreSQL database running locally with persistent data.
Scenario 2: Debug Why a Container Won't Start
# Run without the entrypoint
docker run -it --rm --entrypoint /bin/bash myapp:latest
# Or check the logs of a failed container
docker logs $(docker ps -ql)
The docker ps -ql gives the ID of the most recent container, even if it exited.
Scenario 3: One-Liner to Stop and Remove All Containers
docker stop $(docker ps -aq) && docker rm $(docker ps -aq)
Useful when your Docker environment has become a mess.
Scenario 4: Check What's Eating Disk Space
docker system df
Shows disk usage by images, containers, local volumes, and build cache.
FAQ & Troubleshooting
"Permission Denied" Errors on Linux
Add your user to the docker group:
sudo usermod -aG docker $USER
Log out and back in for changes to take effect.
Container Won't Start — Check the Logs
docker logs mycontainer
If the container exits immediately, try running it interactively:
docker run -it --rm myapp:latest /bin/bash
Port Already in Use
Find what's using the port:
# Linux/macOS
lsof -i :3000
# Or
netstat -tulpn | grep 3000
Kill the process or use a different port.
"No Space Left on Device"
Clean up unused resources:
docker system prune -a
If that doesn't help, check for dangling volumes:
docker volume ls -qf dangling=true | xargs docker volume rm
Docker Compose Fails to Connect to Network
Make sure you're using the project name consistently:
docker-compose -p myproject up -d
Or set COMPOSE_PROJECT_NAME in your .env file.
How Do I Update a Container?
- Pull the new image:
docker pull myapp:latest - Stop the old container:
docker stop mycontainer - Remove the old container:
docker rm mycontainer - Start a new one:
docker run -d --name mycontainer myapp:latest
With Docker Compose, it's simpler:
docker-compose pull && docker-compose up -d
Conclusion
Docker's CLI has a lot of commands, but you don't need to memorize them all. Bookmark this page, and next time you forget how to copy a file out of a container or why docker-compose down -v wiped your database, you'll have the answer.
The real power comes from understanding when to use each command, not just how. Once you internalize the patterns—build, run, debug, clean—you'll spend less time fighting Docker and more time actually building.
Happy containerizing! 🐳
Top comments (0)