Getting Started with Alpine Linux

Publish: 2020-05-06 | Modify: 2020-05-06

Alpine Linux is a Linux distribution based on musl and BusyBox, designed for security, simplicity, and resource efficiency. It has a very small footprint and is suitable for use as a Docker image. Recently, Xiaoz learned about Alpine Linux when packaging container images for CCAA/Zdir. Despite its small size, Alpine Linux is feature-rich 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, it is important to use sh instead of bash.

Alpine Linux images are very small, less than 6MB in size, making them ideal for container packaging.

Software Management in Alpine Linux

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 to update the software before first use. Some commonly used apk commands are:

# 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 Timezone in Alpine Linux

By default, Alpine Linux does not use the GMT+8 timezone. Some projects require synchronization with Beijing time, so we need to modify the default timezone in Alpine Linux. Here's how:

# Install timezone
apk add -U tzdata
# View timezone list
ls /usr/share/zoneinfo
# Copy the desired timezone file to localtime
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# View current time
date
# To reduce the image size, tzdata can be removed
apk del tzdata

Modifying the Software Repository in Alpine Linux

If you are using Alpine Linux on a network in China, you can use a mirror repository for better speed. Some commonly used mirror repositories in China are:

The configuration file for the software repository is located at /etc/apk/repositories and has the following contents:

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

As you can see, the Alpine software repository version used here is v3.11, so when modifying it, the version should be kept consistent. For example, to change to the Alibaba repository:

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

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

Summary

  • Alpine Linux does not have bash by default, so sh is used instead.
  • Alpine Linux uses apk as the package manager.
  • It is recommended to update the software with apk update before first use to avoid any issues.

Alpine Linux has a very small footprint but is not lacking in functionality compared to other Linux distributions. It is perfect for packaging Docker images, and you will find many Docker images based on Alpine Linux when searching on Docker Hub. It is truly tailored for containers.

This article references the following sources:


Comments