ljzsdut
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage
Edit page

02 Alpine包管理工具 Apk

alpine包管理工具 –apk

推荐文档:https://www.cyberciti.biz/faq/10-alpine-linux-apk-command-examples/

Alpine中软件安装包的名字可能会与其他发行版有所不同,可以在https://pkgs.alpinelinux.org/packages网站搜索并确定安装包的名称。

配置软件源

添加国内的镜像地址:

在/etc/apk/repositories文件中加入对应源地址就行了,一行一个地址,如:

echo 'http://mirrors.aliyun.com/alpine/v3.9/main'>/etc/apk/repositories 
echo 'http://mirrors.aliyun.com/alpine/v3.9/community'>>/etc/apk/repositories

如果需要的安装包不在主索引内,但是在测试或社区索引中,那么可以按照以下方法使用这些安装包:

echo 'http://mirrors.ustc.edu.cn/alpine/v3.9/testing'>>/etc/apk/repositories

国内镜像源(镜像站):

阿里云镜像源(推荐,稳定):http://mirrors.aliyun.com/alpine/

清华TUNA镜像源:https://mirror.tuna.tsinghua.edu.cn/alpine/

中科大镜像源:http://mirrors.ustc.edu.cn/alpine/

apk使用

命令格式

apk [options] command
apk [options] command pkgName
apk [options] command pkgName1 pkgName2
/ # apk --help --verbose
apk-tools 2.10.3, compiled for x86_64.

usage: apk COMMAND [-h|--help] [-p|--root DIR] [-X|--repository REPO] [-q|--quiet] [-v|--verbose] [-i|--interactive] [-V|--version] [-f|--force] [--force-binary-stdout]
           [--force-broken-world] [--force-non-repository] [--force-old-apk] [--force-overwrite] [--force-refresh] [-U|--update-cache] [--progress] [--progress-fd FD]
           [--no-progress] [--purge] [--allow-untrusted] [--wait TIME] [--keys-dir KEYSDIR] [--repositories-file REPOFILE] [--no-network] [--no-cache]
           [--cache-dir CACHEDIR] [--cache-max-age AGE] [--arch ARCH] [--print-arch] [ARGS]...

The following commands are available:
  add       Add PACKAGEs to 'world' and install (or upgrade) them, while ensuring that all dependencies are met
  del       Remove PACKAGEs from 'world' and uninstall them
  fix       Repair package or upgrade it without modifying main dependencies
  update    Update repository indexes from all remote repositories (从remote仓库更新本地镜像源index)
  info      Give detailed information about PACKAGEs or repositories
  list      List packages by PATTERN and other criteria
  search    Search package by PATTERNs or by indexed dependencies  (查找所有可用软件包)
  upgrade   Upgrade currently installed packages to match repositories (更新已经安装的软件)
  cache     Download missing PACKAGEs to cache and/or delete unneeded files from cache
  version   Compare package versions (in installed database vs. available) or do tests on literal version strings
  index     Create repository index file from FILEs
  fetch     Download PACKAGEs from global repositories to a local directory
  audit     Audit the directories for changes
  verify    Verify package integrity and signature
  dot       Generate graphviz graphs
  policy    Show repository policy for packages
  stats     Show statistics about repositories and installations
  manifest  Show checksums of package contents

Global options:
  -h, --help              Show generic help or applet specific help
  -p, --root DIR          Install packages to DIR
  -X, --repository REPO   Use packages from REPO
  -q, --quiet             Print less information
  -v, --verbose           Print more information (can be doubled)
  -i, --interactive       Ask confirmation for certain operations
  -V, --version           Print program version and exit
  -f, --force             Enable selected --force-* (deprecated)
  --force-binary-stdout   Continue even if binary data is to be output
  --force-broken-world    Continue even if 'world' cannot be satisfied
  --force-non-repository  Continue even if packages may be lost on reboot
  --force-old-apk         Continue even if packages use unsupported features
  --force-overwrite       Overwrite files in other packages
  --force-refresh         Do not use cached files (local or from proxy)
  -U, --update-cache      Alias for --cache-max-age 1
  --progress              Show a progress bar
  --progress-fd FD        Write progress to fd
  --no-progress           Disable progress bar even for TTYs
  --purge                 Delete also modified configuration files (pkg removal) and uninstalled packages from cache (cache clean)
  --allow-untrusted       Install packages with untrusted signature or no signature
  --wait TIME             Wait for TIME seconds to get an exclusive repository lock before failing
  --keys-dir KEYSDIR      Override directory of trusted keys
  --repositories-file REPOFILE Override repositories file
  --no-network            Do not use network (cache is still used)
  --no-cache              Do not use any local cache path(不在本地缓存安装文件)
  --cache-dir CACHEDIR    Override cache directory
  --cache-max-age AGE     Maximum AGE (in minutes) for index in cache before refresh
  --arch ARCH             Use architecture with --root
  --print-arch            Print default arch and exit

