debian, docker, linux

Creating a minimal Debian container for Docker

In the last post, we introduced some basic techniques to free up unused space on a Debian system. Following those steps, I created a Debian 8 Docker image that takes only 56.7 MB!

Usage

You can get it typing the following, but you really don’t need to because docker run  pulls the image for you if you do not already have it. It is still useful to get updates.

Bash into it with

Run any command inside the container, for instance list root with

Is this small?

In order to see how small this really is, we can compare it to a minimal system built using debootstrap. More details on this below.

Docker images can be made very small, because there are parts of the system that are not needed in order to launch things in a container. If we take a look at the official Debian Docker repository, we can see that their images are smaller than the file system generated by bootstrap. They even have slim versions that get rid of locales and man pages, but they are still bigger than ownyourbits/minidebian.

Is this useful for anything?

It is! docker containers are quite handy to use and allow us to play around with a Debian system easily. Sure, we always could do this with a virtual machine or with bootstrap, but there are benefits to using docker.

One benefit lays in the fact that Docker uses overlayfs, so any changes made to your container will be lost when you exit, unless you issue docker commit. We can play around, we can experiment, break things without fear, and then throw it away.

Another benefit is that we can use it to build more complex systems, overlaying a database, Java runtime, or a web server on top of it. That means that if an Apache server adds a 140 MB layout, you only have to get that compressed overlay, which is quite fast and space efficient.

It is also convenient to distribute stuff with dependencies. Everything is packed for you and you do not have to deal with configuration. This makes trying things out easy. Want to get a feel of gentoo? docker pull gentoo/stage3-amd64 will save you tons of compilation and configuration time.

Finally, we can share this easily on dockerhub.io or our private docker repo.

Details

In order to get a working Debian system that we can then trim down, we have different options.

One of them is working on a live ISO, another is starting from the official Debian Docker repo that we mentioned earlier.

Another one is using good old debootstrap. Debootstrap is a little tool that gets the base debs from the official repositories, then installs them in a directory, so you can chroot to it. It provides the basic directory structure for Debian.

We can see what packages Debian considers essential

This is what debootstrap considers a base filesystem. We already see things that will not be needed in a container. Let’s create the filesystem.

We can then chroot to it manually. Some preparations need to be done to interface the new userspace with the virtual filesystems it expects.

That is already more cumbersome than using Docker. Docker also offers more advanced isolation using newer kernel features like namespaces and cgroups.

It is easier to import this filesystem as a Docker image.

Now we can start freeing up space. The problem with this is that, because Docker uses overlays, you will not get a smaller container even if you delete things. This happens because when you delete in an upper layer it is just marked as deleted so that you can go back to the original contents just by getting rid of the upper layer.

In order to get around this, we can repack everything in an unique layer with

When we are happy with the result, we end up with a Docker image with no metadata. We are only left with creating a basic dockerfile in an empty directory

, and building the final image

In this example, we have only indicated Docker to spawn bash  if no other arguments are given.

In the next post we will create a LAMP installation on top of this small debian layer.

Author: nachoparker

Humbly sharing things that I find useful [ github ]

Leave a Reply

Your email address will not be published. Required fields are marked *