我有一个问题。我的项目使用Laravel框架,当我将其上传到Docker时,Docker镜像的大小变成了6GB,这太大了。如何缩小图像尺寸?
这是我的 docker 文件:
FROM centos:7.7.1908
MAINTAINER ADA
##############################################
# installing required stuff
##############################################
RUN yum -y update
RUN yum install -y epel-release \
&& rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
RUN yum install -y unzip libaio-dev libmcrypt-dev curl nano nodejs wget htop git yum-utils mod_ssl
##############################################
# apache(Httpd) Install
##############################################
RUN yum install -y httpd
RUN yum install -y firewalld
##############################################
# apache enable mod security
# for hide name server apache in response
##############################################
RUN yum install -y mod_security
##############################################
# PHP Install
##############################################
RUN yum --enablerepo=remi-php74 install -y php
RUN yum --enablerepo=remi-php74 install -y php-xml php-soap php-pdo php-mysql php-xmlrpc php-mbstring php-json php-gd php-mcrypt php-oci php-fpm php-zip php-xdebug php-pear php-devel
##############################################
# install php extention bcmath
##############################################
#RUN yum search bcmath
RUN yum install -y php74-php-bcmath.x86_64
RUN cp /etc/opt/remi/php74/php.d/20-bcmath.ini /etc/php.d/
RUN cp /opt/remi/php74/root/usr/lib64/php/modules/bcmath.so /usr/lib64/php/modules/
##############################################
# PHP composer
##############################################
#For php74
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
RUN composer self-update --1
##############################################
# Oracle instantclient install oci8 extention
##############################################
ADD oracle/21.1.0.0.0-1/oracle-instantclient-basic-21.1.0.0.0-1.x86_64.rpm /tmp/
ADD oracle/21.1.0.0.0-1/oracle-instantclient-devel-21.1.0.0.0-1.x86_64.rpm /tmp/
ADD oracle/21.1.0.0.0-1/oracle-instantclient-sqlplus-21.1.0.0.0-1.x86_64.rpm /tmp/
# install oci8 by rpm file
RUN yum install -y /tmp/oracle-instantclient-basic-21.1.0.0.0-1.x86_64.rpm
RUN yum install -y /tmp/oracle-instantclient-devel-21.1.0.0.0-1.x86_64.rpm
RUN yum install -y /tmp/oracle-instantclient-sqlplus-21.1.0.0.0-1.x86_64.rpm
RUN ldconfig
RUN echo "export PHP_DTRACE=yes" >> /root/.bash_profile
RUN export PHP_DTRACE=yes
RUN echo "export ORACLE_HOME=/usr/lib/oracle/21.1/client64" >> /root/.bash_profile
RUN export ORACLE_HOME=/usr/lib/oracle/21.1/client64
RUN echo "export PATH=$PATH:$ORACLE_HOME/bin" >> /root/.bash_profile
RUN export PATH=$PATH:$ORACLE_HOME/bin
RUN echo "export LD_LIBRARY_PATH=$ORACLE_HOME/lib" >> /root/.bash_profile
RUN export LD_LIBRARY_PATH=$ORACLE_HOME/lib
RUN echo "export TNS_ADMIN=$ORACLE_HOME/lib/network/admin" >> /root/.bash_profile
RUN export TNS_ADMIN=$ORACLE_HOME/lib/network/admin
RUN yum-config-manager --enable remi-php74
RUN yum install php php-oci8 -y
##############################################
# supervisor and queue in laravel
##############################################
RUN yum install supervisor -y
##############################################
# install reqiure for dusk (chrome browser)
##############################################
RUN yum install -y epel-release
RUN yum install -y chromium
##############################################
# wkhtmltopdf install
##############################################
RUN yum install -y libpng libjpeg openssl icu libX11 libXext libXrender xorg-x11-fonts-Type1 xorg-x11-fonts-75dpi
ADD wkhtmltoxpdf/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz /tmp/
#RUN tar -xvf /tmp/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
RUN mv /tmp/wkhtmltox/bin/wkhtmltopdf /usr/bin/wkhtmltopdf
RUN mv /tmp/wkhtmltox/bin/wkhtmltoimage /usr/bin/wkhtmltoimage
RUN wkhtmltopdf -V
RUN wkhtmltoimage -V
#RUN wkhtmltopdf https://google.com /tmp/output_pdf_file_name.pdf
#RUN wkhtmltoimage https://google.com /tmp/output_image_file_name.jpg
##############################################
# run crond linux
##############################################
RUN yum -y install crontabs cronie
RUN sed -i -e '/pam_loginuid.so/s/^/#/' /etc/pam.d/crond
RUN chmod 0755 /etc/crontab
RUN crontab /etc/crontab
##############################################
# Security Config For Apache And Upload
##############################################
# upload security
COPY conf/apache/security_apache/mod_security.conf /etc/httpd/conf.d/mod_security.conf
##############################################
# config apache /-FPM/FastCGI /- PHP.ini /-DNS
##############################################
# CONFIG - DNS DYNAMIC RECORD
COPY dns/resolv.conf /etc/resolv.conf
# CONFIG - hosts DYNAMIC RECORD
COPY hosts/hosts /etc/
# CONFIG - php.ini DYNAMIC CONFIG
COPY conf/php/php.ini /etc/php.ini
# CONFIG - APACHE DYNAMIC MPM And FPM
RUN mkdir /run/php-fpm
COPY conf/apache/mpm-mod/00-mpm.conf /etc/httpd/conf.modules.d/00-mpm.conf
COPY conf/apache/FPM_FastCGI/fpm_config/php.conf /etc/httpd/conf.d/php.conf
COPY conf/apache/FPM_FastCGI/www_conf/www.conf /etc/php-fpm.d/www.conf
# CONFIG - APACHE DYNAMIC CONFIG
COPY conf/apache/config_httpd_and_enable_htaccess/httpd.conf /etc/httpd/conf/httpd.conf
# CONFIG - SUPERVISOR DYNAMIC
COPY bootstrap /
COPY supervisord_and_queue/supervisord.conf /etc/supervisord.conf
# SUPERVISOR - laravel-queue.ini
COPY supervisord_and_queue/ini/laravel-queue.ini /etc/supervisord.d/laravel-queue.ini
# CronJab linux
COPY crontab_and_cronjob/crontab /etc/crontab
##############################################
# Directory
##############################################
WORKDIR /var/www/html
##############################################
# ENTRYPOINT && CMD
##############################################
# run supervisord for queue & php-fpm & tomcat & crond
RUN chmod 0755 /bootstrap.sh
ENTRYPOINT crontab /etc/crontab; apachectl; php-fpm; /usr/bin/php /var/www/html/artisan deploy; /bootstrap.sh
##############################################
# FOREGROUND
##############################################
# run FOREGROUND apache
CMD ["httpd", "-D", "FOREGROUND"]
##############################################
# Expose
##############################################
EXPOSE 80
这张图片的大小约为8GB!
latest 22513cf13711 2 weeks ago 7.9GB
更改 docker 文件以减小其大小
减小 Docker Ver Laravel 的大小,任何人都可以在这方面提供帮助,我如何更改 Docker 文件以使图像的大小更小,目前太大了。
你的形象中有很多变化,所以这是一回事。我没有测试的所有
COPY
和 ADD
指令已经达到了 3GB 左右,当我将它们注释掉(以及一堆其他东西,因为图像不会以其他方式构建)时,我得到了大约 4GB 的图像大小。
除此之外,减小映像大小的一个可靠方法是不要单独进行所有这些 yum 安装。
将它们组合起来,然后进行清洁,如下所示:
RUN yum -y update && yum install -y \
epel-release \
unzip \
...
xorg-x11-fonts-75dpi \
crontabs \
cronie \
&& rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm \
&& yum -y clean all && rm -fr /var/cache
这样,我就能够将图像大小从 4GB(没有 ADD 和 COPY)减少到 600MB。