Building images from containers

So far, we have crafted a handful of containers using the standard base images busybox and ubuntu. In this section, let's see how we can add more software to our base image on a running container and then convert that container into an image for future use.

Let's take ubuntu:16.04 as our base image, install the wget application, and then convert the running container to an image by performing the following steps:

  1. Launch an ubuntu:16.04 container using the docker run subcommand, as shown here:
      $ sudo docker run -i -t ubuntu:16.04 /bin/bash
  1. Having launched the container, let's quickly verify if wget is available for our image or not. We used the which command with wget as an argument for this purpose and in our case, it returns empty, which essentially means that it could not find any wget installation in this container. This command is run as follows:
      root@472c96295678:/# which wget
root@472c96295678:/#
  1. Now, let's move on to the next step, which involves the wget installation. Since it is a brand new ubuntu container, before installing wget we must synchronize it with the Ubuntu package repository, as shown here:
      root@472c96295678:/# apt-get update
  1. Once the Ubuntu package repository synchronization is over, we can proceed toward installing wget, as shown here:
      root@472c96295678:/# apt-get install -y wget
  1. Having completed the wget installation, let's confirm our installation of wget by invoking the which command with wget as an argument, as shown here:
      root@472c96295678:/# which wget
/usr/bin/wget
root@472c96295678:/#
  1. Installation of any software would alter the base image composition, which we can also trace using the docker diff subcommand introduced in the Tracking changes inside containers section. From a second Terminal/screen, we can issue the docker diff subcommand, as follows:
      $ sudo docker diff 472c96295678

The preceding command would show a few hundred lines of modification to the ubuntu image. This modification includes the update on the package repository, wget binary, and the support files for wget.

  1. Finally, let's move on to the most important step of committing the image. The docker commit subcommand can be performed on a running or a stopped container. When a commit is performed on a running container, the Docker Engine will pause the container during the commit operation in order to avoid any data inconsistency. We strongly recommend that you perform the commit operation on a stopped container. We can commit a container to an image with the docker commit subcommand, as shown here:
      $ sudo docker commit 472c96295678 
learningdocker/ubuntu_wget
sha256:a530f0a0238654fa741813fac39bba2cc14457aee079a7ae1f
e1c64dc7e1ac25

We committed our image using the learningdocker/ubuntu_wget name.

We also saw how to create an image from a container, step by step. Now, let's quickly list the images on our Docker host and see if this newly created image is a part of the image list, using the following command:

$ sudo docker images
REPOSITORY TAG IMAGE ID
CREATED VIRTUAL SIZE

learningdocker/ubuntu_wget latest a530f0a02386
48 seconds ago 221.3 MB
busybox latest e72ac664f4f0
2 days ago 2.433 MB

ubuntu 16.04 6b4e8a7373fe
2 days ago 194.8 MB

From the preceding docker images subcommand output, it is quite evident that our image creation from the container was quite successful.

Now that you have learned how to create an image from containers using a few easy steps, we encourage you to predominantly use this method for testing. The most elegant and the most recommended way of creating an image is to use the Dockerfile method, which will introduce in the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset