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 ubuntu:20.04 localhost:5000/my-ubuntu 

6. Run the below command to push the newly tagged image to your private registry running at port localhost:5000.

docker push localhost:5000/my-ubuntu

push the newly tagged image to your private registry

7. Run the below command to delete the locally-cached ubuntu images from your localhost to test out your private registry.

docker image remove ubuntu:20.04 && docker image remove localhost:5000/my-ubuntu

 delete the locally-cached ubuntu image

8. Once the locally-cached ubuntu images have been removed, you can run the below command to pull the image from your private registry.

docker pull localhost:5000/my-ubuntu

9. Run the below command to list all the images on your docker host.

docker image ls

You will see the my-ubuntu image listed as being pulled from your private registry, as shown below. This output confirms that the image was successfully pulled from your private registry and your private registry is working correctly.

 list all the images on your docker host

Pushing, Pulling, and Signing Images

Docker offers a few command-line options of pushing, pulling, and searching for images. In this section, you will learn how to use those commands to manage your images in registries from your command line on your host. This example uses the Docker Hub to demonstrate, but you could use any other registries.

1. Run the docker login command to log in to the Docker Hub registry. Provide your username and password when prompted. If you do not have a Docker Hub account, you can create one for free at https://hub.docker.com/.

docker login

log in to the Docker Hub registry

2. Once logged in, you are ready to push your images. Suppose you want to push the ubuntu latest image to the Docker Hub. You should also tag the image with a meaningful name so you can easily reference it in the future. In this example, you will tag the image as howtoforge/ubuntu:latest. Feel free to use your desired tag name.

docker tag ubuntu:latest howtoforge/ubuntu:latest
docker push howtoforge/ubuntu:latest

push your ubuntu image

3. Open your web browser and navigate to the Docker Hub. You will see your image there. This repository was created based on your username and the tag you used. In this example, the repository is howtoforge/ubuntu. Navigate to the tag tab you will see your tag was successfully applied(latest).

Open your web browser

viewing the tag

4. Run the below command to remove the local ubuntu image to test the pull.

docker image rm howtoforge/ubuntu

List the images on your system to verify that the ubuntu image was removed.

docker image ls

remove the local ubuntu image

5. Run the below command to download the my-ubuntu image from your Docker Hub.

docker pull howtoforge/ubuntu

download the latest ubuntu image from your Docker Hub.

6. List the images on your host again to verify that the ubuntu image was pulled. You should see the my-ubuntu image on your system.

docker image ls

docker image ls

While you are working with images, you may want to search for specific images on Docker registries. You can use the docker search command to search for images on Docker registries.

7. Run the docker search command to see all the options and parameters available.

man docker search

You should see the following output. You can see helpful filtering option like:

  • stars=: filter the search results by the number of stars the image has on Docker Hub. You can use this to find the most popular images.
  • is-automated=(true|false): filter the search results to include or exclude automated builds. You can use this to find images that are or are not automated builds.
  • is-official=(true|false): filter the search results to include or exclude official images. You can use this to find images that are official supported by the company behind the image. For example, Ubuntu is the official company behind the Ubuntu image. Nginx is the official company behind the Nginx image. You should always use official images where possible as they have been tested by the company and are more likely to be stable. When you encounter problems with an official image, you can get help from the company that provides the image.

manpage docker search

8. For example, run the docker search --filter=stars=4 ubuntu command to search for the ubuntu image with 4 or more stars on Docker Hub.

docker search --filter=stars=4 ubuntu

search for the ubuntu image with 4 or more stars on Docker Hub.

9. Run the below command to search for the ubuntu image that is an official image from Ubuntu.

docker search --filter=is-official=true ubuntu

The output should be a list of repositories that contain the ubuntu image and indicates that it is an official Ubuntu image(OK), as shown below.

 search for the ubuntu image that is an official image from Ubuntu.

10. Run the below command to search for the ubuntu image that has at least 100 stars rating on Docker Hub and is an official Ubuntu image.

docker search --filter="stars=100" --filter=is-official=true ubuntu

search for the ubuntu image that has at least 100 stars rating on Docker Hub

Docker images are a great way to store your application code and configuration. But at the same time, it's easy to accumulate a lot of unsused images, particularly if you are testing out different applications or trying different configurations. You may find that you no longer need some of the images on your registries.

You can delete an image by navigating to the repository that contains the image and clicking on the delete button.

11. For example, on Docker Hub, navigate to the howtoforge/ubuntu repository. Click on Setting > Delete Repository. Confirm that you want to delete the repository by typing in the name of the repository. Click on Delete.

delete an image

delete an image

Navigate back to the dashboard and you will see that the howtoforge/ubuntu repository is now deleted along with the ubuntu image.

delete an image

Conclusion

Congratulations! You have now reached the end of the article. You should now have a basic understanding of Docker images and how to work with them. If you want to learn more about Docker, we suggest reading the official Docker documentation where you can find more information about topics such as:

  • Dockerfiles
  • Networking
  • Volumes
  • Services
  • Swarm mode
Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

By: Milly at: 2022-03-17 13:41:14

Really thank you very much, I need to start learning Docker and this could be the start.