不同基础镜像docker构建及对比


目录
  1. 1. 以alpine为基础镜像
  2. 2. 以ubuntu为基础镜像
  3. 3. 以debian为基础镜像
  4. 4. 对比
  5. 5. 结论

前文构建了一个镜像,发现dockerfile里有些用不到的数据库和php扩展,本文试着用不同方式构建镜像。

以alpine为基础镜像

Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
FROM php:7.3-fpm-alpine3.11

# 1. 使用国内镜像源加速(可选)
RUN echo "https://mirrors.aliyun.com/alpine/v3.11/main" > /etc/apk/repositories && \
echo "https://mirrors.aliyun.com/alpine/v3.11/community" >> /etc/apk/repositories

# 2. 仅安装必要软件(Nginx,删除所有编译工具)
RUN apk update && \
apk add --no-cache nginx && \
mkdir /run/nginx

# 3. 直接使用默认配置(删除不必要的扩展和自定义配置)
# 注:默认 php.ini 已足够,无需覆盖

# 4. 添加最小化文件
COPY index.php /var/www/html/
COPY default.conf /etc/nginx/conf.d/
COPY run.sh /

# 5. 设置权限并清理
RUN chmod +x /run.sh && \
rm -rf /var/cache/apk/*

# 6. 暴露端口
EXPOSE 80

# 7. 启动命令
ENTRYPOINT ["/run.sh"]

default.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 80;
server_name localhost;

root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;

location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

run.sh:

1
2
3
4
5
#!/bin/sh
# 后台启动
php-fpm -D
# 关闭后台启动,hold住进程
nginx -g 'daemon off;'

文件结构:
├── Dockerfile
├── default.conf
├── index.php
└── run.sh
构建过程输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
root@onecloud:~/php/php-alpine# docker build -t qs/php-alpine:v1 .
[+] Building 16.3s (12/12) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.3s
=> => transferring dockerfile: 814B 0.1s
=> [internal] load metadata for docker.io/library/php:7.3-fpm-alpine3.11 1.9s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> CACHED [1/7] FROM docker.io/library/php:7.3-fpm-alpine3.11@sha256:b0d07b0ccb6847add12a222025066d406a55f055acfce7 0.0s
=> [internal] load build context 0.2s
=> => transferring context: 74.37kB 0.1s
=> [2/7] RUN echo "https://mirrors.aliyun.com/alpine/v3.11/main" > /etc/apk/repositories && echo "https://mirro 2.7s
=> [3/7] RUN apk update && apk add --no-cache nginx && mkdir /run/nginx 6.3s
=> [4/7] COPY index.php /var/www/html/ 0.5s
=> [5/7] COPY default.conf /etc/nginx/conf.d/ 0.5s
=> [6/7] COPY run.sh / 0.5s
=> [7/7] RUN chmod +x /run.sh && rm -rf /var/cache/apk/* 1.2s
=> exporting to image 1.6s
=> => exporting layers 1.4s
=> => writing image sha256:4753ea1ce06772744ecacaec680891a2211f3b55f797860c1132449b160fd1f7 0.0s
=> => naming to docker.io/qs/php-alpine:v1

以ubuntu为基础镜像

Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 # 使用官方Ubuntu镜像
FROM ubuntu:22.04

# 设置非交互式环境
ENV DEBIAN_FRONTEND=noninteractive

# 创建工作目录
RUN mkdir -p /var/www/html

# 安装必要软件
RUN apt-get update && \
apt-get install -y --no-install-recommends \
nginx \
php-fpm \
&& \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# 复制文件(使用明确路径)
COPY index.php /var/www/html/
COPY default.conf /etc/nginx/conf.d/
COPY run.sh /

# 设置权限
RUN chmod +x /run.sh && \
chown -R www-data:www-data /var/www/html

# 清理默认配置
RUN rm -f /etc/nginx/sites-enabled/default

# 暴露端口
EXPOSE 80

# 启动命令
CMD ["/run.sh"]

default.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
}

run.sh:

1
2
3
4
5
#!/bin/sh
# 后台启动
service php8.1-fpm start # 根据实际 PHP 版本调整
# 关闭后台启动,hold住进程
nginx -g 'daemon off;'

构建过程输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
root@onecloud:~/php/php-ubuntu# docker build -t qs/php-ubuntu:v1 .
[+] Building 335.9s (13/13) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.2s
=> => transferring dockerfile: 712B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:22.04 1.8s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load build context 0.7s
=> => transferring context: 90B 0.0s
=> [1/8] FROM docker.io/library/ubuntu:22.04@sha256:d80997daaa3811b175119350d84305e1ec9129e1799bba0bd1e3120da3ff52 58.5s
=> => resolve docker.io/library/ubuntu:22.04@sha256:d80997daaa3811b175119350d84305e1ec9129e1799bba0bd1e3120da3ff52c 0.3s
=> => sha256:d80997daaa3811b175119350d84305e1ec9129e1799bba0bd1e3120da3ff52c3 6.69kB / 6.69kB 0.0s
=> => sha256:4f6f9af1518bf360df5bd6214cd573d6dc0143334234757ed745715f6a95d0cd 424B / 424B 0.0s
=> => sha256:9aa01b7cb660ca7978b749d3ddd2ce2bf9495d1556820e724c091be026c33ad4 2.31kB / 2.31kB 0.0s
=> => sha256:f3e10eb95482b14d0f5a969fafb71afd541d5efd38890d551e27c957ed3ae84e 26.64MB / 26.64MB 30.0s
=> => extracting sha256:f3e10eb95482b14d0f5a969fafb71afd541d5efd38890d551e27c957ed3ae84e 22.7s
=> [2/8] RUN mkdir -p /var/www/html 6.2s
=> [3/8] RUN apt-get update && apt-get install -y --no-install-recommends nginx php-fpm && ap 235.0s
=> [4/8] COPY index.php /var/www/html/ 0.5s
=> [5/8] COPY default.conf /etc/nginx/conf.d/ 0.4s
=> [6/8] COPY run.sh / 0.3s
=> [7/8] RUN chmod +x /run.sh && chown -R www-data:www-data /var/www/html 2.1s
=> [8/8] RUN rm -f /etc/nginx/sites-enabled/default 1.5s
=> exporting to image 28.3s
=> => exporting layers 28.0s
=> => writing image sha256:c53001d8d966e2e8951c57d26a7c90477eecae863ead67dd13071f93475ef8af 0.0s
=> => naming to docker.io/qs/php-ubuntu:v1

以debian为基础镜像

只有Dockerfile第一行与以ubuntu为基础镜像的Dockerfile不同:

1
2
 # 使用 Debian 官方 slim 镜像(比 Ubuntu 更小)
FROM debian:11-slim

构建过程输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
root@onecloud:~/php/php-debian# docker build -t qs/php-debian .
[+] Building 275.6s (13/13) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 745B 0.0s
=> [internal] load metadata for docker.io/library/debian:11-slim 1.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/8] FROM docker.io/library/debian:11-slim@sha256:7aafeb23eaef5d5b1de26e967b9a78f018baaac81dd75246b99781eaaa2d 32.4s
=> => resolve docker.io/library/debian:11-slim@sha256:7aafeb23eaef5d5b1de26e967b9a78f018baaac81dd75246b99781eaaa2d5 0.1s
=> => sha256:7aafeb23eaef5d5b1de26e967b9a78f018baaac81dd75246b99781eaaa2d59ef 4.54kB / 4.54kB 0.0s
=> => sha256:b04da799ff9e4e278427561460ab9a95eaa9264e67cde09b1eb306e5c2bc3f7f 1.04kB / 1.04kB 0.0s
=> => sha256:9c163ad39b7cf022fcd764b33bcf677bfdfc4afb538d049c49c8e2cb2d76eacb 466B / 466B 0.0s
=> => sha256:bfc445187b87c4f640fe8b85c4ee3c251ce5e7023a5ff0acd053bde1f01e6aaf 25.54MB / 25.54MB 9.3s
=> => extracting sha256:bfc445187b87c4f640fe8b85c4ee3c251ce5e7023a5ff0acd053bde1f01e6aaf 19.3s
=> [internal] load build context 0.1s
=> => transferring context: 246B 0.0s
=> [2/8] RUN mkdir -p /var/www/html 4.5s
=> [3/8] RUN apt-get update && apt-get install -y --no-install-recommends nginx php-fpm && ap 207.7s
=> [4/8] COPY index.php /var/www/html/ 0.5s
=> [5/8] COPY default.conf /etc/nginx/conf.d/ 0.3s
=> [6/8] COPY run.sh / 0.3s
=> [7/8] RUN chmod +x /run.sh && chown -R www-data:www-data /var/www/html 3.3s
=> [8/8] RUN rm -f /etc/nginx/sites-enabled/default 1.8s
=> exporting to image 22.8s
=> => exporting layers 22.3s
=> => writing image sha256:dfc71ebf160c92153b8974ee4c86624ad4a8a2652155a049cd3f3b942be77605 0.0s
=> => naming to docker.io/qs/php-debian

对比

基础镜像 构建时间 镜像大小
alpine 16s 64M
ubuntu 336s 187M
debian 276s 164M


镜像体积对比

镜像 基础体积 安装 Nginx+PHP 后体积 特点
alpine ~5MB ~50MB 最小,适合极致轻量化
debian (slim) ~50MB ~100MB 平衡体积与兼容性
ubuntu ~70MB ~150MB 最大,但工具链最完整

结论

  • 追求最小体积 → alpine
  • 平衡选择 → debian slim
  • 需要完整工具链 → ubuntu