Alpine Linux 是一个面向安全应用的轻量级 Linux 发行版。它采用了 musl libc 和 busybox 以减小系统的体积和运行时资源消耗,同时还提供了自己的包管理工具 apk。Alpine Linux 的内核都打了 grsecurity/PaX 补丁,并且所有的程序都编译为 Position Independent Executables (PIE) 以增强系统的安全性。
Alpine Linux 的优势和问题
优势
- Alpine Linux 的 Docker 镜像特点是轻巧(大小只有 5M)且有完整的包管理工具(APK)。
- Docker 官方镜像可能会用 Alpine Linux 替换 Ubuntu。
问题
- Alpine Linux 使用了 musl,可能和其他 Linux 发行版使用的 glibc 实现会有些不同。
- musl 实现的 DNS 服务不会使用 resolv.conf 文件中的 search 和 domain 两个配置,通过 DNS 来进行服务发现时需要注意。
Alpine Linux 包管理
- apk 包管理命令
Alpine 使用 apk 进行包管理,通过 apk –help 命令查看完整的包管理命令,下面列举常用命令:
update:从远程镜像源中更新本地镜像源索引
update 命令会从各个镜像源列表下载 APKINDEX.tar.gz 并存储到本地缓存,一般在/var/cache/apk/(Alpine 在该目录下)、/var/lib/apk/、/etc/apk/cache/下。
$ apk updateadd:安装 PACKAGES 并自动解决依赖关系
add 命令从仓库中安装最新软件包,并自动安装必须的依赖包,也可以从第三方仓库添加软件包。
$ apk add openssh openntpd vim
$ apk add --no-cache mysql-client
$ apk add docker --update-cache --repository http://mirrors.ustc.edu.cn/alpine/v3.4/main/ --allow-untrusted安装指定版本软件包
$ apk add asterisk=1.6.0.21-r0
$ apk add 'asterisk<1.6.1'
$ apk add 'asterisk>1.6.1'del:卸载并删除 PACKAGES
$ apk del openssh openntp vimupgrade:升级当前已安装的软件包
upgrade 命令升级系统已安装的所以软件包(一般包括内核),当然也可指定仅升级部分软件包(通过-u 或–upgrade 选择指定)。
$ apk update #更新最新本地镜像源
$ apk upgrade #升级软件
$ apk add --upgrade busybox #指定升级部分软件包search:搜索软件包
search 命令搜索可用软件包,-v 参数输出描述内容,支出通配符,-d 或—description 参数指定通过软件包描述查询。
$ apk search #查找所以可用软件包
$ apk search -v #查找所以可用软件包及其描述内容
$ apk search -v 'acf*' #通过软件包名称查找软件包
$ apk search -v -d 'docker' #通过描述文件查找特定的软件包info:列出 PACKAGES 或镜像源的详细信息
info 命令用于显示软件包的信息。
$ apk info #列出所有已安装的软件包
$ apk info -a zlib #显示完整的软件包信息
$ apk info --who-owns /sbin/lbu #显示指定文件属于的包清理非必要文件
制作镜像时,用来精简容器大小
rm -rf /var/lib/apt/lists/* && rm /var/cache/apk/*
apk cache clean信任 CA 根证书
解决:x509: certificate signed by unknown authority
cp *.crt /usr/local/share/ca-certificates/
apk --no-cache add ca-certificates
update-ca-certificates /usr/local/share/ca-certificates/更新 CA 证书(解决 TLS 证书校验问题):
apk add --no-cache ca-certificates && update-ca-certificates配置时区
RUN apk --update add tzdata && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
rm -rf /var/cache/apk/*说明:
- 不建议
apk del tzdata,否则设置TZ环境变量时时区不生效
镜像源配置
官方镜像列表
http://rsync.alpinelinux.org/alpine/MIRRORS.txt
MIRRORS.txt 中是当前 Alpine 官方提供的镜像源(Alpine 安装的时候系统自动选择最佳镜像源)
国内镜像源
- 清华 TUNA 镜像源:https://mirror.tuna.tsinghua.edu.cn/alpine/
- 中科大镜像源:http://mirrors.ustc.edu.cn/alpine/
- 阿里云镜像源:http://mirrors.aliyun.com/alpine/
如何配置软件源
以中科大源为例:在/etc/apk/repositories 文件中加入对应源地址就行了,一行一个地址。
$ vi /etc/apk/repositories
# /media/cdrom/apks
http://mirrors.ustc.edu.cn/alpine/v3.5/main
http://mirrors.ustc.edu.cn/alpine/v3.5/communityDockerfile 示例
FROM alpine:3.11.6
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
ENV TZ=Asia/Shanghai
RUN apk update && \
apk add tzdata && \
echo "${TZ}" > /etc/timezone && \
ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime && \
rm /var/cache/apk/*Alpine Linux 使用的是 Gentoo 一样的 OpenRCinit 系统.
以下命令可用于管理 init 系统
rc-update
rc-update 主要用于不同运行级增加或者删除服务。
rc-update 语法格式
Usage: rc-update [options] add <service> [<runlevel>...]
or: rc-update [options] del <service> [<runlevel>...]
or: rc-update [options] [show [<runlevel>...]]
Options: [ asuChqVv ]
-a, --all Process all runlevels
-s, --stack Stack a runlevel instead of a service
-u, --update Force an update of the dependency tree
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)使用实例
$ rc-update add docker boot #增加一个服务
$ rc-update del docker boot #删除一个服务rc-status
rc-status 主要用于运行级的状态管理。
rc-status 语法格式
Usage: rc-status [options] <runlevel>...
or: rc-status [options] [-a | -c | -l | -r | -s | -u]
Options: [ aclrsuChqVv ]
-a, --all Show services from all run levels
-c, --crashed Show crashed services
-l, --list Show list of run levels
-r, --runlevel Show the name of the current runlevel
-s, --servicelist Show service list
-u, --unused Show services not assigned to any runlevel
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)使用实例
$ rc-status #检查默认运行级别的状态
$ rc-status -a #检查所有运行级别的状态rc-service
rc-service 主用于管理服务的状态
rc-service 语法格式
Usage: rc-service [options] [-i] <service> <cmd>...
or: rc-service [options] -e <service>
or: rc-service [options] -l
or: rc-service [options] -r <service>
Options: [ e:ilr:INChqVv ]
-e, --exists <arg> tests if the service exists or not
-i, --ifexists if the service exists then run the command
-I, --ifinactive if the service is inactive then run the command
-N, --ifnotstarted if the service is not started then run the command
-l, --list list all available services
-r, --resolve <arg> resolve the service name to an init script
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)使用实例
$ rc-service sshd start #启动一个服务。
$ rc-service sshd stop #停止一个服务。
$ rc-service sshd restart #重启一个服务。openrc
openrc 主要用于管理不同的运行级。
openrc 语法格式
Usage: openrc [options] [<runlevel>]
Options: [ a:no:s:SChqVv ]
-n, --no-stop do not stop any services
-o, --override <arg> override the next runlevel to change into
when leaving single user or boot runlevels
-s, --service <arg> runs the service specified with the rest
of the arguments
-S, --sys output the RC system type, if any
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)Alpine Linux 可用的运行级
default
sysinit
boot
single
rebootshutdown
使用实例
$ openrc single #更改为single运行级其它指令
$ reboot #重启系统,类似于shutdown -r now。
$ halt #关机,类似于shutdown -h now。
$ poweroff #关机参考
- https://wiki.alpinelinux.org/wiki/Alpine_Linux_Init_System
- https://wiki.alpinelinux.org/wiki/Alpine_Linux_package_management
- /docker/dockerfile/docker-tini/
最近更新
最新评论