docker镜像 Alpine、Debian、Ubuntu、Centos 最佳选择

docker镜像 Alpine、Debian、Ubuntu、Centos 最佳选择_第1张图片

dockerfile 参考:https://gitee.com/ft/hello-py.git 

1. Alpine

        Alpine是一个轻量级的Linux发行版,非常适合在容器中使用。它的镜像非常小巧,通常只有几MB大小。由于其小巧和安全性,Alpine是构建微服务和容器化应用的常见选择。Alpine使用apk作为其包管理工具。

        包管理器: apk 是 Alpine Linux 的包管理器,用于安装、升级、卸载软件包。它支持从官方仓库、社区仓库或本地文件安装软件,并具有依赖解决能力。命令示例:

apk add --no-cache 

--no-cache 参数用于安装时不保留缓存文件,减小镜像大小。

Dockerfile 示例:

FROM alpine
LABEL authors="ff755"
EXPOSE 8000

RUN apk update && \
   apk add --no-cache python3 py3-pip
# 设置环境变量
ENV PATH="/usr/bin/python3:${PATH}"
ENV PIP_COMMAND="pip3"

WORKDIR /app
COPY . /app

RUN pip3 install --upgrade pip && pip3 install --no-cache-dir -r requirements.txt

CMD [ "python3", "./main.py" ]

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

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

➜  hello-py git:(main) docker images
REPOSITORY                                      TAG                       IMAGE ID       CREATED          SIZE
hello-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 

update 更新包索引

install 安装软件包

-y 自动确认

--no-install-recommends 不安装推荐但非必需的依赖项,有助于减小镜像大小。

FROM debian
LABEL authors="ff755"
EXPOSE 8000

WORKDIR /app

COPY . /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.txt

CMD ["python3", "./main.py"]
docker build -f Dockerfile-Debian -t hello-py:debian .
hello-py git:(main) docker images
REPOSITORY                                      TAG                       IMAGE ID       CREATED          SIZE
hello-py                                        debian                    dea671149344   29 minutes ago   548MB


3. CentOS:

        CentOS是一个基于Red Hat Enterprise Linux(RHEL)源代码构建的开源Linux发行版。它提供了稳定、可靠和兼容RHEL的环境。CentOS使用yum作为其包管理工具。

        CentOS由于过于稳定,新版本的包一般没有。使用作为操作系统,构建镜像,建议选择更小镜像作为基础镜像。


4. 镜像比较

Alpine通过Dockerfile构建镜像大小为108MB。

Debian通过Dockerfile构建镜像达到了548MB。

前文基于镜像python:3.11.9-alpine3.19构建出的大小为93.9MB。

hello-py git:(main) docker images
REPOSITORY                                      TAG
    IMAGE ID       CREATED          SIZE
hello-py                                        py3-alpine
    7fccb1cfe4df   40 seconds ago   93.9MB


建议通过Docker Hub查找合适的镜像构建自己的镜像,实在找不到,再利用基础镜像构建。

分别拉取Alpine、Debian、Ubuntu、Centos基础镜像。使用docker pull [镜像名称]

docker images
REPOSITORY                                      TAG                       IMAGE ID       CREATED        SIZE
debian                                          latest                    6f4986d78878   2 years ago    124MB
alpine                                          latest                    c059bfaa849c   2 years ago    5.59MB
ubuntu                                          latest                    ba6acccedd29   2 years ago    72.8MB
centos                                          latest                    5d0da3dc9764   2 years ago    231MB


镜像从小到大依次为 alpine(5.59MB)、ubuntu(72.8MB)、debian(124MB)、centos(231MB)。推荐alpine构建镜像,减小构建出的镜像文件大小。

通过使用适用于不同基础镜像的包管理工具,你可以在Dockerfile中安装所需的软件包。这样可以确保在构建镜像时所需的依赖项都被正确安装,从而创建出满足应用程序要求的镜像 

镜像导出

docker save -o /javaDemo/file.tar IMAGE1 IMAGE2 ...
#导出一个
docker save -o /data/app/javaDemo/demo.tar java-demo
docker save IMAGE1 IMAGE2 ... > /javaDemo/file.tar
#举例导出一个
docker save java-demo > /data/app/javaDemo/demo1.tar
#举例导出两个
docker save java-demo nandao/java-demo > /data/app/javaDemo/demo2.tar
#导出为压缩格式
docker save IMAGE1 IMAGE2 ... | gzip > /javaDemo/file.tar.gz

镜像导入

#导入本地镜像
docker load -i /javaDemo/file.tar
docker load < /javaDemo/file.tar.gz

防火墙开

#查看防火墙状态
systemctl status firewalld
#开启防火墙
systemctl start firewalld.service
#添加防火墙端口
firewall-cmd --zone=public --add-port=8081/tcp --permanent
#重启防火墙
systemctl restart firewalld.service
#查看所有开放的端口
firewall-cmd --list-ports
#关闭防火墙
systemctl stop firewalld.service
 

你可能感兴趣的:(docker,debian,ubuntu)