一、nginx

FROM centos:7 as build#基于centos7镜像MAINTAINER nginx on centos7 by lxy-20240125#注释信息ADD nginx-1.24.0.tar.gz /opt/#将nginx安装包传输到镜像中RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make && \cd /opt/nginx-1.24.0 && \./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && \make && make install#下载编译安装nginx的依赖环境,以及创建nginx用户,进入nginx目录中,完成配置--编译--安装ENV PATH=$PATH:/usr/local/nginx/sbin/#创建镜像的环境变量ADD nginx.conf /usr/local/nginx/conf/nginx.conf#将配置文件传输的镜像中,覆盖原有的nginx.conf文件RUN chmod 777 -R /usr/local/nginx/html/#修改权限 FROM centos:7#再次基于centos7COPY --from=build /usr/local/nginx /usr/local/nginx#把第一阶段的安装目录复制到第二阶段RUN useradd -M -s /sbin/nologin nginx#必须的有个nginx用户EXPOSE 80#暴露80端口CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]#设置启动命令

二、php

FROM centos:7#基础镜像为centos7MAINTAINER this is php image #注释信息ADD php-7.1.10.tar.bz2 /opt/#将文件传输到镜像中RUN yum -y installgd libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel gcc gcc-c++ make pcre-develgcc gcc-c++ make pcre-devel && \yum clean all && \cd /opt/php-7.1.10 && \ ./configure \--prefix=/usr/local/php \--with-mysql-sock=/usr/local/mysql/mysql.sock \--with-mysqli \--with-zlib \--with-curl \--with-gd \--with-jpeg-dir \--with-png-dir \--with-freetype-dir \--with-openssl \--enable-fpm \--enable-mbstring \--enable-xml \--enable-session \--enable-ftp \--enable-pdo \--enable-tokenizer \--enable-zip && make -j 2&& make install && \useradd -M -s /sbin/nologin nginx#通过RUN依次下载php的依赖环境,清理yum缓存#进入文件,配置--编译--安装#创建nginx用户COPY php.ini/usr/local/php/lib/php.iniCOPY php-fpm.conf /usr/local/php/etc/php-fpm.confCOPY www.conf /usr/local/php/etc/php-fpm.d/www.conf#将三个配置文件传输到镜像中 FROM centos:7RUN yum -y installgd libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel gcc gcc-c++ make pcre-develgcc gcc-c++ make pcre-devel && \yum clean all && \useradd -M -s /sbin/nologin nginxCOPY --from=0 /usr/local/php /usr/local/phpEXPOSE 9000#暴露9000端口CMD [ "/usr/local/php/sbin/php-fpm", "-F" ]#启动

三、mysql

FROM centos:7#基于centos7镜像MAINTAINER this is mysql image #注释信息ADD mysql-boost-5.7.44.tar.gz /opt/#将安装包和配置文件传输到镜像中RUN yum -y install make gcc gcc-c++ ncurses ncurses-devel bison cmake openssl-devel && \ yum clean all && \ cd /opt/mysql-5.7.44/ && \cmake \-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \-DSYSCONFDIR=/etc \-DSYSTEMD_PID_DIR=/usr/local/mysql \-DDEFAULT_CHARSET=utf8\-DDEFAULT_COLLATION=utf8_general_ci \-DWITH_EXTRA_CHARSETS=all \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DMYSQL_DATADIR=/usr/local/mysql/data \-DWITH_BOOST=boost \-DWITH_SYSTEMD=1 && make -j 2 && make install#依次安装依赖环境,注意新版本必须要openssl环境,注意提示报错查看#清理yum缓存#进入mysql解压包中,完成cmake--编译--安装FROM centos:7COPY --from=0 /usr/local/mysql /usr/local/mysqlCOPY my.cnf /etc/my.cnfRUN useradd -M -s /sbin/nologinmysql && \chown mysql:mysql /etc/my.cnf && \chown -R mysql:mysql /usr/local/mysql/ &&\/usr/local/mysql/bin/mysqld \--initialize-insecure \--user=mysql \--basedir=/usr/local/mysql \--datadir=/usr/local/mysql/data#创建一个mysql用户#修改配置文件的权限#修改安装目录的权限#直接用命令启动mysqlENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH#配置环境变量EXPOSE 3306#暴露端口CMD ["/usr/local/mysql/bin/mysqld"]#设置默认启动命令

四、缩小镜像体积的方法有哪些?

1)尽量使用体积小的基础镜像,比如alpine,这是专门为容器创建的;

2)尽量减少dockerfile的指令,因为每一条指令对应一层文件系统;(镜像是一层一层文件系统叠加的整体文件系统,对外展现为一个整体);

3)RUN指令的最后可以清理一下yum缓存,清理一下安装包等;

4)COPY尽量放在RUN后面,docker可以更好使用缓存功能;

5)使用.dockerignore文件,将不复制的文件定义在文件中;

6)容器镜像里一般只运行单个应用,这样体积更小,也更好管理;

7)使用多阶段构建;