Docker: Working with Container Images

Updated: Jan 25

In this tutorial, you will learn the core aspects of Docker images, including:

  • The history of images.

  • How to Tag an image.

  • How to View the available images on the host,

  • How to Delete an Image,

  • How to clean up the storage being used by Docker.


Let's start this tutorial by talking about Docker's layer model for managing its container images. A container image takes up less disk space in the local server storage because all modifications made in the image are saved in a layer of its own. If, for example, we have a docker image based on the fedora image, then all the newly created images would only take up storage space for what you've added to the base image.


The layer in the image is set as "Read-Only", and each new layer is a delta of the layer underneath it. Also, the layering model provides another great advantage by speeding things up with repetitive tasks such as rebuilding images that are modified (using caching). In some cases, we will want to limit the number of layers in an image for simplicity using docker files.


Now, it's an excellent time to take the theoretical into practice, starting with downloading a new image using the "pull" command. The Docker pull command takes an image name as an argument and downloads that image to the localhost server.

PS C:\> docker pull nginx


This command will only download the image without creating a container from it. Now that the docker image is successfully downloaded, we can check its presence by running the Docker image command:

Note: we also can use Docker Desktop to see the UI representation for the available images:

And now for the interesting part, if we want to check the history of all the layers constructing this image, we can use the Docker history command and specify an image as an argument:

PS C:\> docker history nginx

The output here shows each layer line by line (In future tutorials, I will cover the topic of how to create your image layers), along with the commands that were executed to create that layer. Regarding the hashes reported as "missing", you can ignore it as it relates to the build cache that is used to make the image not being available locally.


now let's continue and run the following command:

docker images --no-trunc

The "--no-trunc" option tells Docker not to truncate the output. As you can see, Docker did not cut the length of the "sha256" hashes as it did in the previous Docker images command that we run earlier. So why do we need to care about it? Because now er can see the actual and complete name of the image as Docker reference to it in the local server, this can save us a lot of trouble when we need to refer to a specific image.



Docker Tags

You might also notice that we have a column named "TAG". A tag is used to convey helpful information about the resource, such as the resource size, the image version, and more (if no specific tag is specified, then the default tag of latest is used). The tag points to a particular version of an image; if you want to create a tag for a given image, you can use the Docker tag command. So just for practice, we will use the Docker help library to check what can we do when using this tag:

Now you can see the exact syntax we will use to create our tag. Let's say that we've tested this particular Ingenix image, and it's working just fine. We want to make sure we can use this exact image for future deployments. To do that, we'll tag that image with something meaningful to us. Let's say that we are using this image to host our web application and decide to tag this image with the app and underscore stable.


so to do that, we will use the following command:

PS C:\> docker tag nginx:latest nginx:MyAPP_Stable

Let's take a closer look using the images command:

if you look closely, you'll see that the image ID is the same hash for the Nginx images. Meaning that Docker hasn't used up additional space to create a new image but only created an alias to the original one. And one commonly asked question is, "What will happen if you delete the original image"? There is nothing to worry about as the new image will still be available.



Docker Files

We've mentioned Docker Files on a few occasions already, and I think it's a good time for us to add some information about what it means. So, let's take a look at the image layers from a docker file perspective. Yet again, I will use the Nginx image, which runs a Web Server similar to the tags available on our image.


Let's start by navigating to the Docker website and searching for the containers tab; you can find it here, just Log-in with your credentials: https://hub.docker.com/search?q=nginx&type=image


Now when searching for 'Nginx', and click on the official build available:

When clicking on this image, you'll see the supported tags and respective Docker Files for a given tag.

So to see the Docker file, click on the version which in our case is the latest available: (1.21.4 latest):


How to remove an image

If you need to remove an image from the local system, use the docker rmi command and specify the image ID. Also, before removing it, the image must be stopped first. Otherwise, you will get the following error:

So, in this example, I use the -f argument to force the stop of the container currently running:



21 views0 comments