Sub topic 2
glibc, musl, and the userland underneath your app
This chapter assumes you can build and run images, read a simple Dockerfile, and use basic Linux commands. You should also know that a shared library is code loaded by a program at run time.
Your app does not talk to the kernel alone
Most programs do not call the Linux kernel directly. They call the C library (libc), which wraps system calls for files, sockets, memory, DNS lookups, and threads. The kernel comes from the host. The libc comes from your base image.
Two libc implementations matter in this tutorial:
- glibc is the GNU C library. Debian, Ubuntu, and Red Hat based images use it.
- musl is a smaller, simpler C library. Alpine uses it.
A binary compiled against one libc generally cannot run on the other. It expects a specific dynamic loader and specific library files.
See the difference
These commands look for each libc file inside the two images:
docker run --rm debian:12-slim sh -c 'ls /lib/*-linux-gnu/libc.so.6'
docker run --rm alpine:3.20 sh -c 'ls /lib/ld-musl-*'The first prints a glibc library path. The second prints the musl loader. Each file is missing from the other image.
Where the libc difference bites
- Prebuilt binaries. Python wheels tagged
manylinuxtarget glibc. Musl systems needmusllinuxwheels, and not every package publishes them. When none exists, pip builds from source, which needs a compiler and headers that Alpine does not include by default. - Native addons. Node packages with compiled parts ship binaries per libc. Some only provide glibc builds.
- Closed-source binaries. Vendor agents, database drivers, and JVM native libraries are often built only for glibc.
- Behavior differences. musl's DNS resolver and locale handling differ from glibc's. Chapter "Where minimal images break" covers these.
Statically linked programs avoid the problem. A Go binary built with CGO_ENABLED=0 contains everything it needs and runs on either libc, or on an image with none.
The userland is part of the base image too
The userland is the set of commands and tools in the image: the shell, core utilities, and package manager. Debian slim ships GNU coreutils, bash, and apt. Alpine ships BusyBox, which provides small versions of common commands in one binary, plus ash and apk.
| Property | Debian slim | Alpine |
|---|---|---|
| C library | glibc | musl |
| Default shell | `dash` as `/bin/sh`, `bash` also present | BusyBox `ash`, no `bash` |
| Core utilities | GNU coreutils | BusyBox applets |
| Package manager | `apt` | `apk` |
Scripts written for bash often fail on Alpine:
docker run --rm alpine:3.20 bash -c 'echo hello'Docker reports that bash is not found in $PATH. A script with #!/bin/bash on its first line fails with a confusing no such file or directory error, because the interpreter path does not exist. You have two fixes: install bash, or make the script POSIX sh compatible.
FROM alpine:3.20
RUN apk add --no-cache bash
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]Command flags can differ as well. BusyBox sed, grep, find, and ps support fewer options than the GNU versions, so a build script that works on Debian may break on Alpine.
A quick check before you switch
- Find out which libc your dependencies were built for.
- Search your scripts for
bash-only syntax and GNU-only flags. - Run your test suite on the candidate image, not only a smoke test.
Exercise
Your Python service builds fine on python:3.12-slim. On python:3.12-alpine, pip install takes several minutes and fails with a missing compiler error. What is the most likely cause, and what are two ways to respond?
Show answer
A dependency has a glibc wheel but no musllinux wheel, so pip tries to compile it from source, and Alpine has no compiler or headers by default. You can stay on Debian slim, which uses glibc, or install build tools with apk in a build stage. The first is usually simpler.