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!