I have a problem. My project is with the Laravel framework, and when I upload it to Docker, the size of the Docker image becomes 6 GB, which is too much. How can I reduce the size of the image?
This is my docker file:
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
The size of this image is about 8 GB!
latest 22513cf13711 2 weeks ago 7.9GB
Changing the docker file to reduce its size
Decreasing the size of Docker Ver Laravel, can anyone help in this regard, how can I change the Docker file so that the size of the image is smaller, it is currently too large.
You have quite much going on in your image, so that's one thing. All the COPY
and ADD
instructions that I did not test amount to like 3GB already, when I comment them out (and a bunch of other stuff because the image does not build otherwise) I get around 4GB of image size.
Apart from those, a sureway to reduce the image size is to not do all of those yum installs separately.
Combine them and do a clean afterwards, like this:
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
With that, I was able to reduce the image size from the 4GB (without ADD and COPY) to 600MB.