簡介
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 alpineMAINTAINER guoxudong@keking.cn# 拷貝安裝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.txt
MIRRORS.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 themSystem 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 cacheQuerying 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 packagesRepository 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 contentsUse 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相比缺少許多功能。而缺少的這些功能又不想在基礎鏡像中安裝導致容器變大,這個時候就可以在容器運行后,根據實際需要安裝即可。
參考文檔
https://wiki.alpinelinux.org/wiki/Alpine_Linux_package_management