Creating A Custom Base Image

Docker has a rich library of base images, and we strongly recommend that you select a lean image that fits your application well. However, you may choose to custom-build your base image from the get go. Here, in this recipe, we will use debootstrap to create our Ubuntu 18.04 LTS (Xenial Xerus) base image. The debootstrap utility can create any Debian-based system by downloading it from the appropriate repository.

Getting ready

Install debootstrap on any Debian-based system using the following command:

$ apt-get install debootstrap

How to do it…

Perform the following steps:

  1. Create the directory on which you want to populate all the distribution files:
$ mkdir xenial
  1. Now, using debootstrap , install Xenial Xerus inside the directory we created earlier:
$ sudo debootstrap xenial ./xenial

You will see the directory tree, similar to any Linux root filesystem, inside the directory in which Xenial Xerus is installed:

$ ls ./xenial
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc
root  run  sbin  srv  sys  tmp  usr  var
  1. Now, we can export the directory as a Docker image with following command:
$ sudo tar -C xenial/ -c . | docker image import - xenial
  1. Look at the docker image ls output. You should have a new image with xenial as the name.

How it works…

The debootstrap command pulls all the Ubuntu 18.04 (Xenial Xerus) packages to the directory from the package repository. Then they are bundled as a TAR file and pushed to the Docker image import command to create the Docker image.

See also