Docker Image

An Docker image is basically a big tarball containing a filesystem.

Docker Image Explained

Docker image is composed of many layers. Each layer contains files and folders. Docker uses a union filesystem to create a virtual filesystem out of the layers. All layers in a Docker image are immutable.

When the container is created from an Image. Docker adds a writable layer on top of the immutable layers.

Tag

Each image has a tag. A tag is used to version image.

Image Operations

Search Image

Search the Docker Hub for images

1
docker search image_name

Example

1
docker search busybox

Pull Image

By default, docker pull pulls images from Docker Hub.

If no tag is provided, Docker Engine uses the :latest tag as a default.

Syntax

1
docker pull image_name

Example to pull the latest alpine image

1
docker pull alpine

Example to pull MySQL image version 5.7

1
docker pull mysql:5.7

Example to pull from a private registry hosted at https://localhost:5000

1
docker pull localhost:5000/test/test-image

List Image

docker image command

1
docker images

docker images can take an optional [REPOSITORY[:TAG]] argument. You can set REPOSITORY but no TAG or REPOSITORY only

1
2
3
4
5
docker images maven
REPOSITORY TAG IMAGE ID CREATED SIZE
maven 3.6.3-jdk-11-slim 481506cc2cd1 2 weeks ago 419MB
maven 3.5-jdk-8-alpine fb4bb0d89941 18 months ago 119MB
maven 3.5.3-jdk-8-alpine 562eb2188339 22 months ago 119MB

You can use filterring flag( -f or –filter) to list with filter

To list dangling images

1
2
3
4
5
$ docker images --filter "dangling=true"
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 8abc22fbb042 4 weeks ago 0 B
<none> <none> 48e5f45168b9 4 weeks ago 2.489 MB
<none> <none> bf747efa0e2f 4 weeks ago 0 B

To list images before given image id(ab4287b7a939)

1
2
3
4
5
$ docker images -f "before=ab4287b7a939"
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest f7adfc4dbcf5 8 months ago 413MB
openjdk 8-jre-alpine f7a292bbb70c 11 months ago 84.9MB
hello-world latest fce289e99eb9 15 months ago 1.84kB

docker images command can follow option -q to show only the numeric IDs. This is useful when it comes to removing image.

Example to use docker image in conjunction with docker rmi to remove images. see Example to remove dangling images:

1
docker rmi $(docker images -f "dangling=true" -q)

Reference

Build Image from Dockerfile

1
docker build -t image_name directory_path

Remove Image

To remove an image

1
docker rmi image_name/image_id

To remove dangling images

1
docker rmi $(docker images -f "dangling=true" -q)

This also removes all dangling images

1
docker image prune

Save and load

Example to save an image to a tarball

1
docker image save -o /tmp/testimage.tar testimage:v1

Example to load an image from a tarball

1
docker image load -i /tmp/testimage.tar

Docker Registry

A Docker registry stores many Docker Repositories. A Docker Repository contains many versions of the same Docker image.

Docker Trusted Registry(DTR) is the enterprise-level registry from Docker.

When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry.

Registry is also a Docker image you can run.

1
docker run -d -p 5000:5000 --name registry registry

Push Image

Example to push image to docker hub

First tag an image. A tag is used to version images.

1
docker image tag 196d12cf6ab1 username/image:v1

Next, Need to login to the account. Docker Hub in this case

1
docker login -u username

Push image to Docker Hub

1
docker image push username/image:v1

Reference