Skip to content

Instantly share code, notes, and snippets.

@TitasGailius
Created September 7, 2020 15:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TitasGailius/1f6ce9045938299ab53f608236839eac to your computer and use it in GitHub Desktop.
Save TitasGailius/1f6ce9045938299ab53f608236839eac to your computer and use it in GitHub Desktop.
Muli-Stage Docker image for Laravel applications
#
# PHP Dependencies
#
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
#
# Frontend
#
FROM node:14.9 as frontend
WORKDIR /app
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
WORKDIR /app
# 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
@JanMisker
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.

@Phenomenan
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.

@johnnybenson
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