Skip to content

Instantly share code, notes, and snippets.

Created Sep 7, 2020
What would you like to do?
Muli-Stage Docker image for Laravel applications
# PHP Dependencies
FROM composer:2.0 as vendor
COPY database/ database/
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN composer install \
--no-interaction \
--no-plugins \
--no-scripts \
--no-dev \
COPY . .
RUN composer dump-autoload
# Frontend
FROM node:14.9 as frontend
COPY artisan package.json webpack.mix.js yarn.lock tailwind.js ./
RUN yarn install
COPY resources/js ./resources/js
COPY resources/sass ./resources/sass
RUN yarn production
# Application
FROM php:7.4-fpm
# Install PHP dependencies
RUN apt-get update -y && apt-get install -y libxml2-dev
RUN docker-php-ext-install pdo pdo_mysql mbstring opcache tokenizer xml ctype json bcmath pcntl
# Copy Frontend build
COPY --from=frontend /app/node_modules/ ./node_modules/
COPY --from=frontend /app/public/js/ ./public/js/
COPY --from=frontend /app/public/css/ ./public/css/
COPY --from=frontend /app/public/mix-manifest.json ./public/mix-manifest.json
# Copy Composer dependencies
COPY --from=vendor /app/vendor/ ./vendor/
COPY . .
RUN php artisan config:cache
RUN php artisan route:cache
Copy link

You don't need to copy the node_modules in the last step. After yarn production all necessary javascript is compiled into the javascript files in public/js.

Copy link

Phenomenan commented Dec 20, 2021

I get No package 'oniguruma' found when I want to build this. I added RUN apt-get install -y libonig-dev to fix this issue.

Copy link

FWIW, You might not need to explicitly install mbstring (or oniguruma) in the Dockerfile, if you're starting with the PHP image. See the official image, it looks like mbstring is already installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment