Building from scratch

So far, we have been using prepared images from the Docker Hub as our base image; it is possible to avoid this altogether (sort of) and roll your own image from scratch.

Now, when you usually hear the term scratch, it literally means that you start from nothing. That's what we have here—you get absolutely nothing and have to build upon it. Now, this can be a benefit because it will keep the image size very small; but it can also be detrimental if you are fairly new to Docker, as it can get complicated.

Docker has done some of the hard work for us already and created an empty TAR file on the Docker Hub named scratch; you can use it in the FROM section of your Dockerfile. You can base your entire Docker build on this then and add parts as needed.

Again, let's look at using Alpine Linux as our base operating system for the image. One of the reasons why we would want to do this is that it is not only distributed as an ISO, Docker image, and various virtual machine images, but the entire operating system is available as a TAR file; you can find the download in the repository or on the Alpine Linux download page. Just select the appropriate download--the one I used was x86_64--from the MINI ROOT FILESYSTEM at the following URL: https://www.alpinelinux.org/downloads/.

Once it's downloaded, you need to create a Dockerfile that uses scratch and then add the TAR file, like this, for example:

FROM scratch
ADD files/alpine-minirootfs-3.6.1-x86_64.tar /
CMD ["/bin/sh"]

Now that you have your Dockerfile and operating system in a TAR file, you can build your image as you would any other Docker image by running the following:

$ docker image build --tag local:fromscratch .

Once it's built, you can test the image by running this command:

$ docker container run -it --name alpine-test local:fromscratch /bin/sh

This should launch into a shell on the Alpine Linux image; you can check this by running:

$ cat /etc/*release

This will display information on the release the container is running. To get an idea of what this entire process looks like, see the following Terminal output:

Now everything seems straightforward, and thanks to the way Alpine Linux packages their operating system, it is. However, not everything is that straightforward.

There are several tools that can be used to generate a bundle of an operating system:

We are not going to go into any detail on how to use any of these scripts here because if you have to consider this approach, then you probably have some pretty specific requirements.

So what could those requirements be? For most people, it will be legacy applications; for example, what happens if you have an application that requires an operating system that is no longer supported or available from Docker Hub, but you need a more modern platform to support the application on? Well, you should be able to spin your image and install the application there, allowing you host on your old legacy application on a modern, supportable operating system/architecture.

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

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