基於Alpine鏡像定制自己的工具箱


Alpine介紹

Alpine 操作系統是一個面向安全的輕型 Linux 發行版。目前 Docker 官方已開始推薦使用 Alpine 替代之前的 Ubuntu 做為基礎鏡像環境。這樣會帶來多個好處。包括鏡像下載速度加快,鏡像安全性提高,主機之間的切換更方便,占用更少磁盤空間等。

Alpine的特點:

  • 小巧:基於Musl libc和busybox,和busybox一樣小巧,最小的Docker鏡像只有5MB;
  • 安全:面向安全的輕量發行版;
  • 簡單:提供APK包管理工具,軟件的搜索、安裝、刪除、升級都非常方便。
  • 適合容器使用:由於小巧、功能完備,非常適合作為容器的基礎鏡像。
# 拉取鏡像
[root@master src]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
df20fa9351a1: Already exists 
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

# 查看鏡像大小
[root@master ~]# docker images | grep alpine
alpine                  latest              a24bb4013296        4 weeks ago         5.57MB

# 運行鏡像
[root@master src]# docker run -it alpine:latest
/ #

Alpine軟件包管理

1. 配置軟件源

Alpine源配置文件

/ # cat /etc/apk/repositories 
http://dl-cdn.alpinelinux.org/alpine/v3.11/main
http://dl-cdn.alpinelinux.org/alpine/v3.11/community

由於種種原因,官方源在國內很慢,甚至無法連接,我們將其改為國內鏡像源

/ # echo "https://mirrors.aliyun.com/alpine/v3.6/main/"  > /etc/apk/repositories
/ # echo "https://mirrors.aliyun.com/alpine/v3.6/community/" >> /etc/apk/repositories
/ # cat /etc/apk/repositories 
https://mirrors.aliyun.com/alpine/v3.6/main/
https://mirrors.aliyun.com/alpine/v3.6/community/

使用下面的這個可以始終使用最新版的

echo "https://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories
echo "https://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories

2. 軟件包管理

alpine 提供了非常好用的apk軟件包管理工具,可以方便地安裝、刪除、更新軟件。

查詢軟件

/ # apk search curl
lua5.2-curl-0.3.5-r2
uwsgi-alarm_curl-2.0.17-r0
py2-curl-7.43.0-r3
libcurl-7.61.1-r2
py3-curl-7.43.0-r3
py-curl-7.43.0-r3
curl-dev-7.61.1-r2
lua5.3-curl-0.3.5-r2
curl-7.61.1-r2
asterisk-curl-14.7.8-r0
php7-curl-7.1.17-r0
curl-dbg-7.61.1-r2
uwsgi-curl_cron-2.0.17-r0
lua5.1-curl-0.3.5-r2
lua-curl-0.3.5-r2
curl-doc-7.61.1-r2
php5-curl-5.6.40-r0
collectd-curl-5.6.2-r1
gst-plugins-bad1-1.10.4-r1

安裝軟件

/ # apk add curl
fetch https://mirrors.aliyun.com/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.6/community/x86_64/APKINDEX.tar.gz
(1/6) Installing libressl2.5-libcrypto (2.5.5-r2)
(2/6) Installing ca-certificates (20161130-r3)
(3/6) Installing libssh2 (1.8.2-r0)
(4/6) Installing libressl2.5-libssl (2.5.5-r2)
(5/6) Installing libcurl (7.61.1-r2)
(6/6) Installing curl (7.61.1-r2)
Executing busybox-1.31.1-r9.trigger
Executing ca-certificates-20161130-r3.trigger
OK: 9 MiB in 20 packages

安裝軟件指定版本

apk add packagename=11.0.2-r0
apk add "packagename>1.2.3-r0" # 設置最小版本
安裝本地pkg軟件包
apk add --allow-untrusted /path/to/foo.apk

如果有依賴包,就一起安裝
apk add --allow-untrusted pkg1.apk pkg2.apk

同時安裝多個軟件

apk add nodejs npm yarn

卸載軟件

/ # apk del curl
(1/6) Purging curl (7.61.1-r2)
(2/6) Purging libcurl (7.61.1-r2)
(3/6) Purging ca-certificates (20161130-r3)
Executing ca-certificates-20161130-r3.post-deinstall
(4/6) Purging libressl2.5-libssl (2.5.5-r2)
(5/6) Purging libssh2 (1.8.2-r0)
(6/6) Purging libressl2.5-libcrypto (2.5.5-r2)
Executing busybox-1.31.1-r9.trigger
OK: 6 MiB in 14 packages

apk使用幫助信息

/ # apk --help
apk-tools 2.10.4, compiled for x86_64.

Installing and removing packages:
  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

System maintenance:
  fix       Repair package or upgrade it without modifying main dependencies
  update    Update repository indexes from all remote repositories
  upgrade   Upgrade currently installed packages to match repositories
  cache     Download missing PACKAGEs to cache and/or delete unneeded files from cache

Querying information about packages:
  info      Give detailed information about PACKAGEs or repositories
  list      List packages by PATTERN and other criteria
  dot       Generate graphviz graphs
  policy    Show repository policy for packages

Repository maintenance:
  index     Create repository index file from FILEs
  fetch     Download PACKAGEs from global repositories to a local directory
  verify    Verify package integrity and signature
  manifest  Show checksums of package contents

Use apk <command> --help for command-specific help.
Use apk --help --verbose for a full command listing.

This apk has coffee making abilities.

基於Alpine定制帶有curl的鏡像

FROM alpine
RUN echo "https://mirrors.aliyun.com/alpine/v3.6/main/" > /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/v3.6/community/" >> /etc/apk/repositories
RUN ["apk","update"]
RUN ["apk","add","curl"]
# 默認是沒有開啟緩存的,所以裝完也不用清緩存

alpine linux 安裝包報錯

WARNING: Ign解決辦法

apk add gcc 時候報錯

WARNING: Ignoring APKINDEX.167438ca.tar.gz: No such file or directory
WARNING: Ignoring APKINDEX.a2e6dac0.tar.gz: No such file or directory
ERROR: unsatisfiable constraints:
gcc (missing):
required by: world[gcc]

解決辦法
apk update


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM