So far you have seen Docker image is built from Dockerfile
. An image is comprised of multiple layers and it contains the code that gets executed in a Docker container. You can build a custom image, tailor it to the best fit for your need or you can just use docker pull image_name
to download an image from docker hub to your host and then run it using docker run as you did in hello-world example. Now we will look into various aspect of Docker image in depth.
List of handy docker image commands
Execute docker images
to show all top-level images, their repository, tags, and their size.
docker images [OPTIONS] [REPOSITORY[:TAG]]
Refer the official docker images guide
This command is used to build an image from a Dockerfile
docker build [OPTIONS] PATH | URL | .
Build from local machine
When you have the Dockerfile in your host machine, navigate to the folder and run
docker build -t image_name:tag .
Build from GitHub or remote URL
docker build github.com/creack/docker-firefox
Refer the official docker build guide
As you know docker images are made up of multiple layers. Use docker history command to know more about an image.
docker history [OPTIONS] IMAGE
Look at the example below to see how you can check the size of each layer.
Docker inspect is used to get the system level information on Docker objects (images and containers).
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Run docker inspect hello-world
to get inside of low-level information.
There are times when you want to clean up the unnecessary images stored in host/local machine. Use rmi
to remove one or more images.
docker rmi [OPTIONS] IMAGE [IMAGE...]
docker images
will show you the list of images present in your host. docker rmi fd484f19954f
to remove the image that has short id fd484f19954f.
You can not directly remove an image if it is used as layer dependency in another image or if there are any container that uses the image. So remove the dependent image/container first. To delete a container use
docker rm [container_name|container_id]
Use a Docker image
Docker needs to have the corresponding image locally before running it. If the image is not present, docker will try to download it from the image repository (docker registry).
1. Get the image from Docker registry
Use docker pull command to download the image from docker hub.
As I have not specified any registry url, it will pull from Docker Hub. But you can always specify the custom registry if you are using one as docker pull myregistry.local:5000/testing/test-image
Previously I explained, that an image is composed of multi-tiered storage. You can that also see that in the above console log that docker is downloading layer-by-layer, not as a single image file. This optimizes the docker storage and enables to reuse already available layers locally.
2. Run docker image
Technically, you run a container that is created from an image using docker run
. Execute the below command to run this in an interactive mode inside a bash shell.
Now you are running a Nginx:latest container and bash session has started for you in the interactive mode.
-it
: This is just like writting-i
&-t
together.
-i
: Enables/runs the container in interactive mode
-t
: Starts a terminal session inbash
--rm
: This makes sure the container is deleted after it exits. By default, a container is not deleted immediately. We are only enabling this for learning purpose, use cautiously in production.nginx:latest
: This is the image to be used for stating the containerbash
: Interactive bash shell to use with the running container.
Additionally, you can add -p 80:80
to bind the Nginx to port 80.
cat /etc/os-release
prints out the information about OS used in nginx image. Now, type exit
in the terminal to get out of the container and as you rightly guessed, the container will be deleted once you type exit.
Manage docker image (List & delete image )
When you do not want to use a specific image any more or free the disk space, docker allows you to delete the unnecessary images.
1. To list docker image
To list already pulled images.
REPOSITORY – is the image name.
TAG – Is a label, usually a version or build number
IMAGE ID – Uniquely identify the downloaded image (mirrored image)
SIZE – Size of the image on your machine. This could be different from the actual size shown on the docker hub, as it is published in compressed form on docker-hub.
2. List dangling image
When you type docker image ls
, you may find some images without a name and a tag like below
To list out the dangling image specifically
To specifically delete dangling image
docker image prune
3. To list all image – including dangling and the middle layers
Use the below command to list all the images in your local docker repository
4. List similar images
There are situations when you may have multiple images of Nginx with different versions or built on different os. Below you can see alpine linux with different tags.
Delete docker image
To delete a docker image from local machine/host, use the below command.
docker image rm [OPTIONS] IMAGE [IMAGE...]
Alternatively, I prefer using the below one
docker rmi [OPTIONS] IMAGE [IMAGE...]
You can delete image using name (repository), image-id and digest. The image-id could become long to type, so you can pass the first few characters as long as it is enough to distinguish the image from others.
Similarly, you can also use the name:tag
to delete the image.
Untagged images vs deleted images
Observe the output of delete image commands docker rmi
or docker image rm
, you will notice deletion message is basically 2 types, untagged and deleted. Remember, a unique identifier of an image is its ID. But, it can have multiple labels or tags.
When we run the delete command, we ask to delete the image of a tag. Docker checks if there are other images as well that are tagged to this image or if there is an image that uses it as a base image then it will only perform untagged
and it will not delete.
If docker finds no dependent image pointing to the same, then it will perform delete and indicate as deleted
.
How multilayered implementation works?
In docker, each image is made up of multiple layers. Docker uses Union FS to combine these layers into one final image.
Union Fs has 2 purposes, (i) multiple disks can be linked to the same directory, (ii) Unionfs allows any mix of read-only and read-write branches, as well as insertion and deletion of branches anywhere in the fan-out.