17.2. Docker Command Cheat Sheet
Table of Contents
Overview
This chapter gives you a compact reference of the most commonly used Docker commands. It is not a place for long explanations, but a quick way to recall syntax and typical patterns as you work.
Keep this rule in mind: If you are not sure what a command does, especially if it contains rm, prune, or --force, look it up first or test it on a non‑critical system.
Getting Help
Use these when you forget command options or need a quick reminder.
docker help
docker <command> --help
docker compose --help
Working with Images
Pull an image from a registry:
docker pull <image>
docker pull <image>:<tag>
List local images:
docker images
docker image ls
Remove images:
docker rmi <image>
docker image rm <image>
docker image rm <image1> <image2>
Tag images:
docker tag <source-image>:<tag> <target-name>:<tag>
Inspect image metadata:
docker image inspect <image>
Search in Docker Hub from the CLI:
docker search <term>
Deleting an image that is used by existing containers can break those containers. Stop and remove containers first if you intend to fully clean up.
Working with Containers
Run a container from an image:
docker run <image>
docker run --name <name> <image>
docker run -d <image>
docker run -it <image> /bin/bash
List containers:
docker ps
docker ps -a
Stop and start containers:
docker stop <container>
docker start <container>
docker restart <container>
Remove containers:
docker rm <container>
docker rm <container1> <container2>
docker rm -f <container>
View details about a container:
docker inspect <container>
docker rm -f forcefully stops and removes containers. Use it carefully, especially on shared or production environments.
Logs and Debugging
View logs:
docker logs <container>
docker logs -f <container>
docker logs --tail 100 <container>
Run a command inside a running container:
docker exec -it <container> /bin/bash
docker exec -it <container> <command>
Copy files to and from containers:
docker cp <local-path> <container>:/path/in/container
docker cp <container>:/path/in/container <local-path>
Ports, Networking, and Connectivity
Run a container with port mapping:
docker run -p <host-port>:<container-port> <image>
Example:
docker run -p 8080:80 nginx
List networks:
docker network ls
Create and remove networks:
docker network create <network-name>
docker network rm <network-name>
Connect or disconnect containers to networks:
docker network connect <network> <container>
docker network disconnect <network> <container>
Inspect a network:
docker network inspect <network>
Volumes and Data
List volumes:
docker volume ls
Create a volume:
docker volume create <name>
Use a named volume:
docker run -v <volume-name>:/path/in/container <image>
Use a bind mount from the host:
docker run -v /host/path:/path/in/container <image>
Inspect a volume:
docker volume inspect <name>
Remove volumes:
docker volume rm <name>
docker volume rm <name1> <name2>
docker volume rm permanently removes the data in that volume. Check you are not deleting important data.
Building Images with Dockerfiles
Build an image from a Dockerfile:
docker build -t <image-name> .
docker build -t <image-name>:<tag> <path>
Use build arguments:
docker build --build-arg <NAME>=<value> -t <image-name> .
Show build cache usage and progress:
docker build --progress=plain -t <image-name> .
System Cleanup
Remove unused containers, networks, images, and build cache:
docker system prune
docker system prune -a
Remove unused images only:
docker image prune
Remove stopped containers only:
docker container prune
Remove unused volumes only:
docker volume prune
docker system prune -a deletes all unused images and stopped containers. Use it only when you are sure you do not need them anymore.
Resource Usage and Monitoring
Show container resource usage:
docker stats
docker stats <container>
Show running processes inside a container:
docker top <container>
Show low level information:
docker inspect <object-name-or-id>
Docker Compose Basics
Use when working with docker-compose.yml.
Start services:
docker compose up
docker compose up -d
Stop services:
docker compose down
View running services:
docker compose ps
View logs:
docker compose logs
docker compose logs -f
Rebuild and restart:
docker compose up --build
Run a one‑off command in a Compose service:
docker compose run <service> <command>
Quick Patterns to Remember
Run a temporary container and remove it when it exits:
docker run --rm <image> <command>
Map current directory into a container:
docker run -v $(pwd):/app -w /app <image> <command>
Follow a container log until it stops:
docker logs -f <container>
Restart a container on failure:
docker run --restart=on-failure <image>
When combining options, read the full command twice before pressing Enter, especially if it contains rm, prune, or -f. This simple habit can prevent accidental data loss.
Views: 0
Where to Move
Move chapter:
- ☰ 1. Introduction
- ☰ 1.1. What is Docker?
- ☰ 1.2. Why Docker?
- ☰ 1.3. Virtual Machines vs Containers
- ☰ 1.4. When Not to Use Docker
- ☰ 1.5. How Docker Is Used in Real Projects
-
- ☰ 2. Core Concepts
- ☰ 2.1. Images
- ☰ 2.2. Containers
- ☰ 2.3. Docker Engine and Architecture
- ☰ 2.4. Docker Hub and Container Registries
- ☰ 2.5. Tags, Versions, and Digests
-
- ☰ 3. Installing Docker
- ☰ 3.1. Docker on Linux
- ☰ 3.2. Docker on Windows
- ☰ 3.3. Docker on macOS
- ☰ 3.4. Verifying the Installation
- ☰ 3.5. Common Installation Issues
-
- ☰ 4. Your First Docker Container
- ☰ 4.1. Running Your First Container
- ☰ 4.2. Understanding `docker run`
- ☰ 4.3. Interactive vs Detached Mode
- ☰ 4.4. Stopping and Removing Containers
- ☰ 4.5. Listing Containers
-
- ☰ 5. Working with Docker Images
- ☰ 5.1. Pulling Images from Docker Hub
- ☰ 5.2. Listing and Removing Images
- ☰ 5.3. Image Layers Explained
- ☰ 5.4. Image Size Optimization
- ☰ 5.5. Inspecting Images
-
- ☰ 6. Dockerfiles – Building Your Own Images
- ☰ 6.1. What Is a Dockerfile?
- ☰ 6.2. Basic Dockerfile Structure
- ☰ 6.3. Common Instructions (`FROM`, `RUN`, `COPY`, `CMD`, `ENTRYPOINT`)
- ☰ 6.4. Building Images with `docker build`
- ☰ 6.5. Best Practices for Dockerfiles
- ☰ 6.6. Multi-Stage Builds
-
- ☰ 7. Managing Data with Volumes
- ☰ 7.1. Why Containers Are Ephemeral
- ☰ 7.2. Bind Mounts vs Volumes
- ☰ 7.3. Creating and Using Volumes
- ☰ 7.4. Inspecting and Removing Volumes
- ☰ 7.5. Real-World Examples
-
- ☰ 8. Networking in Docker
- ☰ 8.1. Docker Networking Basics
- ☰ 8.2. Bridge Networks
- ☰ 8.3. Port Mapping
- ☰ 8.4. Container-to-Container Communication
- ☰ 8.5. Custom Networks
-
- ☰ 9. Docker Compose
- ☰ 9.1. Why Docker Compose?
- ☰ 9.2. `docker-compose.yml` Structure
- ☰ 9.3. Defining Services, Networks, and Volumes
- ☰ 9.4. Running Multi-Container Applications
- ☰ 9.5. Web App and Database Example
- ☰ 9.6. Docker Compose Best Practices
-
- ☰ 10. Debugging and Monitoring Containers
- ☰ 10.1. Viewing Logs
- ☰ 10.2. Executing Commands in Running Containers
- ☰ 10.3. Inspecting Containers
- ☰ 10.4. Resource Usage
- ☰ 10.5. Common Errors and Fixes
-
- ☰ 11. Security Basics
- ☰ 11.1. Running Containers as Non-Root
- ☰ 11.2. Image Trust and Verification
- ☰ 11.3. Secrets and Environment Variables
- ☰ 11.4. Reducing Attack Surface
- ☰ 11.5. Security Best Practices
-
- ☰ 12. Performance and Optimization
- ☰ 12.1. Reducing Image Size
- ☰ 12.2. Caching and Layer Ordering
- ☰ 12.3. Resource Limits
- ☰ 12.4. Choosing the Right Base Image
-
- ☰ 13. Docker in Development Workflows
- ☰ 13.1. Docker for Local Development
- ☰ 13.2. Hot Reloading with Volumes
- ☰ 13.3. Docker and Git
- ☰ 13.4. Docker in CI/CD Pipelines
- ☰ 13.5. Common Development Patterns
-
- ☰ 14. Deployment Basics
- ☰ 14.1. Running Containers in Production
- ☰ 14.2. Environment Configuration
- ☰ 14.3. Restart Policies
- ☰ 14.4. Logging Strategies
- ☰ 14.5. Basic Scaling Concepts
-
- ☰ 15. Beyond the Basics
- ☰ 15.1. Docker Swarm Overview
- ☰ 15.2. Kubernetes vs Docker
- ☰ 15.3. Container Registries
- ☰ 15.4. When to Move Beyond Docker
-
- ☰ 16. Practical Projects
- ☰ 16.1. Dockerizing a Simple Web App
- ☰ 16.2. Dockerizing a Backend API
- ☰ 16.3. Full-Stack App with Docker Compose
- ☰ 16.4. Common Project Pitfalls
-
- ☰ 17. Troubleshooting and FAQ
- ☰ 17.1. Common Beginner Mistakes
- ☰ 17.2. Docker Command Cheat Sheet
- ☰ 17.3. FAQ
- ☰ 17.4. Useful Resources
-
Comments
Please login to add a comment.
Don't have an account? Register now!