在docker中部署laravel app有更优雅的方法吗

gdx19jrr  于 2023-11-17  发布在  Docker
关注(0)|答案(3)|浏览(123)

经过多次尝试整理我的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文件的一些关键步骤吗?

eqfvzcg8

eqfvzcg81#

  • 每个RUN命令都会在Docker镜像中添加一个新层。请尽可能将相关命令合并到一个RUN语句中。
  • 评估是否每个用COPY或ADD添加的文件都是必要的,或者它们是否可以被压缩。使用.dockerignore文件来确保只有必要的文件被添加到上下文,从而添加到图像。

以下是Dockerfile的修改版本:

FROM alpine:latest as builder

WORKDIR /var/www/html/

RUN apk add --no-cache tzdata \
    zip \
    unzip \
    curl \
    nginx \
    supervisor \
    bash \
    nodejs \
    yarn \
    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-pecl-redis && \
    ln -s /usr/bin/php82 /usr/bin/php && \
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

COPY ./docker/php/php-fpm.conf /etc/php82/php-fpm.conf
COPY ./docker/php/php.ini-production /etc/php82/php.ini
COPY ./docker/nginx/nginx.conf /etc/nginx/
COPY ./docker/nginx/webserver.conf /etc/nginx/http.d/default.conf
COPY ./docker/supervisor/supervisord.ini /etc/supervisor.d/supervisord.ini
COPY . .

RUN composer install --no-dev --optimize-autoloader && \
    yarn --production && \
    yarn cache clean && \
    rm -rf node_modules

FROM alpine:latest

WORKDIR /var/www/html/

COPY --from=builder /var/www/html /var/www/html
COPY --from=builder /etc/php82 /etc/php82
COPY --from=builder /etc/nginx /etc/nginx
COPY --from=builder /etc/supervisor.d/supervisord.ini /etc/supervisor.d/supervisord.ini
COPY --from=builder /usr/local/bin/composer /usr/local/bin/composer
COPY --from=builder /usr/bin/php /usr/bin/php

RUN mkdir -p /run/php/ && \
    mkdir -p /run/nginx/ && \
    touch /run/php/php8.2-fpm.pid && \
    touch /run/nginx/nginx.pid && \
    ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log && \
    chown -R nginx:nginx /var/www/html && \
    chmod -R 755 /var/www/html

RUN chown -R nobody:nobody /var/www/html/storage && \
    chown -R nginx:nginx /var/lib/nginx && \
    chmod -R 755 /var/lib/nginx && \
    chmod -R 755 /var/log/nginx

EXPOSE 80

CMD ["/usr/bin/supervisord"]

字符串

8xiog9wr

8xiog9wr2#

我会补充上面的答案。我也会把所有与nginx相关的东西放在一个单独的容器中。
同时也考虑所有只需要组装的软件包,并在以后删除它们

RUN set -ex \
&& apk add --no-cache --virtual .build-deps \
&& .... all dev packages ...
.......................
&& apk del .build-deps \
&& rm -rf /tmp/* /var/cache/apk/*

字符串

jobtbby3

jobtbby33#

经过一番研究和思考,我找到了减少图像大小的最佳方法。正如@Denis Sinyukov所说,将Dockerfile分解为多个阶段确实有助于减少图像大小,但更重要的是,如果您的应用程序具有前端/全栈laravel应用程序,在您完成构建前端样式/JavaScript或其他之后,你应该删除node_module。在这里我完全摆脱前端构建阶段,然后继续php的东西

FROM node:18.3.0-alpine as FrontendStage

COPY . /var/www/html
WORKDIR /var/www/html/

# Essentials
RUN apk add --no-cache tzdata
ENV TZ=Asia/Jakarta

RUN npm install
RUN npm run production
RUN npm run tailwind-production
RUN rm -rf /var/www/html/node_modules

FROM alpine:latest as BuildStage

WORKDIR /var/www/html/
COPY --from=FrontendStage /var/www/html /var/www/html/

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

# 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 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 php artisan storage:link

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"]

字符串

相关问题