Creating Multi-Stage Docker Builds for Laravel


August 17th, 2018


Starting in Docker version 17.05 multi-stage builds are available to help you optimize Dockerfiles. Using multi-stage builds is a clean way to set up a Docker build pipeline that simplifies the requirements you need on your CI/build server to create a Docker image for your app.

If you’re not familiar with multi-stage builds, no worries! Let me summarize by saying that before multi-stage builds if you wanted to install composer dependencies and frontend dependencies during a docker build of your app, your Docker image would need to have the necessary dependencies such as Node.js installed and Composer PHP.

The resulting Dockerfile and image can be fairly messy and frustrating to maintain.

Another approach that I’ve taken in my build environment is installing composer and node dependencies on the server running the build, and then copying the artifacts such as the vendor/ folder and production-ready JS and CSS build files.

A simplified version of building dependencies outside of Docker and copying them it might look something like the following:

1#!/usr/bin/env bash
3set -e
7composer install \
8 --ignore-platform-reqs \
9 --no-interaction \
10 --no-plugins \
11 --no-scripts \
12 --prefer-dist
14yarn install && yarn production
16docker build -t my-app:$tag .

The above script is a simplified version to illustrate how you’d prepare the PHP and frontend dependencies and the docker build would copy the project into the image.

By using multi-stage builds, you no longer need to install those dependencies or cram them all into your Dockerfile, thus bloating your final image size!

Multi-Stage Approach

Using a Laravel 5.6 application as an example, we can start building the dependencies for production-ready images in one Dockerfile that contains multiple stages.


If you’re following along, create a demo project from the command line with the following:

1laravel new multi-stage-demo
3cd multi-stage-demo
5# Create the Dockerfile file
6touch Dockerfile
8# Generate the yarn lockfile
9yarn install
11git init
12git add .
13git commit -m"First commit"

Next, create a .dockerignore file in the root of your Laravel app with the following:


We aren’t going to go too deeply into how the .dockerignore file works, but the important part to understand is that it prevents copying the defined paths during a docker build. The ignore file means that the vendor/ folder won’t get copied in during a COPY or ADD instruction in the Dockerfile:

1# copies the Laravel app, but skips the ignored files and paths
2COPY . /var/www/html

For example, ignoring the vendor/ folder means that we get a pristine build of the composer dependencies from our build.

The Dockerfile

Our application Dockerfile will break up each stage of the application build with three parts:

  1. Install Composer Dependencies
  2. Build frontend dependencies
  3. Copy our app code and artifacts from the first two stages

Stage 1

The first stage will be composer dependencies. We can use the official composer image to install our application dependencies first in the Dockerfile within the root of the project:

2# PHP Dependencies
4FROM composer:1.7 as vendor
6COPY database/ database/
8COPY composer.json composer.json
9COPY composer.lock composer.lock
11RUN composer install \
12 --ignore-platform-reqs \
13 --no-interaction \
14 --no-plugins \
15 --no-scripts \
16 --prefer-dist

Note the as vendor portion of the FROM instruction. We will reference this name later when we copy the artifact (the vendor/ folder) later in the Dockerfile.

The first stage of our Dockerfile copies a few necessary paths that Laravel configures in the composer.json autoload classmap. Next, it copies in the composer JSON and lock files. Finally, it runs a composer install to install the dependencies.

If you rerun a build you can peek around the image and see the results of the composer install command:

1docker build -t ms-laravel-test -f docker/Dockerfile .
3docker run --rm -it ms-laravel-test bash
5# Inside the running container
6bash-4.4# ls -la vendor/
8bash-4.4# exit

We are done with stage one! This stage of the build installed all the necessary vendor/ files that we’ll copy later.

Stage 2

The next stage of the build is installing our application frontend dependencies using Yarn (or NPM if that’s your thing). After the first stage, append the following to the Dockerfile:

