Getting Started with Pre-built Docker Images

Images are the fundamental component of docker. In this guide, we will discuss what docker images are, how to build them, how to manage them, and how to use them.

Let's get started.

Prerequisites

  • Before you start using docker images, you need to install docker on your system.
  • This guide uses Ubuntu, but the steps should be similar for other distributions.
  • A user account with root privileges.

What Should You Know?

Before you start working with docker images, it is important that you know what they are.

Docker images are the fundamental components of docker. Docker uses images to create containers. Images are essentially templates that contain all the information necessary to create a container, like the code, libraries, and runtime.

A docker container is a runtime instance of an image. It contains everything that the image contains, plus any changes or additions that you make while the container is running. For example, if you install a new application on your system, the new application will be installed in your container.

Managing Images With the CLI

Managing images with the Docker CLI is a must for Docker administrators. The Docker CLI is a powerful tool that gives you complete control over your images and containers. In this section, you will learn how to use the Docker CLI to manage your images.

1. Run the below command to make sure Docker is running.

sudo systemctl status docker

make sure Docker is running.

2. If Docker is not running, start it with the below command.

sudo systemctl start docker

3. Add your user account to the docker group. Log out and log back in for the changes to take effect.

sudo usermod -aG docker $(whoami)

4. If you run the docker image command, you will see a list of all the options available for the docker image command.

docker image

As shown below, the docker image command has many options, but some of the most common-used ones are:

  • ls: List the images on your system.
  • pull: Pull an image from a registry.
  • push: Push an image to a registry.
  • create: Create a new image from a Dockerfile.
  • rm: Remove an image from your system.
  • tag: Tag an image with a name.

docker image CLI options

To get started, let's take a look at how to list the images on your system.

5. Run the docker image ls command to list the images on your host. The docker image ls command lists all the images on your host, including the repositories that they were pulled from. The ID, the repository, the tag, and the size of the image.

docker image ls

 lists all the images on your host,

Suppose that you want to remove an image from your host. You can use the rm command. You can use the image name or the image ID to remove an image.

docker image rm <image-name>
docker image rm <image-id>

In case you use the ID, make sure the first few characters in the ID are unique to the images in the list. In other words, the first few letters in the ID should not be used by any other images.

6. For example, to remove the ubuntu:12.04 image, you can use any of the following commands. Note that we can just use only the first few characters of the ID(5b1) to remove the image since there are no other images with that first few characters.

docker image rm ubuntu:12.04
docker image rm 5b1

The ubuntu:12.04 image will be deleted from your host along with every layer that is used to create the image, as shown below.

remove the ubuntu:12.04 image

7. List the images on your system again to verify that the ubuntu:12.04 image is no longer listed.

docker image ls

verify that the ubuntu:12.04 image is no longer listed.

Note the docker image ls command and the docker images command does the exact same thing. However, the second is now deprecated since Docker prefer developers to use the docker command with its correct subcommands. We have the same thing with the docker rmi command. This command is used to remove an image and all its layers exact same as docker image rm. Docker recommends to use docker rm with its correct subcommands.

docker image ls
docker images

We have the same output.

docker image ls command and the docker images command does the exact same thing.

So far, you have removed one unused image at a time. What if you want to remove two or more unused images at the same time?

This is where the prune command comes in handy. The prune command can be used to remove all the unused images from your host in one go. You can use the prune command to remove all the dangling images, all the unused images, or a combination of both. Dangling images are the images that don't have at least one container associated with them.

8. Run the docker image prune command to remove all the unused/dangling images from your system.

docker image prune

As you can see, the prune command removed all the unused images and their layers from your host and saved you a lot of disk space in the process.

remove all the unused/dangling images

9. List the images on your host again to verify that the images are really gone. Now that you know how to list, remove, and prune images from your host. These basic commands will help you keep your system clean and organized.

docker image ls

 List the images on your host again

Inspecting Docker Images

You might think of Docker images and containers as mystical black boxes. You are wondering what kind of magic is going on inside them. In this section, we dispel some of the mysticism by taking a look at what is in a Docker image and how you can use that information to your advantage.

1. Docker offers a way to inspect the contents of an image. The docker inspect command takes a path to an image and prints out a wealth of information about it, as shown in the following example.

docker inspect ubuntu

As shown below, the output from the above command contains a lot of information. You are seeing all information here in a format called JSON array.

Inspecting Docker Images

Inspecting Docker Images

The information in the output is shown like a key-value store. You can use a Linux tool like the pipe (|) to grep the output for a specific keyword. Then, you can use the piped output for whatever purpose you want. The following examples demonstrate how you can use do this.

2. Run the below command to save the output of the docker inspect command to a file called image-inspect.txt.

docker image inspect ubuntu > image-inspect.txt

3. Then, use an editor of your choice to open the file and inspect its contents.

sudo nano image-inspect.txt

The output of the docker inspect command contains a wealth of information about an image, as shown below. Inside the editor, you can easily navigate like scroll up/down the editor to inspect different information. You can edit the information if you want.

