Docker Commands Cheatsheet
Docker commands for images, containers, and Docker Compose
Your data never leaves the browser — all processing is done locally 0 server requests
Images
-
docker pull nginx:latestDownload an image from a registry
-
docker imagesList locally available images
-
docker build -t myapp:1.0 .Build an image from the Dockerfile in the current directory
-
docker rmi 이미지명Remove an image
-
docker image prune -aRemove all unused images ※ use with caution
-
docker tag myapp:1.0 user/myapp:1.0Tag an image with a new name (registry path)
-
docker push user/myapp:1.0Push an image to a registry
Running Containers
-
docker run -d -p 8080:80 --name web nginxRun in background and map host port 8080 to container port 80
-
docker run -it --rm ubuntu bashOpen a shell in a temporary container, auto-removed on exit
-
docker run -e KEY=value 이미지Run a container with an environment variable injected
-
docker run -v $(pwd):/app 이미지Mount the current directory into the container at /app
-
docker run --restart unless-stopped 이미지Automatically restart the container when Docker restarts
Container Management
-
docker psList running containers (-a includes stopped ones)
-
docker stop webGracefully stop a container (start/restart also available)
-
docker rm webRemove a stopped container (-f force-removes a running one)
-
docker container pruneRemove all stopped containers at once
-
docker rename old newRename a container
Debugging
-
docker logs -f --tail 100 webStream logs live starting from the last 100 lines
-
docker exec -it web bashOpen a shell inside a running container (use sh if bash is unavailable)
-
docker inspect webShow detailed container configuration (IP, mounts, etc.) as JSON
-
docker statsShow real-time CPU and memory usage per container
-
docker cp web:/var/log/nginx/access.log ./Copy a file from the container to the host
-
docker port webShow the port mappings of a container
Volumes & Networks
-
docker volume lsList volumes (create/rm/inspect also available)
-
docker volume pruneRemove unused volumes ※ data will be deleted
-
docker network lsList networks
-
docker network create mynetCreate a custom network (containers can communicate by name)
-
docker network connect mynet webConnect a running container to a network
Docker Compose
-
docker compose up -dStart all services in compose.yaml in the background
-
docker compose downStop services and remove containers and networks (-v also removes volumes ※ caution)
-
docker compose psCheck the status of services
-
docker compose logs -f 서비스명Stream logs for a specific service in real time
-
docker compose build --no-cacheRebuild images without using cache
-
docker compose exec 서비스명 bashOpen a shell inside a service container
-
docker compose restart 서비스명Restart a specific service
System Cleanup
-
docker system dfShow disk usage by images, containers, and volumes
-
docker system pruneRemove stopped containers, unused networks, and dangling images
-
docker system prune -a --volumesRemove all unused resources ※ irreversible, proceed with caution
-
docker builder pruneClear the build cache
Docker: Containers from Build to Production
Docker packages applications and their dependencies into portable containers. The basic workflow is: write a Dockerfile → docker build to create an image → docker run to start a container. Images are immutable; containers are running instances of images.
docker compose simplifies multi-container setups with a YAML file. docker compose up -d starts all services in the background and docker compose down stops and removes them.
⚠ docker system prune -a and docker volume prune can delete data that is difficult to recover. Always back up important volumes before pruning.
FAQ
- What is the difference between docker stop and docker kill?
- docker stop sends SIGTERM to the container, giving it time to shut down gracefully (default 10-second timeout). docker kill sends SIGKILL immediately, which forces termination without cleanup. Always prefer docker stop first.
- How do I see the logs of a running container?
- Use docker logs <container-id-or-name>. Add -f to follow live output (like tail -f) and --since 1h to limit to the last hour. For Docker Compose, use docker compose logs -f service-name.
- How do I clean up unused Docker resources?
- docker system prune removes all stopped containers, unused networks, dangling images, and build cache. Add -a to also remove unused images (not just dangling). Run docker system df first to see how much space you can reclaim.