😎Alpine、Debian、Ubuntu、Centos,誰是最佳選擇?

字節碼在跳舞 2024-04-12 18:39:14

Alpine

Debian/Ubuntu

CentOS

鏡像比較

今天告訴你一些常用的基礎鏡像。選擇合適的基礎鏡像取決于您的應用程序的要求。

代碼示例,文中dockerfile 參考:https://gitee.com/ft/hello-py.git

1. Alpine

Alpine是一個輕量級的Linux發行版,非常適合在容器中使用。它的鏡像非常小巧,通常只有幾MB大小。由于其小巧和安全性,Alpine是構建微服務和容器化應用的常見選擇。Alpine使用apk作爲其包管理工具。

包管理器:apk是 Alpine Linux 的包管理器,用于安裝、升級、卸載軟件包。它支持從官方倉庫、社區倉庫或本地文件安裝軟件,並具有依賴解決能力。命令示例:

apk add --no-cache <package-name>

--no-cache參數用于安裝時不保留緩存文件,減小鏡像大小。

Dockerfile 示例:

FROM alpineLABEL authors="ff755"EXPOSE 8000RUN apk update && \apk add --no-cache python3 py3-pip# 設置環境變量ENV PATH="/usr/bin/python3:${PATH}"ENV PIP_COMMAND="pip3"WORKDIR /appCOPY . /appRUN pip3 install --upgrade pip && pip3 install --no-cache-dir -r requirements.txtCMD [ "python3", "./main.py" ]

可以在官網alpinelinux packages中搜索包名。

docker build -f Dockerfile-Alpine -t hello-py:alpine .

➜  hello-py git:(main) docker imagesREPOSITORY                                      TAG                       IMAGE ID       CREATED          SIZEhello-py                                        alpine                    2d3b6ae1dc4f   36 minutes ago   108MB

2. Debian/Ubuntu:

Debian和Ubuntu是常見的Linux發行版,它們也提供了官方的Docker鏡像。這些鏡像相對較大,但提供了廣泛的軟件包和工具,適用于各種應用場景。Debian、Ubuntu都可以使用apt包管理工具。

包管理器:apt(Advanced Package Tool)是 Debian 及其衍生發行版(如 Ubuntu)的包管理器。它具有強大的依賴解決能力和豐富的軟件包資源。命令示例:

apt-get update && apt-get install -y --no-install-recommends <package-name>

update更新包索引

install安裝軟件包

-y自動確認

--no-install-recommends不安裝推薦但非必需的依賴項,有助于減小鏡像大小。

FROM debianLABEL authors="ff755"EXPOSE 8000WORKDIR /appCOPY . /app# 更新 apt 包索引RUN apt-get update && \# 安裝 Python 及其基本開發依賴包apt-get install -y python3 python3-dev python3-pip && \# 清理下載的包文件,以減小鏡像大小rm -rf /var/lib/apt/lists/*# 使用國內源加速安裝項目依賴RUN pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple# 安裝項目依賴,不使用緩存以確保獲取最新版本RUN pip3 install --no-cache-dir -r requirements.txtCMD ["python3", "./main.py"]

docker build -f Dockerfile-Debian -t hello-py:debian .

➜  hello-py git:(main) docker imagesREPOSITORY                                      TAG                       IMAGE ID       CREATED          SIZEhello-py                                        debian                    dea671149344   29 minutes ago   548MB

3. CentOS:

CentOS是一個基于Red Hat Enterprise Linux(RHEL)源代碼構建的開源Linux發行版。它提供了穩定、可靠和兼容RHEL的環境。CentOS使用yum作爲其包管理工具。

CentOS由于過于穩定,新版本的包一般沒有。使用作爲操作系統,構建鏡像,建議選擇更小鏡像作爲基礎鏡像。

如果有興趣,可自己嘗試一下,使用Centos構建鏡像。

4. 鏡像比較

Alpine通過Dockerfile構建鏡像大小爲108MB。

Debian通過Dockerfile構建鏡像達到了548MB。

前文基于鏡像python:3.11.9-alpine3.19構建出的大小爲93.9MB。

➜  hello-py git:(main) docker imagesREPOSITORY                                      TAGIMAGE ID       CREATED          SIZEhello-py                                        py3-alpine7fccb1cfe4df   40 seconds ago   93.9MB

建議通過Docker Hub查找合適的鏡像構建自己的鏡像,實在找不到,再利用基礎鏡像構建。

分別拉取Alpine、Debian、Ubuntu、Centos基礎鏡像。使用docker pull [鏡像名稱]

➜  ~ docker imagesREPOSITORY                                      TAG                       IMAGE ID       CREATED        SIZEdebian                                          latest                    6f4986d78878   2 years ago    124MBalpine                                          latest                    c059bfaa849c   2 years ago    5.59MBubuntu                                          latest                    ba6acccedd29   2 years ago    72.8MBcentos                                          latest                    5d0da3dc9764   2 years ago    231MB

鏡像從小到大依次爲 alpine(5.59MB)、ubuntu(72.8MB)、debian(124MB)、centos(231MB)。推薦alpine構建鏡像,減小構建出的鏡像文件大小。

通過使用適用于不同基礎鏡像的包管理工具,你可以在Dockerfile中安裝所需的軟件包。這樣可以確保在構建鏡像時所需的依賴項都被正確安裝,從而創建出滿足應用程序要求的鏡像。

忍不住要加個關注!不是我吹,但你會後悔沒關注的!

0 阅读:34

字節碼在跳舞

簡介:分享學習筆記、知識。