Alpine 操作系統是一個面向安全的輕型 Linux 發行版。它不同于通常 Linux 發行版,Alpine 采用了 musl libc 和 busybox 以減小系統的體積和運行時資源消耗,但功能上比 busybox 又完善的多,因此得到開源社區越來越多的青睞。在保持瘦身的同時,Alpine 還提供了自己的包管理工具 apk,可以通過 https://pkgs.alpinelinux.org/packages 網站上查詢包信息,也可以直接通過 apk 命令直接查詢和安裝各種軟件。
Alpine 由非商業組織維護的,支持廣泛場景的 Linux發行版,它特別為資深/重度Linux用戶而優化,關注安全,性能和資源效能。Alpine 鏡像可以適用于更多常用場景,并且是一個優秀的可以適用于生產的基礎系統/環境。
Alpine Docker 鏡像也繼承了 Alpine Linux 發行版的這些優勢。相比于其他 Docker 鏡像,它的容量非常小,僅僅只有 5 MB 左右(對比 Ubuntu 系列鏡像接近 200 MB),且擁有非常友好的包管理機制。官方鏡像來自 docker-alpine 項目。
目前 Docker 官方已開始推薦使用 Alpine 替代之前的 Ubuntu 做為基礎鏡像環境。這樣會帶來多個好處。包括鏡像下載速度加快,鏡像安全性提高,主機之間的切換更方便,占用更少磁盤空間等。
下表是官方鏡像的大小比較:
REPOSITORY ? ? ? ? ?TAG ? ? ? ? ? IMAGE ID ? ? ? ? ?VIRTUAL SIZE
alpine ? ? ? ? ? ? ?latest ? ? ? ?4e38e38c8ce0 ? ? ?4.799 MB
debian ? ? ? ? ? ? ?latest ? ? ? ?4d6ce913b130 ? ? ?84.98 MB
ubuntu ? ? ? ? ? ? ?latest ? ? ? ?b39b81afc8ca ? ? ?188.3 MB
centos ? ? ? ? ? ? ?latest ? ? ? ?8efe422e6104 ? ? ?210 MB
獲取并使用官方鏡像
由于鏡像很小,下載時間往往很短,讀者可以直接使用 docker run 指令直接運行一個 Alpine 容器,并指定運行的 Linux 指令,例如:
$ docker run alpine echo '123'
123
遷移至 Alpine 基礎鏡像
目前,大部分 Docker 官方鏡像都已經支持 Alpine 作為基礎鏡像,可以很容易進行遷移。
例如:
ubuntu/debian -> alpine
python:2.7 -> python:2.7-alpine
ruby:2.3 -> ruby:2.3-alpine
另外,如果使用 Alpine 鏡像替換 Ubuntu 基礎鏡像,安裝軟件包時需要用 apk 包管理器替換 apt 工具,如
$ apk add --no-cache <package>
Alpine 中軟件安裝包的名字可能會與其他發行版有所不同,可以在 https://pkgs.alpinelinux.org/packages 網站搜索并確定安裝包名稱。如果需要的安裝包不在主索引內,但是在測試或社區索引中。那么可以按照以下方法使用這些安裝包。
$ echo "http://dl-4.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
$ apk --update add --no-cache <package>
apk命令詳解
/ # apk -h
apk-tools 2.10.0, 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 官網:http://alpinelinux.org/
Alpine 官方倉庫:https://github.com/alpinelinux
Alpine 官方鏡像:https://hub.docker.com/_/alpine/
Alpine 官方鏡像倉庫:https://github.com/gliderlabs/docker-alpine
?