Docker Commands Cheat Sheet
Essential Docker commands every developer should know. From basic container management to advanced networking.
Table of Contents
1. Basic Docker Commands
Check Docker Version
docker --versionDisplays the installed Docker version.
Docker System Info
docker infoShows detailed information about the Docker installation.
Docker Help
docker --helpLists all available Docker commands.
2. Container Management
Run a Container
docker run -d --name my-container -p 8080:80 nginxRuns a container in detached mode with port mapping. -d = detached, -p = port mapping.
List Running Containers
docker psShows all currently running containers.
List All Containers
docker ps -aShows all containers including stopped ones.
Stop a Container
docker stop <container_id>Gracefully stops a running container.
Start a Container
docker start <container_id>Starts a stopped container.
Remove a Container
docker rm <container_id>Removes a stopped container. Use -f to force remove a running container.
Container Logs
docker logs -f <container_id>View container logs. -f follows the log output in real-time.
Execute Command in Container
docker exec -it <container_id> /bin/bashOpens an interactive shell inside the running container.
3. Image Commands
List Images
docker imagesLists all Docker images on your system.
Pull an Image
docker pull nginx:latestDownloads an image from Docker Hub.
Build an Image
docker build -t my-app:1.0 .Builds an image from a Dockerfile in the current directory.
Tag an Image
docker tag my-app:1.0 username/my-app:1.0Creates a tag for an image (useful for pushing to registries).
Push an Image
docker push username/my-app:1.0Pushes an image to Docker Hub or another registry.
Remove an Image
docker rmi <image_id>Removes an image from your system.
4. Volume Commands
Create a Volume
docker volume create my-volumeCreates a new named volume for persistent data storage.
List Volumes
docker volume lsLists all Docker volumes.
Run with Volume
docker run -d -v my-volume:/data nginxRuns a container with a volume mounted at /data.
Bind Mount
docker run -d -v $(pwd):/app nginxMounts current directory to /app in the container.
Remove Volume
docker volume rm my-volumeRemoves a volume. Volume must not be in use.
5. Network Commands
List Networks
docker network lsLists all Docker networks.
Create a Network
docker network create my-networkCreates a new bridge network.
Run Container in Network
docker run -d --network my-network --name app nginxRuns a container connected to a specific network.
Connect Container to Network
docker network connect my-network container_nameConnects an existing container to a network.
Inspect Network
docker network inspect my-networkShows detailed information about a network.
6. Docker Compose
Example docker-compose.yml
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
depends_on:
- db
db:
image: postgres:15
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:Start Services
docker compose up -dStarts all services defined in docker-compose.yml in detached mode.
Stop Services
docker compose downStops and removes all containers, networks created by compose.
View Logs
docker compose logs -fShows combined logs from all services.
Rebuild Services
docker compose up -d --buildRebuilds images and restarts services.
Scale Service
docker compose up -d --scale web=3Runs 3 instances of the web service.
7. Cleanup Commands
⚠️ Warning: These commands permanently delete resources. Use with caution!
Remove All Stopped Containers
docker container pruneRemoves all stopped containers.
Remove Unused Images
docker image prune -aRemoves all images not used by containers.
Remove Unused Volumes
docker volume pruneRemoves all unused volumes.
Remove Everything Unused
docker system prune -a --volumesNuclear option: removes all unused containers, images, networks, and volumes.
Check Disk Usage
docker system dfShows Docker disk usage breakdown.
Written by Ramjee Prasad • Backend Developer
← Back to Portfolio