open the file and inspect its contents.

You can also use the --format argument to format the output in a specific way.

4. For example, to print out the ID of the ubuntu image, you can use the following command.

docker image inspect ubuntu --format='{{.ID}}'

print out the ID of the ubuntu image

5. List the image to verify the image ID.

docker image ls

List the image to verify the ID.

6. Run the below command to pipe the output of the docker inspect command to the more command. This will allow you to paginate through the output so you can see all of the information that is contained in it.

docker image inspect ubuntu | more

This time, you will see the Hostname(b32714f341a6) in the ContainerConfig section, as shown below.

docker inspect command

7. What if you want the hostname value (b32714f341a6) only? You can achieve this by running the below command.

docker image inspect ubuntu --format='{{.ContainerConfig.Hostname}}'

the host name value

As you can see, you can use the docker inspect command with Linux tools to do some more filtering. Like a range. For example, to list all IP addresses in the 192.168.0.0/16 range. And you can even combine it with other scripting tools to do even more cool things.

Using Docker Tags 

As you continue to work with Docker images, you'll want to start using tags. Tags allow you to easily identify and manage your images. For example, you can create a tag for each environment in which an image is used, or for each customer or client that an image is used for. This makes it easy to find the specific image you need, and to keep track of which images have been used for which purposes.

The syntax for adding a tag to an image is following:

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Your tag can be any valid ASCII string, and can contain lowercase and uppercase letters, digits, underscores, periods and dashes. It cannot start with a period or a dash, and must not exceed 128 characters in length. Let's take a look at an example.

1. In order to add a tag to an image, we first need to know the name or the ID of the image we want to tag. We can list all of the available images on our system by running the following command:

docker image ls

2. Now run the below command to add the "myubuntu" tag to the image.

docker tag ubuntu:latest ubuntu:myubuntu

3. Now if we rerun the docker image ls command, we can see that our image has been tagged with "myubuntu".

working with tags

Note that the tags are just aliases. Meaning an image can have multiple tags but they all refer back to the same source image. So you can use whatever tag you want, as long as it makes sense to you. And it's a good idea to add tags when you create images so that you can easily keep track of them later.

4. Let's take a look at one more example. Suppose we want to create an image of Ubuntu with the tag "original". We can do this by running the following command:

docker tag myubuntu:latest myubuntu:original

The command above will create an image tagged with "original" that is based on the image tagged with "latest". We now have two myubuntu images: one tagged with latest and one tagged with original, but they point back to the same source image ID(2b4cba85892a).

working with tags

Working with Docker Registries

Now that we have a basic understanding of what Docker images are, let's take a look at working with registries. Docker Hub is the most popular public registry, but there are other options available.

Docker Hub is a public registry that is free to use for both personal and commercial purposes. It has a wide variety of images available, and you can also create your own images and share them with others. To use Docker Hub, you simply need to create an account and then install the docker client. You can then use the docker search command to find images, and the docker pull command to download them.

So far, we've been working with the ubuntu image that is available on Docker Hub, a Docker public registry. However, you can also create a private registry to store your images.

Deploying a Private Docker Registry

There are a few reasons why you might want to use a private registry:

  • To store images that are only for internal use. Maybe you have images that contain sensitive information and you don't want them to be publicly available. Or maybe you want to keep track of which images have been used in your environment and don't want others to be able to download them without your permission.
  • To speed up downloads of images in your distribution pipeline CI/CD by caching them locally. Running your own registry and storing it on your own is an excellent way to link it with and improve your CI/CD system.

1. Check if your docker host is ready to create a local registry.

docker version

2. Run the docker run command below to have a local and private registry up and running. Where:

  • -p 5000:5000 = map port 5000 on the docker host to port 5000 on the container
  • --restart=always = ensures that the container is always restarted if it fails
  • --name registry = name of your private registry
  • registry:2 = the image that will be used to create the registry container
docker run -d -p 5000:5000 --restart=always --name registry registry:2

a local and private registry

3. Run the below command to list all the running containers on your docker host.

docker ps 

You will see the following output. This output indicates that the registry container is up and running on your docker host. The :::5000->5000/tcp notation is a shortcut that maps the port on the docker host to the port on the registry container.

list all the running containers on your docker host.

Now that we have our private registry up and running, let's take a look at how to add images to it. The following example shows how to add the ubuntu image to the registry. First, you will need to pull the image from Docker Hub to your local host and give it a specific tag. You will then push the newly tagged image to your private registry. Finally, delete the ubuntu image from your local host and pull the image from your private registry to test it out.

4. Run the below command to download the ubuntu image with the tag 20.04 from Docker Hub to your local host.

 docker image pull ubuntu:20.04

 download the ubuntu image with the tag 20.04

5. Next, run the below command to tag the downloaded Ubuntu image as "my-ubuntu." You can use any tag you want, but it's a good idea to use something that makes sense for your organization.

docker tag u