Why Alpine Linux?

Alpine Linux is a very lightweight distro. If you’re using it with Docker, the base container is under 5MB. If you compare that to many of the other popular distros, it’s the only one under 100MB:

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              97434d46f197        12 days ago         188 MB
fedora              latest              ddd5c9c1d0f2        3 weeks ago         204.7 MB
centos              latest              d0e7f81ca65c        3 weeks ago         196.6 MB
alpine              3.3                 70c557e50ed6        4 weeks ago         4.798 MB
debian              latest              f50f9524513f        4 weeks ago         125.1 MB

If you’re looking to shrink the size of your images. Alpine might be the right choice for you.

Why should I care about smaller containers?

If you’re running a single container locally, it’s not a huge deal if it’s large. It may take a while to download, but once you have it locally, you’re fine. As you attempt to scale your container, that time that it takes to download effect how long it takes for your application to be deployed to new nodes. If you’re dealing with even larger deployments, the larger the container, the more bandwidth and stress you’ll be putting on your registries. Depending on where your registries are hosted, this could translate into significant bandwidth charges, and significantly higher storage requirements.

More hardcore users will oftentimes start with a completely empty base container and the only build and compile in what they need to make the smallest container possible.

No matter what your strategy, always look for ways to make your containers smaller.

The package manager

As a disclaimer, you may find a lot of gaps in packages and package versions when working with. Your package manager will be apk. Don’t get too nervous, it’s very similar to apt, yum, and a lot of other tools you use.

Disable cache

If you want to install something without caching things locally, which is recommended for keeping your containers small, include the --no-cache flag. Example:

apk add --no-cache curl

This is a small gain, it keeps you from having a the common rm -rf /var/cache/apk/* at the end of your Dockerfile.

Referencing repositories

This is a lesson personally learned. There is a lot of documentation around how to use apk that’s provided by Alpine Linux. Many reference https://dl-3.alpinelinux.org, but what I’ve found is that specific mirror goes down from time to time. I’ve had much greater success pointing to https://dl-cdn.alpinelinux.org. I’m yet to have a build error related to a mirror being down from pointing to dl-cdn.

build-essential equivalent

Ubuntu users are very familiar with build-essential. It contains pretty much all of the applications you need to to compile applications from source (make, gcc, etc). The equivalent package with in Alpine is build-base:

apk add --no-cache build-base

As of Alpine Linux 3.3, this will add 19 packages, and over 100MB to your container, so you’ll definitely want to clean up after you’ve done your compilation.

Step 5 : RUN apk add --no-cache build-base
 ---> Running in 35d6cf7f7a5b
fetch https://dl-cdn.alpinelinux.org/alpine/v3.3/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.3/community/x86_64/APKINDEX.tar.gz
(1/19) Installing binutils-libs (2.25.1-r0)
(2/19) Installing binutils (2.25.1-r0)
(3/19) Installing gmp (6.1.0-r0)
(4/19) Installing isl (0.14.1-r0)
(5/19) Installing libgomp (5.3.0-r0)
(6/19) Installing libatomic (5.3.0-r0)
(7/19) Installing libgcc (5.3.0-r0)
(8/19) Installing pkgconf (0.9.12-r0)
(9/19) Installing pkgconfig (0.25-r1)
(10/19) Installing mpfr3 (3.1.2-r0)
(11/19) Installing mpc1 (1.0.3-r0)
(12/19) Installing libstdc++ (5.3.0-r0)
(13/19) Installing gcc (5.3.0-r0)
(14/19) Installing make (4.1-r0)
(15/19) Installing musl-dev (1.1.12-r3)
(16/19) Installing libc-dev (0.7-r0)
(17/19) Installing fortify-headers (0.7-r0)
(18/19) Installing g++ (5.3.0-r0)
(19/19) Installing build-base (0.4-r1)
Executing busybox-1.24.1-r7.trigger
OK: 166 MiB in 39 packages
 ---> 375d9b5ead00

Installing Docker inside of Alpine

The day will come when you’ll want to install Docker inside of Alpine Linux, maybe even inside a container. If you’re like me and you’re looking for the latest version of Docker, you’ll need to reference another repository. To save you some time, here’s your command:

apk add --update --repository https://dl-cdn.alpinelinux.org/alpine/edge/community/ tini docker

Similar to other distros, the packaged version of Docker is recent, but it’s not the latest and greatest.

Wrapping Up

Hopefully some of these tips help save you some time in your experiments with Alpine Linux. If you have any other interesting tips and tricks, throw them in the comments section.