BudiBadu Logo
Samplebadu

Dockerfile by Example: Alpine Image Usage

Docker Engine 20.10+

Alpine Linux is a popular choice for base images due to its tiny size. This sample code highlights the specific package manager (apk) and compatibility considerations.

Code

FROM alpine:3.18

# 1. Install packages with apk
# --no-cache avoids storing the index locally
RUN apk add --no-cache \
    curl \
    bash \
    python3

# 2. Create a user (syntax differs slightly from Debian)
RUN addgroup -S myapp && adduser -S myapp -G myapp

USER myapp
CMD ["python3", "--version"]

Explanation

Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox. Its primary advantage is its size; an Alpine base image is often under 5MB, compared to 100MB+ for Ubuntu. This makes it ideal for containers where storage and network bandwidth are concerns. However, because it uses musl libc instead of the standard glibc, some compiled binaries (like certain Python wheels or C++ apps) may not work without recompilation.

Key differences in Alpine:

  • Uses apk instead of apt-get for package management
  • Uses musl libc which may cause compatibility issues with glibc binaries
  • Default shell is sh (ash), not bash (unless installed)
  • User creation commands (adduser) have different flags

When using Alpine, you should always use apk add --no-cache to keep the image small. If you encounter "symbol not found" errors with pre-compiled binaries, it is likely a glibc vs. musl compatibility issue. In such cases, you can either compile the software from source on Alpine or switch to a "slim" Debian-based image.

Code Breakdown

5
apk add --no-cache installs packages without bloating the image.
11
addgroup -S ... creates a system group in Alpine syntax.
1
FROM alpine:3.18 selects the specific Alpine version.
13
USER myapp applies the non-root user for security.