2 Ways to Create Your Own Docker Base Image

Greetings to everyone, today we'll learn about docker base Images and how we can build our own. Docker is an Open Source project that provides an open platform to pack, ship and run any application as a lightweight container. It has no boundaries of Language support, Frameworks or packaging system and can be run anywhere, anytime from a small home computers to high-end servers. It makes them great building blocks for deploying and scaling web apps, databases, and back-end services without depending on a particular stack or provider.

Docker Images is a read-only layer which never changes. It Docker uses a Union File System to add a read-write file system over the read-only file system. But all the changes go to the top-most writeable layer, and underneath, the original file in the read-only image is unchanged. Since images don't change, images do not have state.  Base Images are those images that has no parent. The major benefits of it is that it allows us to have a separate linux OS running.

Here are the ways on how we can create a custom base image.

1. Creating Docker Base Image using Tar

We can create our own base image using tar, we'll want to start building it with a working Linux Distribution we'll want to package as base image. This process may differ and depends on what distribution we are trying to build. In Debian distribution of Linux, debootstrap is preinstalled. We'll need to install debootstrap before starting the below process. Debootstrap is used to fetch the required packages to build the base system. Here, we'll create image based on Ubuntu 14.04 "Trusty".  To do so, we'll need to run the following command in a terminal or shell.

$ sudo debootstrap trusty trusty > /dev/null
$ sudo tar -C trusty -c . | sudo docker import - trusty
creating docker base image using debootstrap

Here, the above command creates a tar file of the current directory and outputs it to STDOUT, where "docker import - trusty" takes it from STDIN and creates a base image called trusty from it. Then, we'll run a test command inside that image as follows.

$  docker run trusty cat /etc/lsb-release

Here are some example scripts that will allow us to build quick base images in Docker GitHub Repo .

 2. Creating Base Image using Scratch

In the Docker registry, there is a special repository known as Scratch, which was created using an empty tar file:

$ tar cv --files-from /dev/null | docker import - scratch

creating docker base image using scratch

We can use that image to base our new minimal containers FROM:

FROM scratch
ADD script.sh /usr/local/bin/run.sh
CMD ["/usr/local/bin/run.sh"]

The above Dockerfile  is from an extremely minimal image. Here, first it starts with the possible minimal image ie scratch, then it copies script.sh that is created to /usr/local/bin/run.sh and then run the script /usr/local/bin/run.sh .


Here, in this tutorial, we learned how we can build a custom Docker Base Image out of the box. Building a docker base image is an easy task because there are sets of packages and scripts already available for. Building a docker base image is a lot useful if we want to install what we want in it. So, if you have any questions, suggestions, feedback please write them in the comment box below. Thank you ! Enjoy  :-)

3 Comments... add one

  1. Hi,

    I have just started using Docker and I am trying to learn how to create my first base image. I had a question regarding the second way- Creating base image using Scratch. In the example, you have shown that it starts with a totally blank filesystem, then it finally runs the script /usr/local/bin/run.sh I was wondering since it's a totally blank file system, has not bash or sh binary, how does the script run?

    I am beginner so I am trying to understand how images work. I am still confused if an image contains a file system, a kernel or both?


    • Hi Ms. Priyanka,
      I am glad to know that you are learning Docker and doing things out of the box. Here, what I mean to say is, totally blank file system as a super minimal image ie Scratch which does not contain any bugs, security leaks, slow code or technical debt. It is also considered as the smallest possible image and is basically called as empty. And yes, it contains necessary binaries to execute scripts and operations.

      Thank You for your comment.
      Arun Pyasi


Leave a Comment