×

COPY --from 的奇妙用法

Falcon 2023-12-08 views:
自动摘要

正在生成中……

这哥们的构建很有意思,他是把php composer安装的依赖作为单独一层进行了构建,然后也可以同样把前端node安装好的依赖作为另外单独一层,最后从这些构建好的镜像把文件复制过来就可以了。

核心命令是 COPY FROM 官方说明:https://docs.docker.com/build/building/multi-stage/ 比如:

COPY --from=vendor app/vendor/ ./vendor/

类似的操作是早前我也接触了一种在构建镜像时安装php composer的方法,好处是不用自己维护composer.

COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

This is how i do it with Laravel 8.4 in 2021 to deploy it to CloudRun in Google Cloud:

Dockerfile

#Get Composer
FROM composer:2.0 as vendor

WORKDIR /app

COPY database/ database/
COPY composer.json composer.json
COPY composer.lock composer.lock

RUN composer install \
    --no-interaction \
    --no-plugins \
    --no-scripts \
    --no-dev \
    --prefer-dist

COPY . .
RUN composer dump-autoload

// some more custom steps like


FROM node:14.9 as frontend
...
FROM php:7.4-fpm
...

Copy Composer dependencies



# Copy Composer dependencies
COPY --from=vendor app/vendor/ ./vendor/
COPY . .
# Some more custom steps
...

End of my Dockerfile to launch app with cleared optimized cache


# Run Laravel commands
RUN php artisan optimize:clear

CMD php artisan serve --host=0.0.0.0 --port=8080

EXPOSE 8080
本文收录于