Source

Setup Docker with PHP for Laravel PART 2

#[Pragmatic(Kiwi)]
2 min readMay 9, 2022

--

root
|__ docker
| |_ dev
| |_ nginx.dockerfile
| |_ php.dockerfile
| |_ prod
| |_ nginx.prod.dockerfile
| |_ php.prod.dockerfile
|__ nginx
| |_ default.conf
| |_ default.prod.conf
| |_ laravel-docker.test.pem
| |_ laravel-docker.test-key.pem
|_ opcache.ini

Checkout PART1 to setup NGINX.

We will create a Dockerfile with PHP for both DEV and PROD, building them on top of each other until we create a development environment for Laravel.

SETUP FOR DEV

PHP.DOCKERFILE CONFIG
php.dockerfile

FROM php:8-fpm-alpine we are using PHP 8 from Linux Alpine

ENV — we are creating two environmental variables

RUN adduser -g ${PHPGROUP} -s /bin/sh -D ${PHPUSER} — we add a “laravel” user to console without any password

RUN sed -i we are replacing the “user = www-data” and “group = www-data” with our user, “laravel”

RUN mkdir -p /var/www/html/public we create a public directory on the web server

RUN docker-php-ext-install pdo pdo_mysql — we are installing pdo and pdo_mysql using the docker-php-ext-install utility

SETUP FOR PROD

PHP.PROD.DOCKERFILE config
php.prod.dockerfile

For production we are adding opcache to our server so we do not make a call to the server on every request. This is pretty standard.

Opcache config
opcache.ini

And this opcache.ini config is a standard boilerplate. You should customize it as to meet your needs but this works for most of apps.

--

--