Home About Me

Essential Docker Commands for Working with Images

Docker image management revolves around a small set of practical commands: listing what is already on your machine, pulling what you need, checking disk usage, cleaning up unused images, and moving images between environments.

List local images

Use the following command to view images stored locally:

docker images

Common options:

  • -a: show all local images, including historical image layers
  • -q: display only image IDs

Pull an image

To download an image from a registry:

#不加TAG默认拉取最新版
docker pull 镜像名[:TAG]

If no TAG is specified, Docker pulls the latest version by default.

Search for available images

You can look up images in a registry with:

docker search [镜像名]

Option:

  • --limit: return only the first N results, with a default of 25

Example:

docker search --limit 5 redis

Check how much space Docker is using

To inspect disk usage for images, containers, and volumes:

docker system df

This is useful when local Docker data starts taking up more space than expected.

Remove an image

Delete an image by ID, name, or name:TAG:

docker rmi [镜像ID/镜像名/镜像名:TAG]

Option:

  • -f: force removal

A more aggressive variant removes all local images and should be used carefully:

强制删除所有镜像(慎用):
docker rmi -f $(docker image -qa)

Export an image

To save an image into a tar archive:

docker save [镜像名/镜像ID] -o xxx.tar
# -o 可以使用 > 替换

This is commonly used when moving images to another machine without pulling them again from a registry.

Import an image

Load a previously exported image with:

docker load -i xxx.tar

Inspect image history

To view the layer history and metadata of an image:

docker history [镜像名/镜像ID]

If you want the full CMD information rather than truncated output, add --no-trunc:

docker history [镜像名/镜像ID] --no-trunc

Retag an image

To assign a new tag or name to an existing image:

docker tag [旧镜像名/镜像ID] [新镜像名]

This is especially handy when preparing an image for a different repository or version label.