Docker Commands Cheat Sheet
Basic Docker Commands
Check Docker version:
docker --version
Get help:
docker [command] --help
Working with Images
List images:
docker images
Pull an image from Docker Hub:
docker pull [image]:[tag]
Build an image from a Dockerfile:
docker build -t [image]:[tag] .
Remove an image:
docker rmi [image]:[tag]
Managing Containers
Create a container (without starting it):
docker create [image]:[tag]
Run a container:
docker run [options] [image]:[tag]
- Options:
-d
: Detached mode (run container in background)-p
: Publish a container's port(s) to the host (e.g.,-p 80:80
)--name
: Assign a name to the container List running containers:
docker ps
List all containers (including stopped):
docker ps -a
Stop a running container:
docker stop [container_id or name]
Start a stopped container:
docker start [container_id or name]
Remove a container:
docker rm [container_id or name]
Execute a command in a running container:
docker exec [options] [container_id or name] [command]
- Options:
-it
: Interactive terminal
Docker Networks
List networks:
docker network ls
Create a network:
docker network create [network_name]
Connect a container to a network:
docker network connect [network_name] [container_id or name]
Disconnect a container from a network:
docker network disconnect [network_name] [container_id or name]
Docker Volumes
List volumes:
docker volume ls
Create a volume:
docker volume create [volume_name]
Remove a volume:
docker volume rm [volume_name]
Mount a volume: Use the -v
or --mount
flag with docker run
(e.g., -v [volume_name]:[path_in_container]
or --mount source=[volume_name], target=[path_in_container]
)
Docker Compose (For multi-container applications)
Start services:
docker-compose up
Stop services:
docker-compose down
Rebuild services:
docker-compose up --build
List services:
docker-compose ps
If you have enjoyed this post, please consider buying me a coffee ☕ to help me keep writing!