Using Alpine Linux With Docker

Not all operating systems were created equal. Some are larger and more powerful. Some are smaller and more flexible. Which one is right for you? The answer, of course, is “it depends on what you are doing”.

If you are using containers in Docker you probably have some kind of work that you want the container to accomplish. You probably don’t need support for multiple development platforms, printing, scheduling or complex network set-ups. You just need the operating system to support your task and no more.

This gives you the opportunity to choose a smaller operating system. This will reduce the size of the Docker image making it cheaper to store images, faster to transfer and load images and reduce the demands on the host meaning more work can be done with the same hardware.

Linux distributions match up the Linux kernel with a set a utilities to create full operating system. The set of utilities chosen and the means of controlling them (the package manager) are what make the difference between distributions. The full-service Linux distributions with commercial support include Ububtu, RedHat and SUSE. These all offer operating systems for use in demanding high-end services. The choice of utilities in each distribution are focused on power and supportability. There are other distributions such as Alpine (https://www.alpinelinux.org/) which aim to offer something more “lightweight” but still flexible and useful.

The base Docker image for Ubuntu is 77MB. The base Docker image for Alpine is 5MB.

You might have seen in our other guide on how to create a Ubuntu-based Docker image that would run an Apache2 web server. Its size was 211MB.

Lets try and do that with Alpine and see how large the Docker image is to do the same thing.

[Spoiler: its under 12 MB]

Install Docker

First of all you have to have docker installed. You can find the instructions to install docker on different platforms here.

We installed the docker community edition on an Ubuntu host, instructions here

The Alpine Linux Image

Obtain the Alpine base image from the Docker Hub.

docker search alpine
docker pull alpine
docker image list
Command line output showing a list of Alpine-based images in the Docker Hub, and downloading an image to the local store

Lets have a look inside the Alpine Distribution. To create a container based on the Alpine base image and create a shell in interactive mode you can type:

docker run -it –rm alpine sh

This will open a shell which lets you execute commands inside the distribution.

ls
ls /bin
ls -l /bin/sh
ls -l /bin/netstat
ls -l /bin/busybox
command line output of executing a shell within a running Alpine container.

The file system and contents of the /bin directory is typical “Unix like” but all the commands redirect to the BusyBox executable. BusyBox was specifically created to make standard utilities available in small systems with low resources (note its less than 1MB in size).

Create The Web Page And Prepare The dockerfile

We created an build directory, ~/dockAlpWeb, and created a simple index.html page file to be shown by the web server (for simplicity we’ve not bothered with html).

Message from within the Alpine-based Docker Container

Now in the same directory we created the dockerfile (the script that tells Docker how to create our user-built image)

FROM alpine:latest

RUN apk add apache2

ADD index.html /var/www/localhost/htdocs/index.html

VOLUME ["/var/log/apache2"]

EXPOSE 80

CMD ["/usr/sbin/httpd", "-d", "/var/www", "-f", "/etc/apache2/httpd.conf", "-k", "start", "-DFOREGROUND"]

Note that Alpine Linux uses the APK package manager (alpine package keeper) and the Apache2 package operates differently to that published by Ubuntu.

We build the image

docker build -t alp_web_image ~/dockAlpWeb
docker image list
Command line output of building a Alpine-based image within Docker

Note that the Alpine-based image is under 12MB compared to the 211MB of the Ubuntu-based image.

Run A Container Based on the Image

docker run -d -p 90:80 -v /home/user1/weblog:/var/log/apache2 alp_web_image
docker container list
command line output of running an Apache2 web server within an Alpine based container.

We can see the web page from within the Alpine-based container mapped onto port 90 of the host.

Screenshot of a browser on the host reading a file from the web server in the Alpine container

We can also see the volume mount of the log directory.

command line output showing the contents of the log within the container from outside

Summary

If your workload is suitable, choosing a lightweight distribution for Docker containers can significantly increase performance and reduce costs of operation.

Alpine Linux is however not commercially supported and its security profile is unknown meaning it is not be suitable every task.