Laravel is a modern web application framework for artisans.

Introduction

Building a dockerfile for a Laravel application does not need to be tedious. The good folks at The Coding Machine have created several base images that we can leverage.

Prerequisites

You need to have the following setup on your development machine to proceed with this tutorial

  1. PHP >= 7.3
  2. Composer
  3. Laravel
  4. Docker

Let’s get started

Create a new laravel project

> laravel new laravel-app

and navigate into the laravel-app directory and execute the following command:

> php artisan serve

Try it out in your browser localhost:8000

Before we create our Dockerfile lets first create a .dockerignore file:

file: laravel-app\.dockerignore

node_modules/
public/hot/
public/storage/
storage/*.key
vendor/
.env.backup
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
Dockerfile
docker-compose.yml
docker-compose.yaml
**.git
.idea
**.gitignore
**.gitattributes
**.sass-cache

We can now create our Dockerfile:

file: laravel-app\Dockerfile

ARG PHP_EXTENSIONS="intl pdo_mysql pdo_pgsql bcmath"

FROM thecodingmachine/php:7.4-v3-slim-apache as php_base
LABEL maintainer "Julius Krah <juliuskrah@gmail.com>"

ENV TEMPLATE_PHP_INI=production
COPY --chown=docker:docker . /var/www/html
RUN composer install --quiet --optimize-autoloader --no-dev

FROM node:10 as node_dependencies
WORKDIR /var/www/html

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=false
COPY --from=php_base /var/www/html /var/www/html

RUN npm set progress=false && \
  npm config set depth 0 && \
  npm install && \
  npm run prod && \
  rm -rf node_modules

FROM php_base
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
COPY --from=node_dependencies --chown=docker:docker /var/www/html /var/www/html

Build and tag the image:

> docker build -t laravel-app .

Time to test our image with a container

> docker run --rm --name laravel -e DB_CONNECTION=pgsql -p 8000:80 laravel-app

NOTE: You can replace all options in the .env file with environment variables on the command line e.g. -e DB_CONNECTION=pgsql

Conclusion

An optional step is to tag and upload your image to docker hub:

> docker tag laravel-app <username>/laravel-app
> docker login
> docker push <username>/laravel-app

In this post we discussed creating a laravel project and making an OCI image out of it. Until the next post, keep doing cool things :+1:.