2# Frontend
4FROM node:8.11 as frontend
6RUN mkdir -p /app/public
8COPY package.json webpack.mix.js yarn.lock /app/
9COPY resources/assets/ /app/resources/assets/
11WORKDIR /app
13RUN yarn install && yarn production

Once again we are using an official image, this time the LTS version of Node, which comes with yarn as well. We create a /app/public directory and copy in the needed files into the /app path so that Laravel Mix knows can build the JavaScript and CSS.

Last, we install the package dependencies and run the yarn production command to finish installing our frontend assets!

At this point we can build our image again and inspect the frontend dependencies:

1docker build -t ms-laravel-test .
2docker run --rm -it ms-laravel-test bash
4# Inside the running container
5root@faa6904a98df:/app# ls -la
6root@faa6904a98df:/app# ls -la public/{js,css}
7root@faa6904a98df:/app# exit

Feel free to investigate the frontend paths while in the container and finally exit the running container. We’re ready to build the final stage using the artifacts we’ve created!

Stage 3

The final stage of our Docker build involves copying in the application files from the source code and copying the artifacts from earlier build stages.

To create the final build stage, append the following to the Dockerfile:

2# Application
4FROM php:7.2-apache-stretch
6COPY . /var/www/html
7COPY --from=vendor /app/vendor/ /var/www/html/vendor/
8COPY --from=frontend /app/public/js/ /var/www/html/public/js/
9COPY --from=frontend /app/public/css/ /var/www/html/public/css/
10COPY --from=frontend /app/mix-manifest.json /var/www/html/mix-manifest.json

We first copy the application into the image. The .dockerignore file ensures that ignored paths are not copied from the host machine into the image.

Note the COPY --from instructions which reference the names of earlier stages (i.e. FROM node:8.11 as frontend). We are copying the artifacts from earlier stages into their proper place within the final image.

If you build the image one last time, you can see the full-constructed application without the overhead of node packages or composer scripts!

1docker build -t ms-laravel-test .
2docker run --rm -it ms-laravel-test bash
4# Inside the running container
5root@1d6251d2362c:/var/www/html# ls -la

Our final image is a PHP 7.2 image with Apache, however, to run this app we would need to modify the default Vhost config file to point to the /var/www/html/public that Laravel expects. We’ll skip that since this article’s focus is using multi-stage builds ;)

Here’s the final Dockerfile in full:

2# PHP Dependencies
4FROM composer:1.7 as vendor
6COPY database/ database/
8COPY composer.json composer.json
9COPY composer.lock composer.lock
11RUN composer install \
12 --ignore-platform-reqs \
13 --no-interaction \
14 --no-plugins \
15 --no-scripts \
16 --prefer-dist
19# Frontend
21FROM node:8.11 as frontend
23RUN mkdir -p /app/public
25COPY package.json webpack.mix.js yarn.lock /app/
26COPY resources/assets/ /app/resources/assets/
28WORKDIR /app
30RUN yarn install && yarn production
33# Application
35FROM php:7.2-apache-stretch
37COPY . /var/www/html
38COPY --from=vendor /app/vendor/ /var/www/html/vendor/
39COPY --from=frontend /app/public/js/ /var/www/html/public/js/
40COPY --from=frontend /app/public/css/ /var/www/html/public/css/
41COPY --from=frontend /app/mix-manifest.json /var/www/html/mix-manifest.json

Learn More

The first place you should check out to learn more about multi-stage builds is Use multi-stage builds from the official Docker documentation. Multi-stage builds are useful and an excellent way to build your final application artifact without many local dependencies.

If you want to learn more about developing with Docker and PHP, including Laravel, check out my book Docker for PHP Developers. If you don’t want the starter Laravel code, you can also get the book only version. Laravel News readers get a discount on both versions of the book! You can learn more about the book announcement here.

The links included are affiliate links which means if you decide to buy Laravel News gets a little kickback to help run this site.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.