This apk has coffee making abilities.

更新仓库源index

apk update #更新最新本地镜像源元数据

更新软件

apk upgrade #升级所有软件(更新系统)
apk upgrade openssh #升级指定软件
apk upgrade openssh openntp vim #升级多个软件

apk add --upgrade busybox #升级指定软件包及其依赖包

查找软件

apk search 	#查找所有可用软件包
apk search -v #查找所有可用软件包及其描述内容
apk search -v 'acf*' #通过通配符软件包名称查找软件包
apk search -v -d 'docker' #通过描述文件查找特定的软件包(description)

安装软件

# 安装1个软件
apk add nginx

# 安装多个软件
apk add nginx bash

# 指定版本
apk add nginx=1.14.2-r2

# 指定最小版本
apk add "nginx>1.14.2-r2"

# 使用正则表达式指定版本
apk add bash=~5.0

小技巧:禁用缓存

# --no-cache作用:不在/var/cache/apk/目录下生成相关的缓存文件,所以rm -f /var/cache/apk/*没作用。
apk add --no-cache mariadb mariadb-client mariadb-server-utils pwgen && rm -f /var/cache/apk/*

小技巧:集合命名

# 集合命名
apk -U add --no-cache --virtual .name git openssh-client
apk del .name

这种用法在 Dockerfile 中很常见,将多个包的集合命名为一个名称,方便了后续卸载,例如下面的场景:

# These packages are not installed immediately, but are added at runtime or ONBUILD to shrink the image as much as possible. Notes:
#   * build-base: used so we include the basic development packages (gcc)
#   * linux-headers: commonly needed, and an unusual package name from Alpine.
ENV BUILD_PACKAGES="build-base linux-headers aria2"
RUN apk add --no-cache --virtual=.build-deps $BUILD_PACKAGES && \
    apk add --no-cache libaio libaio-dev && \
    aria2c -c -x 16 https://www.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.14/source/tarball/percona-xtrabackup-2.4.14.tar.gz && \
    aria2c -c -x 16 http://jenkins.percona.com/downloads/boost/boost_1_59_0.tar.gz

...其他操作...

RUN apk del .build-deps  #临时软件使用完后,卸载掉

安装本地pkg软件包

apk add --allow-untrusted /path/to/foo.apk
示例(如果有依赖包,就一起安装)apk add --allow-untrusted pkg1.apk pkg2.apk

查看包

localhost:~# apk info -h
apk-tools 2.12.7, compiled for x86_64.

usage: apk info [<OPTIONS>...] PACKAGES...
   or: apk info -W FILE
   
......

Info options:
  -a, --all             List all information known about the package
  -d, --description     Print the package description
  -e, --installed       Check package installed status
  -L, --contents        List files included in the package
  -P, --provides        List what the package provides
  -r, --rdepends        List reverse dependencies of the package (all other
                        packages which depend on the package)
  -R, --depends         List the dependencies of the package
  -s, --size            Print the package's installed size
  -w, --webpage         Print the URL for the package's upstream webpage
  -W, --who-owns        Print the package which owns the specified file
  --install-if          List the package's install_if rule
  --license             Print the package SPDX license identifier
  --replaces            List the other packages for which this package is
                        marked as a replacement
  --rinstall-if         List other packages whose install_if rules refer to
                        this package
  --triggers            Print active triggers for the package

查看本地已经安装的包

apk info
apk info -vv | grep 'htop'

查看包是否已经安装

localhost:~#  apk info -e atop    # 没有任何输出,表示未安装
localhost:~#  apk info -e htop
htop

查看指定文件所属于的包

# apk info --who-owns /etc/passwd
/etc/passwd is owned by alpine-baselayout-3.0.4-r0
# apk info --who-owns /sbin/apk
/sbin/apk is owned by apk-tools-2.6.8-r2

查看已经安装的包的安装文件列表

localhost:~# apk info -L htop
htop-3.1.1-r0 contains:
usr/bin/htop
usr/share/applications/htop.desktop
usr/share/icons/hicolor/scalable/apps/htop.svg
usr/share/pixmaps/htop.png

localhost:~# apk manifest htop
sha1:feed447f952ed71ca6b8356056c761ecc77379e5  usr/bin/htop
sha1:6102c885e5c62cacda5975609ffa5b23c2fbf2d8  usr/share/applications/htop.desktop
sha1:439f099193e643f10f92733fbda735f6ecb17baf  usr/share/icons/hicolor/scalable/apps/htop.svg
sha1:b7715ea3c224bfad909c0272ed87fa422c0169fd  usr/share/pixmaps/htop.png

查看包的依赖

# apk info -R atop
atop-2.2_p3-r0 depends on:
so:libc.musl-x86_64.so.1
so:libncursesw.so.6
so:libz.so.1

查看当前包被操作系统上哪些包依赖

# apk info -r bash
bash-completion-2.4-r0

显示软件包所有的信息

# apk info -a zlib
zlib-1.2.12-r1 description:
A compression/decompression Library

zlib-1.2.12-r1 webpage:
https://zlib.net/

zlib-1.2.12-r1 installed size:
108 KiB

zlib-1.2.12-r1 depends on:
so:libc.musl-x86_64.so.1

zlib-1.2.12-r1 provides:
so:libz.so.1=1.2.12

zlib-1.2.12-r1 is required by:
kmod-libs-29-r2
openssh-client-common-8.8_p1-r1
kmod-29-r2
apk-tools-2.12.7-r3
openssh-client-default-8.8_p1-r1
openssh-server-8.8_p1-r1

zlib-1.2.12-r1 contains:
lib/libz.so.1
lib/libz.so.1.2.12

zlib-1.2.12-r1 triggers:

zlib-1.2.12-r1 has auto-install rule:

zlib-1.2.12-r1 affects auto-installation of:

zlib-1.2.12-r1 replaces:

zlib-1.2.12-r1 license:
Zlib

编译安装xtrabackup示例

apk add cmake make gcc g++ libaio libaio-dev

wget https://www.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.14/source/tarball/percona-xtrabackup-2.4.14.tar.gz
wget -P /tmp http://jenkins.percona.com/downloads/boost/boost_1_59_0.tar.gz

cmake -DBUILD_CONFIG=xtrabackup_release -DWITH_MAN_PAGES=OFF -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/tmp/boost_1_59_0.tar.gz
cmake -DBUILD_CONFIG=xtrabackup_release -DWITH_MAN_PAGES=OFF -DIGNORE_AIO_CHECK=ON -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/tmp && make -j4

常用软件

RUN apk --update --no-cache add\
  alpine-sdk\
  autoconf\
  automake\
  binutils-gold\
  build-base\
  dumb-init\
  g++\
  gcc\
  gcompat\
  gnupg\
  gzip\
  jpeg\
  jpeg-dev\
  libc6-compat\
  libffi\
  libffi-dev\
  libpng\
  libpng-dev\
  libstdc++\
  libtool\
  linux-headers\
  openssl-dev\
  make\
  mysql\
  mysql-client\
  mysql-dev\
  mesa-gles\
  nasm\
  nodejs\
  nss\
  openjdk8-jre\
  openssh-client\
  paxctl\
  python3\
  python3-dev\
  py-pip\
  sudo\
  tar\
  unzip\
  curl\
  git\
  bash\
  wget\
  chromium

总结

CommandUsageExample
apk updateUpdate the package listapk update
apk upgradeUpgrade the systemapk update apt ugrade
apk add pkgAdd a packageapk add apache
apk del pkgDelete a packageapk del nginx
apk search -vSearch for packagesapk search -v
apk search -v -d ’nginx’
apk search -v ‘apache*’
apk infoList all installed pacakgesapk info
apk fixRepair package or upgrade it without modifying main dependenciesapk fix
apk policy pkgShow repository policy for packagesapk policy bash
apk statsShow statistics about repositories and installationsapk stats

参考文档

https://www.cyberciti.biz/faq/10-alpine-linux-apk-command-examples/

https://wiki.alpinelinux.org/wiki/Alpine_Linux_Init_System

https://wiki.alpinelinux.org/wiki/Alpine_Linux_package_management