When it comes to Docker, it’s difficult to talk about one concept without involving some part of another Docker concept. You are likely to notice this correlation in this post as well where we will talk about Docker images. Here we will talk a bit about what a Dockerfile is and mainly focus on creating Docker images and also understand what is a Docker base image.
What is a Docker image?
A docker image is what forms the blueprint of our desired application which forms the basis of the containers that we intend to run. An image is a combination of a file system and parameters that are needed to support and run a container. To explain how we use images in a real-world environment we will use a sample scenario that is explained next.
Let’s consider a scenario wherein we are working for an organization called Linuxnix and we hire some developers to write Python code for us. In production, we happen to use Ubuntu 16.04 and so the requirement is that the code should work in Ubuntu. Now what we will download an image of Ubuntu 16.04 from the Ubuntu repository on Docker hub and use that as our base image. We want to make sure that we are working with the most secure and updated version of the OS so we will run apt-get update within the container and finally install Python version3 in the container. We will be using a Dockerfile to create this image and we will discuss that in greater detail in a different post. Once we’ve created this image we can push it to our repository on Docker hub and ask our developers to pull the Ubuntu image that we have published on our Docker repository and get started hassle-free. Then the developers could write a hello world script in python to ensure that everything is working and upload/push it back up to our repository.
Now that we have a scenario ready, let’s get started.
Step 1: Pull down image for Ubuntu 16.04
First, let’s check if we have any images on our system at the moment with the below commands.
[sahil@linuxnix ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 6 months ago 1.84kB [sahil@linuxnix ~]$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 6 months ago 1.84kB [sahil@linuxnix ~]$
Note that both commands provide the same output. The hello-world image is the one we used to validate our docker install in an earlier post. Now we will pull the image for Ubuntu 16.04 with the Docker pull command.
[sahil@linuxnix ~]$ docker pull ubuntu:16.04 16.04: Pulling from library/ubuntu 35b42117c431: Pull complete ad9c569a8d98: Pull complete 293b44f45162: Pull complete 0c175077525d: Pull complete Digest: sha256:a4d8e674ee993e5ec88823391de828a5e9286a1597b731eaecaaf9066cfdf539 Status: Downloaded newer image for ubuntu:16.04 [sahil@linuxnix ~]$
We could’ve simply typed docker pull ubuntu and that would’ve downloaded the latest image for the Ubuntu container which may also be an edge release and not the latest stable release. Therefore, we added the tag 16.04 to it to ensure that we are downloading the required image only. Notice that the in the output of the docker pull command multiple pull operations occurred and were completed. In this case, the multiple pull operations account for multiple layers of the image. Layers get created when we make modifications to the base image. For example, if we update the base image that we’ve downloaded then that would add one layer. Next, when we install Python 3 on the image then that would add another layer. To understand how multiple layers got applied onto the image that we are currently using it’s best to check out it Dockerfile which should ideally be available at Docker Hub.
you can view the docker file for our Ubuntu 16:04 image by clicking here. Every change that occurs to the image will add a new layer. Now we will explain the different fields of the docker image command output as they pertain to our Ubuntu image.
[sahil@linuxnix ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 16.04 13c9f1285025 2 weeks ago 119MB
- Under the repository section, we’ll have the repository name listed which in this case is Ubuntu (the repository on Docker hub from where the image was downloaded).
- The tag attribute is generally used to denote the version of the operating system or the application that runs inside the container.
- The IMAGE ID for the image is something that we can use to refer to the image beside its name while we are working with it.
- The image id that we see in the output is not the full image id and to view that we need to run docker images –no-trunk.
[sahil@linuxnix ~]$ docker images --no-trunc REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 16.04 sha256:13c9f1285025c03cfd56a2809973bfec93a6468953c4d0ed70afb1f492f50489 2 weeks ago 119MBThe above output shows that the image id is, in fact, a SHA256 hash that has been composed of all the image ids of the layers that are involved in the creation of this image.
- The image id also has a significance when it comes to security and authenticity allowing you to validate that the image that you have downloaded is the intended authentic image to be used.
- The created attribute tells us when the particular image was created.
- The value mentioned in the size field denotes the size of the container image.
This concludes our discussion on Docker images. We hope that this post helped improve your understanding of Docker images and we assure you that there are more Docker posts on the way.
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021