Sub topic 5
Alpine: small, musl-based, apk-managed
This chapter assumes you can write a basic Dockerfile, know what a shared library is, and have read the earlier chapter on glibc and musl. Here you will see what Alpine contains, how to install packages on it, and where it causes problems.
What Alpine is
Alpine Linux is a small distribution. Its base image is only a few megabytes, compared with tens of megabytes for Debian slim. Three choices make it small:
- musl libc replaces glibc as the C library.
- BusyBox supplies the shell and core commands in one small binary.
- apk is the package manager, and its packages are built to stay compact.
Unlike distroless images, Alpine still has a shell (/bin/sh, which is BusyBox ash) and a package manager. You can open a shell in the container and install tools. That makes it easier to debug than distroless, with a smaller footprint than Debian slim.
Installing packages with apk
Use apk add --no-cache so apk does not keep its package index in the layer. This is the same idea as cleaning apt lists on Debian, in one flag.
FROM node:22-alpine
WORKDIR /app
RUN apk add --no-cache tini
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
USER node
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "server.js"]This image installs tini, a small init process that forwards signals and reaps zombie processes. The official Node image provides the non-root node user, so USER node needs no extra setup. Alpine tags for official images follow the pattern <version>-alpine.
Where musl changes behavior
Most differences come from musl being a different C library, not from Alpine itself.
- Prebuilt wheels. Standard Python
manylinuxwheels target glibc and do not install on Alpine. Many popular packages now publishmusllinuxwheels. Packages that do not will compile from source, which needs a compiler and headers. - Native Node addons. Addons that ship glibc-only prebuilt binaries fail to load or fall back to a source build with
node-gyp. - DNS. musl's resolver differs from glibc's in how it handles search domains and TCP fallback. Older Alpine releases showed odd lookup failures in Kubernetes clusters. Recent releases are better, but test name resolution in your real environment.
- Locales and tools. musl has limited locale support. BusyBox commands accept fewer options than GNU ones, so a script that uses
sed -iorgrep -Pin a GNU-specific way can fail.bashis not installed by default. - Performance. Some workloads run slower on musl, for example heavy use of its default memory allocator under threads. Measure before assuming.
When a package must be compiled, install the toolchain as a temporary virtual package and remove it in the same layer:
FROM python:3.12-alpine
WORKDIR /app
COPY requirements.txt .
RUN apk add --no-cache --virtual .build-deps gcc musl-dev libffi-dev \
&& pip install --no-cache-dir -r requirements.txt \
&& apk del .build-deps
COPY . .
CMD ["python", "app.py"]The compiled extension may need runtime libraries that you must keep with a separate apk add. Also, a source build can take much longer than a wheel install, and the time saved on image size is lost in CI.
When Alpine fits
Alpine works well for Go or Rust tools, Node services with pure JavaScript dependencies, and small utility images that need a shell and a few packages. It fits poorly when you depend on glibc-only binaries, such as vendor-supplied agents, or on numerical Python libraries without musllinux wheels. In those cases Debian slim is usually the safer choice.
Exercise
A Python service builds fine on python:3.12-slim but fails on python:3.12-alpine with a long compiler error while installing one dependency. Give the likely cause and two ways to fix it.
Show answer
The dependency has no musllinux wheel, so pip tries to compile it from source and Alpine lacks gcc and headers. Fix it by installing a build toolchain with apk (for example gcc and musl-dev in a --virtual group, removed afterward), or by switching to python:3.12-slim, where the glibc manylinux wheel installs without a compiler.