Alpine Pkg Manager(包管理)

针对Alpine系统的包管理简要使用

1. 相关Alpine包管理地址

  1. 包管基础:https://wiki.alpinelinux.org/wiki/Alpine_Linux_package_management
  2. 包内容检索:https://pkgs.alpinelinux.org

1.1. apk相关命令使用

信息检索

// 查看所有包
apk search -v

// 查看所有含acf的包
apk search -v 'acf*' 

// 查看描述中含NTP的包
apk search -v --description 'NTP' 

包内容查看

// 包所有信息,涵盖cmd提供、安装大小、描述等
apk info -a zlib

// 包概要说明(描述、大小、官网)
apk info -v asterisk

// 包命令提供
apk info -P bind-tools

// 列出apline已安装的包
apk -vv info|sort

1.4. 包缓存

Alpine支持将下载的包缓存到一个指定的目录,方便其他Alpine复用(通过挂载)

apk add e2fsprogs
dd if=/dev/zero of=/apkcache.img bs=1M count=128
mkfs.ext2 -F /apkcache.img
mkdir -p /etc/apk/cache
mount -t ext2 /apkcache.img /etc/apk/cache
apk update

1.5. 包统计

/ # apk stats
installed:
  packages: 28
  dirs: 155
  files: 3043
  bytes: 18485248
  triggers: 2
available:
  names: 24065
  packages: 10335
atoms:
  num: 7248

2. busybox与busybox-extra

BusyBox是一个多调用的二进制程序,它结合了许多常见的Unix将实用程序转换为单个可执行文件。

大多数命令通过软链到busybox:

...
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 stat -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 stty -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 su -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 sync -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 tar -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 touch -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 true -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 umount -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 uname -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 usleep -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 watch -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jul 11 17:29 zcat -> /bin/busybox

2.1. 当前busybox定义的函数

// 输入busybox命令查看定义的相关功能支持
$ busybox

Currently defined functions:
[, [[, acpid, add-shell, addgroup, adduser, adjtimex, arch, arp, arping, ash,
    awk, base64, basename, bbconfig, bc, beep, blkdiscard, blkid, blockdev, brctl,
    bunzip2, bzcat, bzip2, cal, cat, chgrp, chmod, chown, chpasswd, chroot, chvt,
    cksum, clear, cmp, comm, conspy, cp, cpio, crond, crontab, cryptpw, cut, date,
    dc, dd, deallocvt, delgroup, deluser, depmod, df, diff, dirname, dmesg,
    ....

$ busybox-extras
Currently defined functions:
    arch, dnsd, fakeidentd, ftpd, ftpget, ftpput, httpd, inetd, readahead, telnet,
    telnetd, tftp, tftpd, udhcpd

3. alpine网络调试工具

  • telnet
    • apk add busybox-extras
  • busybox自带工具
    • nc
    • netstat
  • 需要安装:
    • curl (curl)
    • tcpdump (tcpdump)
    • dig (bind-tools)
  • 可选安装:
    • socat (socat)
    • ss (iproute2)
// 基于ss查看监听情况
ss -tan -4 -o state listening

4. Dockerfile文件

$ cat Dockerfile
FROM alpine:latest

RUN apk add --no-cache \
    busybox-extras curl tcpdump

COPY bashrc ~/.bashrc

构建自己的Apline基础Docker镜像

可能需要修改的Mirror仓库源

// 修改成阿里云镜像(https://developer.aliyun.com/mirror/alpine)
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

// 修改成清华的镜像
sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories

// 增加testing仓库(默认只有main和community)
sed -i '$a@testing http://mirrors.tuna.tsinghua.edu.cn/alpine/edge/testing' /etc/apk/repositories

基于命令查询安装包

通过alpine官方提供的包检索Page查询:https://pkgs.alpinelinux.org/contents?file=ss&path=&name=&branch=edge

选择自定义相关安装的内容

// 我这里选择了curl、dig、系统、网络调试、openssl等工具的集成
apk update && apk add curl bind-tools \
    strace sysstat procps iproute2 \
    tcpdump \
    openssl ca-certificates \
    git tree \
    util-linux

// 测试包安装的内容
apk add perf@testing