docker nginx-php容器镜像瘦身优化

Posted elvis

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker nginx-php容器镜像瘦身优化相关的知识,希望对你有一定的参考价值。

1、 在安装好php环境的容器,参考上面贴出的链接那篇文章的部分,做好基础工作:

#创建工作目录
mkdir /rootfs

#进入工作目录
cd /rootfs

#创建基础目录
mkdir -p bin etc lib proc sys tmp dev/pts usr/lib64 usr/bin usr/local/bin

#创建一个空的DNS服务用的文件
touch etc/resolv.conf

#复制名称解析文件
cp /etc/nsswitch.conf etc/nsswitch.conf

#创建用户和用户组文件
echo root:x:0:0:root:/:/bin/sh > etc/passwd
echo root:x:0: > etc/group

#创建php程序运行的用户
echo www:x:1001:1001:root:/:/bin/sh >> etc/passwd
echo www:x:1001: >> etc/group

#依赖库软链接
ln -s lib lib64

#工具软链接
ln -s bin sbin

#下载busybox,若网速慢可以用迅雷下载,然后复制到/sbin/busybox
curl -o /sbin/busybox http://busybox.net/downloads/binaries/1.21.1/busybox-x86_64

#加权限
chmod +x /sbin/busybox

#安装busybox
cp /sbin/busybox bin
busybox --install -s bin

2、找依赖,假设我的php安装在/usr/local/php中,用交互模式进入容器

运行命令查看依赖:

ldd /usr/local/php/sbin/php-fpm
ldd /usr/local/nginx/sbin/nginx

会列出一大堆依赖库,并且这些库在不同的路径。由于用到busybox,那就将这些库复制到工作目录的根目录的lib即可。即/rootfs/lib/中,用这个工具找出来的是不完整的、还有c++运行库等等。

我整理出来的完整依赖库是:

ldd /usr/local/sbin/php-fpm 
    linux-vdso.so.1 (0x00007fff2cf62000)
    libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fd1cc915000)
    libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007fd1cc6fe000)
    libedit.so.2 => /usr/lib/x86_64-linux-gnu/libedit.so.2 (0x00007fd1cc4c5000)
    libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007fd1cc29b000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fd1cc093000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fd1cbe78000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd1cbb77000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd1cb973000)
    libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007fd1cb75b000)
    libxml2.so.2 => /usr/lib/x86_64-linux-gnu/libxml2.so.2 (0x00007fd1cb3f4000)
    libssl.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 (0x00007fd1cb193000)
    libcrypto.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007fd1cad96000)
    libcurl.so.4 => /usr/lib/x86_64-linux-gnu/libcurl.so.4 (0x00007fd1cab22000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd1ca777000)
    libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007fd1ca567000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd1ca34a000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fd1cd911000)
    liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007fd1ca127000)
    libidn.so.11 => /usr/lib/x86_64-linux-gnu/libidn.so.11 (0x00007fd1c9ef3000)
    librtmp.so.1 => /usr/lib/x86_64-linux-gnu/librtmp.so.1 (0x00007fd1c9cd6000)
    libssh2.so.1 => /usr/lib/x86_64-linux-gnu/libssh2.so.1 (0x00007fd1c9aad000)
    libgssapi_krb5.so.2 => /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2 (0x00007fd1c9862000)
    libkrb5.so.3 => /usr/lib/x86_64-linux-gnu/libkrb5.so.3 (0x00007fd1c958e000)
    libk5crypto.so.3 => /usr/lib/x86_64-linux-gnu/libk5crypto.so.3 (0x00007fd1c935d000)
    libcom_err.so.2 => /lib/x86_64-linux-gnu/libcom_err.so.2 (0x00007fd1c9159000)
    liblber-2.4.so.2 => /usr/lib/x86_64-linux-gnu/liblber-2.4.so.2 (0x00007fd1c8f4a000)
    libldap_r-2.4.so.2 => /usr/lib/x86_64-linux-gnu/libldap_r-2.4.so.2 (0x00007fd1c8cf8000)
    libgnutls-deb0.so.28 => /usr/lib/x86_64-linux-gnu/libgnutls-deb0.so.28 (0x00007fd1c89d9000)
    libhogweed.so.2 => /usr/lib/x86_64-linux-gnu/libhogweed.so.2 (0x00007fd1c87aa000)
    libnettle.so.4 => /usr/lib/x86_64-linux-gnu/libnettle.so.4 (0x00007fd1c8578000)
    libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007fd1c82f5000)
    libgcrypt.so.20 => /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007fd1c8014000)
    libkrb5support.so.0 => /usr/lib/x86_64-linux-gnu/libkrb5support.so.0 (0x00007fd1c7e08000)
    libkeyutils.so.1 => /lib/x86_64-linux-gnu/libkeyutils.so.1 (0x00007fd1c7c04000)
    libsasl2.so.2 => /usr/lib/x86_64-linux-gnu/libsasl2.so.2 (0x00007fd1c79e8000)
    libp11-kit.so.0 => /usr/lib/x86_64-linux-gnu/libp11-kit.so.0 (0x00007fd1c77a2000)
    libtasn1.so.6 => /usr/lib/x86_64-linux-gnu/libtasn1.so.6 (0x00007fd1c758e000)
    libgpg-error.so.0 => /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007fd1c737c000)
    libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007fd1c7173000)
