michelangelus - Fotolia

Use Docker and Alpine Linux to build lightweight containers

Alpine Linux won't always be the right OS choice for IT teams, but it's an option worth consideration when lightweight container images are the goal.

When it comes to Docker, sometimes less is more -- a maxim that applies especially to the base OS images installed in each Docker image.

The use of a lightweight image -- one with less than 200 MB -- can result in significant resource and cost savings when used alongside optimized applications. A lightweight image also takes less time to deploy compared to a larger one, as it boots up faster.

Most OS images are lightweight, with minimal compute resource requirements. But others, such as Windows containers, are huge. Alpine Linux is a super lightweight Linux distribution that's useful for Docker containers.

In this Docker and Alpine Linux tutorial, we'll build an Nginx web server that demonstrates how small a Docker container image can be.

Author's note: The container image we build in this Docker and Alpine Linux tutorial is not production-ready.

Alpine Linux as an OS option

As Figure 1 demonstrates, a container image can be less than 6 MB with an Alpine Linux OS -- far more lightweight than one with an Ubuntu OS.

Docker images with Ubuntu and Alpine Linux, size comparison.
Figure 1. This list compares Alpine Linux versus Ubuntu Docker image sizes.

Alpine Linux is extremely bare bones, as it doesn't come with the niceties most base images include, such as a GUI or systemd. This means admins, however, can't simply swap out their current OS configuration, such as Ubuntu, for Alpine Linux.

How to work with Alpine Linux

There is an official Alpine Linux image available, along with various tags for various release versions. By default, there is only a root account.

Alpine Linux comes with BusyBox, a suite of Unix utilities. To run a base Alpine Linux image, use the command docker run with flags to initialize and tag for Alpine. Specify /bin/sh to run a BusyBox shell:

docker run -i -t alpine /bin/sh

The command sudo docker run -i -t alpine /bin/sh
Figure 2. This command runs a rudimentary Alpine Linux-based Docker container.

For the most part, default setup with BusyBox is straightforward. However, if you want to run the Unix shell Bash, use apk, the package manager for Alpine. To add Bash to the Dockerfile, use apk add bash. The command apk add is how to add packages.

Tell the container to run Bash with a similar command setup -- this time with /bin/bash rather than bin/sh:

sudo docker run -i -t alpine /bin/bash

To remove packages, use apk del <package> name. To locate a package, perform a simple apk search. More details on packages and configurations can be found here.

There are many web servers available to run with Alpine Linux in a Docker container, but the smallest, lightest and arguably quickest is Nginx. To install Nginx with apk use apk add nginx.

With apk, we have all the tools to build a Dockerfile for the container image. The super light Dockerfile below shows a basic setup, with files copied, as needed. For the sake of simplicity, we use several RUN commands, rather than merge them, and create a smaller image.

FROM alpine:latest
LABEL maintainer="[email protected]"
RUN apk add nginx
RUN mkdir -p /run/nginx
RUN touch /run/nginx/nginx.pid
RUN adduser -D -g 'www' www
RUN mkdir /www 
RUN chown -R www:www /var/lib/nginx
RUN chown -R www:www /www
COPY nginx.conf /etc/nginx/nginx.conf
COPY index.html /www
RUN ["./usr/sbin/nginx"]

This Dockerfile is enough to start the web server. It won't, however, show much, as we have excluded the Nginx configuration.

Alpine Linux does have a service management system, OpenRC, as an optional extra, but it is not necessary in Docker images. Instead, use the Nginx binary files to run OpenRC via the command line, as it only has one job.

Go beyond the basics

At this point in the Docker and Alpine Linux tutorial, the container will run -- but, again, it is not production-ready; we have simply created a functioning Alpine Linux image with a base Nginx server that will display the base webpage. You can use the Dockerfile to create a more tailored, bespoke image for testing and deployment.

Depending on how you want to roll your image, you could use the copy command to copy HTML source files into the image. The files can stand alone without a central set of files -- such as HTML or JavaScript -- that are network-mounted. While more resilient in this configuration, the container image does have to be recomposed with every source code change.

As mentioned above, IT teams can't swap a base image from Ubuntu or another distribution to Alpine Linux and expect it to work interchangeably. But if teams want super lightweight containers, learning how to build a Dockerfile and executable container with Alpine Linux might be worth the investment.

Dig Deeper on Containers and virtualization