Docker Command Cheat Sheet: Managing Containers, Images, and Operations
This guide provides a comprehensive collection of Docker commands for inspecting Docker versions, handling images (listing, searching, pulling, deleting, building), and managing containers (starting, stopping, logging, attaching, executing, committing, and copying files), offering a practical reference for developers and operations engineers.
1. Docker Container Information
## View Docker version
docker version
## View Docker system information
docker info
## Show Docker help
docker --help2. Image Operations
2.1 View Images
## List local images
docker images
## Include intermediate layers
docker images -a
## Show only image IDs
docker images -q
## Include intermediate layers (short IDs)
docker images -qa
## Show digests column
docker images --digests
## Show full image information
docker images --no-trunc2.2 Search Images
## Search MySQL image in Docker Hub
docker search mysql
## Filter by stars >= 600
docker search --filter=stars=600 mysql
## Show full description without truncation
docker search --no-trunc mysql
## List only automated images
docker search --automated mysql2.3 Pull Images
## Pull latest Redis image
docker pull redis
## Pull all tags of Redis
docker pull -a redis
## Pull a private repository image
docker pull bitnami/redis2.4 Delete Images
## Delete a single image
docker rmi redis
## Force delete (even if containers use it)
docker rmi -f redis
## Delete multiple images
docker rmi -f redis tomcat nginx
## Delete all local images
docker rmi -f $(docker images -q)2.5 Build Images
## Write a Dockerfile (example path)
cd /docker/dockerfile
vim mycentos
## Build image from Dockerfile
docker build -f /docker/dockerfile/mycentos -t mycentos:1.13. Container Operations
3.1 Start Containers
## Run container interactively with a name
docker run -i -t --name mycentos
## Run container in detached mode
docker run -d mycentosNote: Using docker ps -a after a detached run may show the container as exited because Docker requires a foreground process to keep the container alive.
## Start stopped containers
docker start redis
## Restart a container
docker restart redis3.2 Container Processes
## List processes inside a container (similar to ps)
# docker top [OPTIONS] CONTAINER [ps OPTIONS]
docker top redis
## List processes of all running containers
for i in `docker ps | grep Up | awk '{print $1}'`; do echo && docker top $i; done3.3 Container Logs
## Show logs (default)
docker logs rabbitmq
## Follow logs with timestamps, show last 20 lines
docker logs -f -t --tail=20 redis
## Show last 10 lines since a specific date
docker logs --since="2019-05-21" --tail=10 redis3.4 Attach and Exec
## Run container and immediately get a shell
docker run -it centos /bin/bash
## Exit container without stopping it (Ctrl+P+Q)
# (shortcut)
## Attach to a running container (shares the same TTY)
# Ensure CTRL-D or CTRL-C do not stop the container
docker attach --sig-proxy=false centos
## Execute a command in a running container (interactive)
docker exec -i -t centos /bin/bash
## Execute a command and return output to current terminal
docker exec -i -t centos ls -l /tmp
## Execute a command in detached mode (no output)
docker exec -d centos touch cache.txt3.5 View Containers
## List running containers
docker ps
## List only container IDs of running containers
docker ps -q
## List all containers (running + stopped)
docker ps -a
## Show total file size of running containers
docker ps -s
## Show most recently created container
docker ps -l
## Show last 3 created containers
docker ps -n 3
## Do not truncate output
docker ps --no-trunc3.6 Stop and Delete Containers
## Stop a running container
docker stop redis
## Kill a running container
docker kill redis
## Remove a stopped container
docker rm redis
## Force remove a running container
docker rm -f redis
## Remove multiple containers
docker rm -f $(docker ps -a -q)
# or
docker ps -a -q | xargs docker rm
## Remove container network link named db
docker rm -l db
## Remove container and its mounted volumes
docker rm -v redis3.7 Commit Container to Image
## Commit a container as a new image with author and message
docker commit -a "DeepInThought" -m "my redis" [container_id] myredis:v1.13.8 Copy Files Between Host and Container
## Copy file from container to host
docker cp rabbitmq:/[container_path] [local_path]
## Copy file from host to container
docker cp [local_path] rabbitmq:/[container_path]/
## Copy and rename file inside container
docker cp [local_path] rabbitmq:/[container_path]Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