ldd /usr/local/nginx/sbin/nginx 
    linux-vdso.so.1 (0x00007fff3d5dd000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7f01b3f000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7f01922000)
    libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f7f016eb000)
    libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f7f0147d000)
    libssl.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 (0x00007f7f0121c000)
    libcrypto.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007f7f00e1f000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f7f00c04000)
    libxml2.so.2 => /usr/lib/x86_64-linux-gnu/libxml2.so.2 (0x00007f7f0089d000)
    libxslt.so.1 => /usr/lib/x86_64-linux-gnu/libxslt.so.1 (0x00007f7f0065d000)
    libexslt.so.0 => /usr/lib/x86_64-linux-gnu/libexslt.so.0 (0x00007f7f00447000)
    libperl.so.5.20 => /usr/lib/x86_64-linux-gnu/libperl.so.5.20 (0x00007f7f00086000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7effd85000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7eff9da000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f7f01d43000)
    liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007f7eff7b7000)
    libgcrypt.so.20 => /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007f7eff4d6000)
    libgpg-error.so.0 => /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007f7eff2c4000)

复制相应的.so包到rootfs目录下

3、复制php程序

#复制PHP程序
cp -R /usr/local/php  /rootfs/usr/local/php
cp -R /usr/local/nginx /rootfs/usr/local/nginx
cp -R /usr/local/etc /rootfs/usr/local

4、打包tar文件 

tar cf /rootfs.tar .

5、创建DockerFile

#将rootfs.tar 复制出来,其实这个已经是一个基于busybox+nginx+php创建的小型系统了
docker cp ngx01:/rootfs.tar .
FROM scratch
ADD rootfs.tar /
WORKDIR /usr/local/nginx/ptweb
RUN  apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 3B4FE6ACC0B21F32      && apt-get update      && apt-get install supervisor -y      && apt-get install cron -y      && /bin/ln -svf  /usr/share/zoneinfo/Asia/Shanghai /etc/localtime      && echo Asia/Shanghai >/etc/timezone      && ln -svf /usr/local/nginx/sbin/nginx /usr/sbin/nginx      && apt-get autoclean -y      && apt-get clean  -y      && apt-get autoremove -y
EXPOSE 80
ENTRYPOINT ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]

然后build系统

docker build -t ngx-php-supervisor:v01 .

或者使用已经封装过的rootfs.tar:https://github.com/lvelvis/docker-debian-artifacts/releases/download/v1.1/rootfs.tar.xz

以上是关于docker nginx-php容器镜像瘦身优化的主要内容,如果未能解决你的问题,请参考以下文章

Docker项目部署lnmp+wordpress

docker容器composer 部署 laravel框架

docker 镜像瘦身工具 docker-slim

docker 镜像瘦身工具 docker-slim

运维实操——docker容器镜像大小优化

Docker容器:封装镜像+优化