😎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

字节码在跳舞

简介:分享学习笔记、知识。