求教这个 DockerFile 如何优化下镜像大小

2021-06-09 11:17:22 +08:00
 kestrelBright
FROM daocloud.io/library/ubuntu:18.04

ENV HOME /home
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No

RUN set -eux; \
    apt-get update; \
    apt-get install -y locales; \
    locale-gen en_US.utf8; \
    rm -rf /var/lib/apt/lists/*
ENV LANG en_US.utf8
ENV LANGUAGE en_US.utf8

RUN set -eux; \
    apt-get update; \
    apt-get install -y apt-file; \
    apt-file update; \
    apt-get install -y software-properties-common; \
    apt-get install -yq --no-install-recommends vim libboost-all-dev gcc-4.8; \
    add-apt-repository -y ppa:ondrej/php; \
    apt-get update -y; \
    apt-get install -y curl wget openssl php7.2 php7.2-bcmath php7.2-cli php7.2-cgi php7.2-common php7.2-dev php7.2-apcu php7.2-geoip php7.2-fpm php7.2-json php7.2-memcached php7.2-memcache php7.2-mysql php7.2-pgsql php7.2-mongo php7.2-sqlite php7.2-sybase php7.2-interbase php7.2-odbc php7.2-ldap php7.2-gmp php7.2-intl php7.2-imagick php7.2-gd php7.2-imap php7.2-curl php7.2-oauth php7.2-redis php7.2-enchant php7.2-xml php7.2-xmlrpc php7.2-tidy php7.2-recode php7.2-readline php7.2-zip php7.2-pspell php7.2-mbstring php7.2-swoole; \
    rm -rf /var/lib/apt/lists/*

# Tidy up
RUN set -eux; \
    apt-get -y autoremove; \
    apt-get clean; \
    apt-get autoclean; \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN mkdir -p /usr/share/nginx/html && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# Allow mounting files
VOLUME ["/usr/share/nginx/html"]
# Set the work directory
WORKDIR /usr/share/nginx/html
CMD ["php-fpm7.2", "-F"]
EXPOSE 9000
2609 次点击
所在节点    Docker
26 条回复
Rwing
2021-06-09 11:29:46 +08:00
用一个现成的 php 基础镜像.....
everyx
2021-06-09 11:52:56 +08:00
ENV 都移到前面,然后合并所有的 RUN 命令
luvmhx
2021-06-09 11:58:40 +08:00
为啥是 Ubuntu…
basefas
2021-06-09 12:03:10 +08:00
用 alpine 做基础镜像,合并 RUN,看看打完包每层的大小,再优化
napsterwu
2021-06-09 12:30:23 +08:00
直接在 https://hub.docker.com/_/php 挑自己要的,又是 install 又是 make 会产生很多用不上的文件
mohuishou
2021-06-09 12:50:53 +08:00
swulling
2021-06-09 12:54:54 +08:00
先加后删的方式没办法降低空间。要把 apt install 和 clean 写一行
BeautifulSoap
2021-06-09 12:58:41 +08:00
lz 你装 php 干嘛手动装? php 官方就有基于 alpine 的镜像,体积只有 20 多 mb,nginx 也通过 alpine 的包管理来添加
Vhc001
2021-06-09 12:59:44 +08:00
docker pull php:fpm-alpine
dolphintwo
2021-06-09 14:59:59 +08:00
alpine 正解
kestrelBright
2021-06-09 15:33:16 +08:00
@BeautifulSoap 学习下呗
kestrelBright
2021-06-09 15:47:12 +08:00
@everyx 只少了 1mb
kestrelBright
2021-06-09 15:58:52 +08:00
@luvmhx 比较好找以前旧的包吧
kestrelBright
2021-06-09 16:05:12 +08:00
@swulling 试过,没区别,还是一样大
```
RUN set -eux; \
apt-get update; \
apt-get install -y locales && apt-get clean; \
locale-gen en_US.utf8; \
apt-get update; \
apt-get install -y apt-file && apt-get clean; \
apt-file update; \
apt-get install -y software-properties-common && apt-get clean; \
apt-get install -yq --no-install-recommends vim libboost-all-dev gcc-4.8 && apt-get clean; \
add-apt-repository -y ppa:ondrej/php; \
apt-get update -y; \
apt-get install -y curl wget openssl php7.2 php7.2-bcmath php7.2-cli php7.2-cgi php7.2-common php7.2-dev php7.2-apcu php7.2-geoip php7.2-fpm php7.2-json php7.2-memcached php7.2-memcache php7.2-mysql php7.2-pgsql php7.2-mongo php7.2-sqlite php7.2-sybase php7.2-interbase php7.2-odbc php7.2-ldap php7.2-gmp php7.2-intl php7.2-imagick php7.2-gd php7.2-imap php7.2-curl php7.2-oauth php7.2-redis php7.2-enchant php7.2-xml php7.2-xmlrpc php7.2-tidy php7.2-recode php7.2-readline php7.2-zip php7.2-pspell php7.2-mbstring php7.2-swoole && apt-get clean; \
```
BeautifulSoap
2021-06-09 16:17:34 +08:00
@kestrelBright
如果你只是想用的话,直接 FROM php:7.2-alpine 就行了,没必要去关心怎么做到的

你如果是想知道该用什么办法把 php 的镜像体积减下来的话,可以去看 php 官方的 alpine 镜像的 Dockerfile 是怎么构建的就行了。php 官方的 alpine 镜像大小只有 20 多 MB 大小,只不过构建过程很复杂,不一定能学得来。。。。。

https://github.com/docker-library/php/tree/master/7.3/alpine3.13/fpm
kestrelBright
2021-06-09 16:20:57 +08:00
@mohuishou 不大明白怎么拷贝前一阶段的,是这样吗?
```
FROM daocloud.io/library/ubuntu:18.04 as php72

ENV HOME /home
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
ENV LANG en_US.utf8
ENV LANGUAGE en_US.utf8

RUN set -eux; \
apt-get update; \
apt-get install -y locales; \
locale-gen en_US.utf8; \
apt-get update; \
apt-get install -y apt-file; \
apt-file update; \
apt-get install -y software-properties-common; \
apt-get install -yq --no-install-recommends vim libboost-all-dev gcc-4.8; \
add-apt-repository -y ppa:ondrej/php; \
apt-get update -y; \
apt-get install -y curl wget openssl php7.2 php7.2-bcmath php7.2-cli php7.2-cgi php7.2-common php7.2-dev php7.2-apcu php7.2-geoip php7.2-fpm php7.2-json php7.2-memcached php7.2-memcache php7.2-mysql php7.2-pgsql php7.2-mongo php7.2-sqlite php7.2-sybase php7.2-interbase php7.2-odbc php7.2-ldap php7.2-gmp php7.2-intl php7.2-imagick php7.2-gd php7.2-imap php7.2-curl php7.2-oauth php7.2-redis php7.2-enchant php7.2-xml php7.2-xmlrpc php7.2-tidy php7.2-recode php7.2-readline php7.2-zip php7.2-pspell php7.2-mbstring php7.2-swoole; \
apt-get -y autoremove; \
apt-get clean; \
apt-get autoclean; \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*; \
mkdir -p /usr/share/nginx/html && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

FROM daocloud.io/library/ubuntu:18.04
COPY --from=php72 /usr /usr

VOLUME ["/usr/share/nginx/html"]
WORKDIR /usr/share/nginx/html
CMD ["php-fpm7.2", "-F"]
EXPOSE 9000
```
kestrelBright
2021-06-09 16:24:34 +08:00
@BeautifulSoap 就是学下怎么减小体积
mohuishou
2021-06-09 16:26:01 +08:00
@kestrelBright 类似 PHP 的脚本语言,一般不需要做多阶段,选个小基础镜像就可以有效减少大小了
kestrelBright
2021-06-09 16:31:40 +08:00
@swulling no-install-recommends 有效,少了 100mb
THESDZ
2021-06-09 16:47:02 +08:00
build 阶段可以单独放一个 docker 镜像,然后从这个临时镜像复制
php 我不懂,找个 nodejs 的你看看


FROM greencloudcomputing/nodejs14 AS builder
WORKDIR /build
COPY ./ .
RUN cd /build && npm --registry https://registry.npm.taobao.org install && npm run build

FROM nginx:stable-alpine
RUN apk add --repository https://mirrors.aliyun.com/alpine/v3.4/main/ --update curl && rm -rf /var/cache/apk/*
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /build/dist /usr/share/nginx/html/

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/782347

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX