Getting Started with Alpine Linux for Docker and Containers

alpine linuxdocker alpineapk package manageralpine linux timezonealpine linux mirrors
Published·Modified·

Alpine Linux is a Linux distribution based on musl and BusyBox, designed specifically for security, simplicity, and resource efficiency. It is extremely lightweight, making it ideal for use in Docker images. Xiaoz recently learned about Alpine Linux while packaging container images for CCAA/Zdir. Despite its small size, it is fully functional and incredibly convenient.

Using Alpine Linux with Docker

You can run Alpine Linux using the command docker run -it alpine /bin/sh. Since Alpine Linux does not have bash built-in, sh is used as the pseudo-terminal. When writing shell scripts for Alpine Linux, be sure to use sh instead of bash.

The Alpine Linux image is very small, under 6MB, making it particularly suitable for container packaging.

Alpine Linux Software Management

Alpine Linux uses the apk command to manage software, similar to yum in CentOS or apt-get in Debian. It is recommended to run apk update upon first use to update the software list and avoid potential issues. Common apk commands include:

# Update software
apk update
# Search for a specific software
apk search xxx
# Install software
apk add xxx
# Uninstall software
apk del xxx
# View help
apk -h

Setting the Alpine Linux Timezone

Alpine Linux is not set to the UTC+8 timezone by default. For projects that need to synchronize with Beijing Time, you must modify the default timezone. The steps are as follows:

# Install timezone data
apk add -U tzdata
# View the list of timezones
ls /usr/share/zoneinfo
# Copy the required timezone file to localtime
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# Check the current time
date
# To keep the image lean, you can remove tzdata
apk del tzdata

Modifying Alpine Linux Software Sources

If you are using Alpine Linux within China, using domestic mirror sources can significantly improve download speeds. Common domestic mirror sources include:

The software source configuration file is located at /etc/apk/repositories. The default content looks like this:

http://dl-cdn.alpinelinux.org/alpine/v3.11/main
http://dl-cdn.alpinelinux.org/alpine/v3.11/community

As you can see, the version used here is v3.11. When modifying the sources, ensure the version remains consistent. For example, to switch to the Aliyun mirror:

http://mirrors.aliyun.com/alpine/v3.11/main
http://mirrors.aliyun.com/alpine/v3.11/community

For more software sources, refer to the official list: https://mirrors.alpinelinux.org/

Summary

  • Alpine Linux does not have bash by default; use sh instead.
  • Alpine Linux uses apk as its package manager.
  • It is recommended to run apk update upon first use to avoid anomalies.

Alpine Linux is extremely lightweight but offers functionality comparable to other Linux distributions. It is perfect for packaging Docker images. If you search for images on Docker Hub, you will find that many are based on Alpine Linux, making it naturally suited for containers.

This article references the following content: