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
1
Rwing 2021-06-09 11:29:46 +08:00
用一个现成的 php 基础镜像.....
|
2
everyx 2021-06-09 11:52:56 +08:00 via Android
ENV 都移到前面,然后合并所有的 RUN 命令
|
3
luvmhx 2021-06-09 11:58:40 +08:00 via iPhone
为啥是 Ubuntu…
|
4
basefas 2021-06-09 12:03:10 +08:00
用 alpine 做基础镜像,合并 RUN,看看打完包每层的大小,再优化
|
5
napsterwu 2021-06-09 12:30:23 +08:00 via iPhone
直接在 https://hub.docker.com/_/php 挑自己要的,又是 install 又是 make 会产生很多用不上的文件
|
6
mohuishou 2021-06-09 12:50:53 +08:00
|
7
swulling 2021-06-09 12:54:54 +08:00 via iPhone
先加后删的方式没办法降低空间。要把 apt install 和 clean 写一行
|
8
BeautifulSoap 2021-06-09 12:58:41 +08:00
lz 你装 php 干嘛手动装? php 官方就有基于 alpine 的镜像,体积只有 20 多 mb,nginx 也通过 alpine 的包管理来添加
|
9
Vhc001 2021-06-09 12:59:44 +08:00
docker pull php:fpm-alpine
|
10
dolphintwo 2021-06-09 14:59:59 +08:00
alpine 正解
|
11
kestrelBright OP @BeautifulSoap 学习下呗
|
12
kestrelBright OP @everyx 只少了 1mb
|
13
kestrelBright OP @luvmhx 比较好找以前旧的包吧
|
14
kestrelBright OP @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; \ ``` |
15
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 |
16
kestrelBright OP @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 ``` |
17
kestrelBright OP @BeautifulSoap 就是学下怎么减小体积
|
18
mohuishou 2021-06-09 16:26:01 +08:00
@kestrelBright 类似 PHP 的脚本语言,一般不需要做多阶段,选个小基础镜像就可以有效减少大小了
|
19
kestrelBright OP @swulling no-install-recommends 有效,少了 100mb
|
20
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/ |
21
maxbon 2021-06-09 16:51:45 +08:00
ENV 放一起,RUN 放一起,理论上 RUN 只能有一条,多了就大很多,另外有 apt-get 操作的话,最后一步清缓存,RUN 的最后一步永远是删临时文件和清缓存
|
22
everyx 2021-06-09 16:54:22 +08:00 via Android
@kestrelBright 那就只能修改 base image 了
|
23
kestrelBright OP @everyx base image 很小也,只有 63.3mb ,我最开始的 DockerFile 弄出来镜像 1.5g ,现在 899mb
|
24
kestrelBright OP @mohuishou 基础镜像只有 63mb 也,能请教下这个怎么在 build 阶段从临时镜像复制不?是不是就是把安装好 php 和那些扩展复制到基础镜像?
|
25
kestrelBright OP @maxbon 嗯嗯,这样可以减小大概 1/3
|
26
maxbon 2021-06-10 13:51:57 +08:00
@kestrelBright 你拿 ubuntu 做基础镜像就离谱,不过既然一定要用,可以卸载掉一些没用的服务
|