DEV Community

Manoj Kumar Patra
Manoj Kumar Patra

Posted on • Updated on

Docker Cheat Sheet - Container Images

Image

An image contains the application binaries, application dependencies, metadata about the image and how to run the image.

Images on Docker Hub

Official images on Docker Hub just have an <image-name> unlike others which follow the format <author-name>/<image-name>.

Pull an image:

# Pulls the latest version
docker pull <image-name>
Enter fullscreen mode Exit fullscreen mode

Pull a specific version of the image:

docker pull <image-name>:<image-version>
Enter fullscreen mode Exit fullscreen mode

Pull a specific version and distribution of the image:

docker pull <image-name>:<image-version>-<dist>
Enter fullscreen mode Exit fullscreen mode

Push image changes

To upload changed layers to an image registry:

docker image push <image-name>
Enter fullscreen mode Exit fullscreen mode

NOTE: To perform push, we need to login with docker login.

List all images:

docker image ls
Enter fullscreen mode Exit fullscreen mode

Image layer

Image layers present a series of file system changes as an actual file system.

View image history

docker history <image-name>:image-version>
Enter fullscreen mode Exit fullscreen mode

Each layer in the image has a unique SHA, so that, if another image has a same version of a layer and the layer exists in the cache, that layer will be re-used, i.e., the rest of the layers in the image are run on top of the cached layer.

Same layers are not stored more than once on the host system.

Container layer

Container layer is a read and write layer created on top of the image layer.

When a file on the image is changed as part of the container, the difference in the file is copied over to the container (copy on write) without making any changes in the history of the container.

Inspect image

# Get JSON metadata about the image
docker inspect <image-name>:<image-version>
Enter fullscreen mode Exit fullscreen mode

Image tags

Tagging an image

docker image tag <source-image>:<source-tag> <target-image>:<target-tag>
Enter fullscreen mode Exit fullscreen mode

Tags will default to latest unless specified.

latest is just a default tag that should be assigned to the newest stable version.

Build an image

docker image build -t <image-name> .
Enter fullscreen mode Exit fullscreen mode

The . will search for the default Dockerfile.

-t is a shorthand for tag.

Prune commands

Clean up all dangling images:

docker image prune
Enter fullscreen mode Exit fullscreen mode

Clean up everything:

docker system prune
Enter fullscreen mode Exit fullscreen mode

Remove all unused images:

docker image prune -a
Enter fullscreen mode Exit fullscreen mode

View space usage:

docker system df
Enter fullscreen mode Exit fullscreen mode

Top comments (0)