簡介
Small. Simple. Secure.Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox.
Alpine Linux 是一個社群開發的面向安全應用的輕量級Linux發行版。 Alpine 的意思是“高山的”,它采用了musl libc和busybox以減小系統的體積和運作時資源消耗,同時還提供了自己的包管理工具apk。
适用環境
由于其小巧、安全、簡單以及功能完備的特點,被廣泛應用于衆多Docker容器中。我司目前使用的基礎鏡像均是基于該系統,
dockerhub上有提供各種語言的基礎鏡像.如:
node:8-alpine
、
python:3.6-alpine
,同時也可以基于alpine鏡像制作符合自己需求的基礎鏡像。
簡單的鏡像建構示例
這裡提供一個python3的基礎鏡像的
Dockerfile
,
get-pip.py可在
https://pip.pypa.io/en/latest/installing/下載下傳。
FROM alpine
MAINTAINER [email protected]
# 拷貝安裝pip的腳本
COPY get-pip.py /get-pip.py
# 設定alpine的鏡像位址為阿裡雲的位址
RUN echo "https://mirrors.aliyun.com/alpine/v3.6/main/" > /etc/apk/repositories \
# 安裝依賴包
&& apk update \
&& apk add --no-cache bash \
# libevent-dev libxml2-dev libffi libxml2 libxslt libxslt-dev \
python3 gcc g++ python3-dev python-dev linux-headers libffi-dev openssl-dev \
# 由于通過apk安裝的pip總是基于python2.7的版本,不符合項目要求,此處使用get-pip.py的方式
#安裝基于python3.6的pip
&& python3 /get-pip.py \
# 删除不必要的腳本
&& cd .. \
&& rm -f /get-pip.py \
# 此環境專用做運作django項目,是以移除不必要的工具,減少空間
# && pip uninstall -y pip setuptools wheel \
# 最後清空apk安裝時産生的無用檔案
&& rm -rf /var/cache/apk/*
對比:同樣版本的python,對比鏡像大小,可見使用alpine的優勢
~ docker images | grep python
python 3.4 ccbffa0d70d9 2 months ago 922MB
alpine-python3 latest 69e41b673a50 2 months ago 297MB
apk包管理
-
鏡像源配置
官方鏡像源清單:
http://dl-cdn.alpinelinux.org/alpine/MIRRORS.txtMIRRORS.txt中是目前Alpine官方提供的鏡像源(Alpine安裝的時候系統自動選擇最佳鏡像源)
國内鏡像源- 清華TUNA鏡像源: https://mirror.tuna.tsinghua.edu.cn/alpine/
- 中科大鏡像源: http://mirrors.ustc.edu.cn/alpine/
- 阿裡雲鏡像源: http://mirrors.aliyun.com/alpine/
$ vi /etc/apk/repositories # 将這兩行插入到repositories檔案開頭 http://mirrors.aliyun.com/alpine/v3.9/main http://mirrors.aliyun.com/alpine/v3.9/community # 後面是原有的預設配置 http://dl-cdn.alpinelinux.org/alpine/v3.8/main http://dl-cdn.alpinelinux.org/alpine/v3.8/community
-
apk包管理指令
這裡介紹一些常用的操作apk包管理指令
-
可以檢視完整的包管理指令apk --help
bash-4.3# apk --help 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.
-
列出所有已安裝的軟體包apk info
-
更新最新本地鏡像源apk apk update
-
更新軟體apk upgrade
-
apk search
搜尋可用軟體包,搜尋之前最好先更新鏡像源
bash-4.3# apk search #查找是以可用軟體包
bash-4.3# apk search -v #查找是以可用軟體包及其描述内容
bash-4.3# apk search -v 'acf*' #通過軟體包名稱查找軟體包
bash-4.3# apk search -v -d 'docker' #通過描述檔案查找特定的軟體包
-
從倉庫中安裝最新軟體包,并自動安裝必須的依賴包,也可以從第三方倉庫添加軟體包apk add
bash-4.3# apk add curl busybox-extras #軟體以空格分開這裡,這裡列舉我們用的最多的curl和telnet bash-4.3# apk add --no-cache curl bash-4.3# apk add mongodb --update-cache --repository http://mirrors.ustc.edu.cn/alpine/v3.6/main/ --allow-untrusted #從指定鏡像源拉取
- 安裝指定版本軟體包
bash-4.3# apk add mongodb=4.0.5-r0 bash-4.3# apk add 'mongodb<4.0.5' bash-4.3# apk add 'mongodb>4.0.5'
- 更新指定軟體包
bash-4.3# apk add --upgrade busybox #更新指定軟體包
- 安裝指定版本軟體包
-
解除安裝并删除指定軟體包apk del
-
結語
Alpine以其小巧、簡單在docker容器中得到了廣泛的應用。但是Alpine Linux使用了musl,可能和其他Linux發行版使用的glibc實作會有些不同。這裡主要介紹了它的基礎用法,但是足以滿足日常運維需要。畢竟在kubernetes叢集中操作容器内環境較直接在虛拟機或者實體機上操作更為複雜,由于縮減的容器的大小,導緻和CentOS或Ubuntu相比缺少許多功能。而缺少的這些功能又不想在基礎鏡像中安裝導緻容器變大,這個時候就可以在容器運作後,根據實際需要安裝即可。