using linux apline on container

115 阅读1分钟

需求要点

  • run bash on alpine
  • set apk source on alpine in china
  • mgt soft with apk on alpine
  • set time zone in china

使用场景

Alpine is a security-oriented lightweight Linux distribution. Compared with CentOS, ubuntu is much smaller, about 5M. Due to its small size, it is used in many scenarios to create some lightweight images on demand. (why:to create some lightweight images)

The c library used by Alpine is the mini version of musl libc, unlike the gnu libc used by other Linux distributions. Although so-called compatible, but also only partially compatible, what is missing to fill what is, through a soft link to associate. (compatible:use a soft link to associate missing c library)

Note: After all, it is not an orthodox glibc. For some systems that rely heavily on glibc, it is recommended not to use Alpine, such as systems that use Oracle JDK. It is recommended to change Alpine to OpenJDK. Many large projects that rely heavily on glibc generally do not provide Alpine-based Dockerfile, such as tomcat.(do not use it on alpine : some projects that rely heavily on glibc)

操作指南

basic usage

# check docker running
# docker run -ti --rm alpine echo hello

# run bash on alpine
# docker run -ti --rm alpine:latest /bin/sh; # rm it after exiting
docker run -it --name=alpine alpine:latest /bin/sh

# exit container
# exit

# set apk source on alpine in china
sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories

# get alpine os version
alpine_version=`cat /etc/alpine-release`;echo "alpine $alpine_version";
# alpine 3.18.2

# get apk help usage
apk -h

# add pkg
# apk add xx

# del pkg
# apk del xx

# lst installed pkgs
apk list

# searh pkg to add
# apk search xx

# fetch pkg
# apk fetch xx

# get pkg detailed info
# apk info xx

# clean cache
# apk cache clean

# cache purge
# apk cache purge


# get time zone type
# about time zone type UTC,GMT,CST
date | cut -d " " -f6


# get time zone (format:+0800)
date -R | cut -d " " -f6;

set apk source on alpine in china

# set apk source on alpine in china
# 清华大
sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
# 阿里云
# sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 中科大
# sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

# todo:
# alpine/use-apk-src.sh tsinghua

set time zone in china

apk add tzdata;

TZ_TOUSE="/Asia/Shanghai";ln -sf /usr/share/zoneinfo/$TZ_TOUSE /etc/localtime ;echo "$TZ_TOUSE" /etc/timezone;date -R | cut -d " " -f6;

set time zone in china (off-line)

  • download pkg and its deps on on-line machine
wks=/root/off-line-pkgs;mkdir -p $wks;apk fetch -R -o $wks tzdata;
  • remove installed pkg (on-line) , if you are in debug|test env.
# ls -l $wks;
apk del -r tzdata;date;
  • install off-line pkg
# install off-line pkgs
owd=`pwd`;cd $wks ; ls | xargs -n 1 |xargs apk add --no-network;cd $owd;
  • set time zone to shanghai in china
# if you have set it, skip.
TZ_TOUSE="/Asia/Shanghai";ln -sf /usr/share/zoneinfo/$TZ_TOUSE /etc/localtime ;echo "$TZ_TOUSE" /etc/timezone;date -R | cut -d " " -f6;
  • remove downloaded pkg (off-line)
rm -rf $wks

参考文献

alpine-intro-and-apk-basic-usage

sgerrand/alpine-pkg-glibc: A glibc compatibility layer package for Alpine Linux (github.com)

choose alpine or centos ?

上篇阅读

installing docker on wsl & ubuntu

下篇阅读

build-simple-app-on-alpine (todo)

move some app to alpine (todo)

run some dev tool on alpine (todo)