This post covers essential Docker commands youโll use regularly when working with containers, images, volumes, and the Docker system itself.
๐ณ Container Management
๐ Run a Container
Start a container from an image and expose ports:
docker run -d -p 13008:6080 -p 13009:8060 --name cloudide registry.cn-shanghai.aliyuncs.com/rgsoft/cloudide_java:latest
๐ Stop Containers
Stop all running containers:
docker container stop $(docker ps -qa)
Stop containers based on a specific image:
docker container stop $(docker ps -qa --filter "ancestor=6b8e62f218e1")
๐งน Remove Containers
Remove containers created from a specific image:
docker container rm $(docker ps -qa --filter "ancestor=98d8bb571885")
๐ Inspect Container Details
Retrieve a container’s IP address:
docker inspect <container_id_or_name> | grep "IPAddress"
Check the size of a container (writable layer and virtual total):
docker container inspect <container_id_or_name> --size
# or
docker inspect -s <container_id_or_name>
Output Example:
SIZE
8.61GB (virtual 25.7GB)
8.61GB
: Writable layer โ data added or changed since the container started.25.7GB
: Total size = base image + writable layer.
๐ผ๏ธ Image Management
๐ ๏ธ Build an Image
Build an image from a Dockerfile:
docker build -t cloudide-vnc-java:study -f Dockerfile .
๐ธ Commit a Container to an Image
Create a new image from a running container:
docker commit <container_id> <new_image_name>
๐ค Save & ๐ฅ Load Images
Export an image to a .tar
file:
docker save -o my_image.tar my_image:latest
Import an image from a .tar
file:
docker load -i my_image.tar
๐งน Flatten a Container (Export & Import)
If you’ve made many manual changes inside a running container and want to keep them while reducing the image size, consider flattening the container using docker export
and docker import
.
# 1. Export the current container's filesystem as a tar stream and import it as a new image
docker export test-container | docker import - my-new-clean-image
# 2. Stop and remove the old container
docker stop test-container && docker rm test-container
# 3. Start a new container from the flattened image
docker run -dit --name new-container my-new-clean-image
๐ง Why Flatten?
- Keeps your runtime changes while removing Docker image history.
- Reduces image size by excluding deleted files (e.g., after uninstalling large packages like Conda).
- Useful for archiving or deployment when build reproducibility is not required.
โ ๏ธ Note
This process creates a โblack boxโ image:
- No build history
- No labels or Dockerfile instructions
- No multi-stage build benefits
๐พ Volumes
Coming soon โ how to create, inspect, and mount Docker volumes for persistent storage.
๐งฎ System Information
๐ฝ Check Disk Usage
See how much space Docker is using for images, containers, volumes, and cache:
docker system df
๐ง Summary
This cheat sheet provides a quick reference to many common Docker operations. Whether you’re cleaning up containers, managing images, or inspecting your system’s resource usage, these commands help keep your Docker environment healthy and efficient.
Have suggestions or want more topics like networking or volume management? Feel free to reach out!
---
Let me know if youโd like:
- A **Markdown file download** version
- Diagrams or visuals for image lifecycle
- A follow-up post about **Docker volumes**, **networking**, or **multi-stage builds**
Ready to push to your Hugo repo anytime!