经过多次尝试整理我的laravel docker部署,我已经带来了下面的Dockerfile。但是,我的镜像仍然超过2Gb,这是巨大的。有什么我可以做的来优化我的最终镜像大小吗?这里是我的Dockerfile
FROM alpine:latest
WORKDIR /var/www/html/
# Essentials
RUN apk add --no-cache tzdata
ENV TZ=Asia/Jakarta
RUN apk add --no-cache zip unzip curl nginx supervisor
# Installing bash
RUN apk add bash
RUN sed -i 's/bin\/ash/bin\/bash/g' /etc/passwd
# Installing PHP
RUN apk add --no-cache php82 \
php82-common \
php82-fpm \
php82-pdo \
php82-opcache \
php82-zip \
php82-gd \
php82-phar \
php82-iconv \
php82-cli \
php82-curl \
php82-openssl \
php82-mbstring \
php82-exif \
php82-tokenizer \
php82-fileinfo \
php82-json \
php82-xml \
php82-xmlreader \
php82-xmlwriter \
php82-simplexml \
php82-dom \
php82-pdo_mysql \
php82-tokenizer \
php82-pecl-redis
RUN ln -s /usr/bin/php82 /usr/bin/php
# Installing composer
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN rm -rf composer-setup.php
RUN apk add nodejs yarn
# Configure supervisor
RUN mkdir -p /etc/supervisor.d/
COPY ./docker/supervisor/supervisord.ini /etc/supervisor.d/supervisord.ini
# Configure PHP
RUN mkdir -p /run/php/
RUN touch /run/php/php8.2-fpm.pid
COPY ./docker/php/php-fpm.conf /etc/php82/php-fpm.conf
COPY ./docker/php/php.ini-production /etc/php82/php.ini
# Configure nginx
COPY ./docker/nginx/nginx.conf /etc/nginx/
COPY ./docker/nginx/webserver.conf /etc/nginx/http.d/default.conf
RUN mkdir -p /run/nginx/
RUN touch /run/nginx/nginx.pid
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
# Building process
COPY . .
RUN composer install
RUN chown -R nobody:nobody /var/www/html/storage
RUN yarn
# Run a cron job
ADD ./docker/cron/crontab.txt /crontab.txt
RUN /usr/bin/crontab /crontab.txt
# add log for supervisor laravel worker
RUN touch /var/www/html/storage/logs/worker.log
# Generate Laravel app encryption key
RUN cp .env.example .env
RUN php artisan key:generate --ansi
RUN php artisan vendor:publish --all
RUN chown -R nginx:nginx /var/www/html -v
RUN chmod -R 777 /var/www/html -v
RUN chown -R nginx:nginx /var/lib/nginx -v
RUN chmod -R 755 /var/lib/nginx -v
RUN chmod -R 755 /var/log/nginx -v
# Exposing port 80 (http)
EXPOSE 80
# Auto start supervisor on start
CMD ["/usr/bin/supervisord"]
字符串
我之前试过做多阶段,但结果图像仍然相当大。我错过了创建docker文件的一些关键步骤吗?
3条答案
按热度按时间eqfvzcg81#
.dockerignore
文件来确保只有必要的文件被添加到上下文,从而添加到图像。以下是
Dockerfile
的修改版本:字符串
8xiog9wr2#
我会补充上面的答案。我也会把所有与nginx相关的东西放在一个单独的容器中。
同时也考虑所有只需要组装的软件包,并在以后删除它们
字符串
jobtbby33#
经过一番研究和思考,我找到了减少图像大小的最佳方法。正如@Denis Sinyukov所说,将Dockerfile分解为多个阶段确实有助于减少图像大小,但更重要的是,如果您的应用程序具有前端/全栈laravel应用程序,在您完成构建前端样式/JavaScript或其他之后,你应该删除node_module。在这里我完全摆脱前端构建阶段,然后继续php的东西
字符串