DEV Community

AttractivePenguin
AttractivePenguin

Posted on

The Docker Command Cheatsheet Every Developer Needs on Their Desk

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

  1. Building Images
  2. Running Containers
  3. Docker Compose Essentials
  4. Debugging Running Containers
  5. Cleaning Up
  6. Networking
  7. Volumes and Data
  8. Real-World Scenarios
  9. FAQ & Troubleshooting

Building Images

Basic Build

docker build -t myapp:latest .
Enter fullscreen mode Exit fullscreen mode
  • -t tags 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 .
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

Tag with both a version and latest for easier rollbacks.

Build with Build Arguments

docker build --build-arg NODE_ENV=production -t myapp:latest .
Enter fullscreen mode Exit fullscreen mode

Pass environment variables into the build process.

Build with a Specific Dockerfile

docker build -f Dockerfile.prod -t myapp:prod .
Enter fullscreen mode Exit fullscreen mode

Useful when you have different Dockerfiles for different environments.


Running Containers

Basic Run

docker run -d -p 3000:3000 --name mycontainer myapp:latest
Enter fullscreen mode Exit fullscreen mode
  • -d runs in detached mode (background)
  • -p maps host port to container port
  • --name gives the container a friendly name

Run with Environment Variables

docker run -d -p 3000:3000 -e DATABASE_URL=postgres://localhost/db myapp:latest
Enter fullscreen mode Exit fullscreen mode

Or use an env file:

docker run -d -p 3000:3000 --env-file .env myapp:latest
Enter fullscreen mode Exit fullscreen mode

Run with Volume Mount (Development)

docker run -d -p 3000:3000 -v $(pwd)/src:/app/src myapp:latest
Enter fullscreen mode Exit fullscreen mode

Mount your local code into the container for hot reloading.

Run Interactively (Shell Access)

docker run -it --rm myapp:latest /bin/bash
Enter fullscreen mode Exit fullscreen mode
  • -i keeps STDIN open
  • -t allocates a pseudo-TTY
  • --rm removes the container when you exit

Run with Resource Limits

docker run -d --memory=512m --cpus=0.5 myapp:latest
Enter fullscreen mode Exit fullscreen mode

Prevent runaway containers from eating all your resources.


Docker Compose Essentials

Start Services

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

The -d flag runs in detached mode.

Rebuild and Start

docker-compose up -d --build
Enter fullscreen mode Exit fullscreen mode

Force a rebuild of all services.

Stop Services

docker-compose down
Enter fullscreen mode Exit fullscreen mode

Stop and remove containers, networks, and the default network.

Stop and Remove Volumes

docker-compose down -v
Enter fullscreen mode Exit fullscreen mode

Use when you want a completely fresh start.

View Logs

docker-compose logs -f
Enter fullscreen mode Exit fullscreen mode

The -f flag follows log output in real-time.

Execute Command in Running Service

docker-compose exec app /bin/bash
Enter fullscreen mode Exit fullscreen mode

Get a shell inside a running service container.


Debugging Running Containers

View Container Logs

docker logs -f mycontainer
Enter fullscreen mode Exit fullscreen mode

Follow logs in real-time. Add --tail 100 to see only recent entries:

docker logs -f --tail 100 mycontainer
Enter fullscreen mode Exit fullscreen mode

Execute Command in Running Container

docker exec -it mycontainer /bin/bash
Enter fullscreen mode Exit fullscreen mode

Get an interactive shell inside a running container.

Inspect Container Details

docker inspect mycontainer
Enter fullscreen mode Exit fullscreen mode

See all the nitty-gritty details: IP address, mounts, environment variables, etc.

View Container Resource Usage

docker stats mycontainer
Enter fullscreen mode Exit fullscreen mode

See CPU, memory, and network usage in real-time.

Copy Files from Container

docker cp mycontainer:/app/logs ./logs
Enter fullscreen mode Exit fullscreen mode

Pull files out of a container for local inspection.

Copy Files to Container

docker cp ./config.json mycontainer:/app/config.json
Enter fullscreen mode Exit fullscreen mode

Push files into a running container (useful for quick config changes).


Cleaning Up

Remove Stopped Containers

docker container prune
Enter fullscreen mode Exit fullscreen mode

Remove Unused Images

docker image prune
Enter fullscreen mode Exit fullscreen mode

Remove ALL Unused Resources

docker system prune
Enter fullscreen mode Exit fullscreen mode

This removes stopped containers, unused networks, and dangling images.

Remove Everything (Including Volumes)

docker system prune -a --volumes
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Sort images by size to find disk hogs.


Networking

List Networks

docker network ls
Enter fullscreen mode Exit fullscreen mode

Create a Network

docker network create mynetwork
Enter fullscreen mode Exit fullscreen mode

Connect Container to Network

docker network connect mynetwork mycontainer
Enter fullscreen mode Exit fullscreen mode

Run Container on Specific Network

docker run -d --network mynetwork --name app myapp:latest
Enter fullscreen mode Exit fullscreen mode

Containers on the same network can communicate by container name.


Volumes and Data

List Volumes

docker volume ls
Enter fullscreen mode Exit fullscreen mode

Create a Named Volume

docker volume create mydata
Enter fullscreen mode Exit fullscreen mode

Use a Named Volume

docker run -d -v mydata:/app/data myapp:latest
Enter fullscreen mode Exit fullscreen mode

Named volumes persist even after the container is removed.

Inspect a Volume

docker volume inspect mydata
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Useful when your Docker environment has become a mess.

Scenario 4: Check What's Eating Disk Space

docker system df
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Log out and back in for changes to take effect.

Container Won't Start — Check the Logs

docker logs mycontainer
Enter fullscreen mode Exit fullscreen mode

If the container exits immediately, try running it interactively:

docker run -it --rm myapp:latest /bin/bash
Enter fullscreen mode Exit fullscreen mode

Port Already in Use

Find what's using the port:

# Linux/macOS
lsof -i :3000

# Or
netstat -tulpn | grep 3000
Enter fullscreen mode Exit fullscreen mode

Kill the process or use a different port.

"No Space Left on Device"

Clean up unused resources:

docker system prune -a
Enter fullscreen mode Exit fullscreen mode

If that doesn't help, check for dangling volumes:

docker volume ls -qf dangling=true | xargs docker volume rm
Enter fullscreen mode Exit fullscreen mode

Docker Compose Fails to Connect to Network

Make sure you're using the project name consistently:

docker-compose -p myproject up -d
Enter fullscreen mode Exit fullscreen mode

Or set COMPOSE_PROJECT_NAME in your .env file.

How Do I Update a Container?

  1. Pull the new image: docker pull myapp:latest
  2. Stop the old container: docker stop mycontainer
  3. Remove the old container: docker rm mycontainer
  4. Start a new one: docker run -d --name mycontainer myapp:latest

With Docker Compose, it's simpler:

docker-compose pull && docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

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)