Docker with PHP 8.1, NGINX and MySQL

Mitchel Nieuwendijk
3 min readDec 13, 2021

In this blogpost you are learning how to create an PHP 8.1, nginx and MySQL development environment with docker.

Requirements

  • Docker
  • Docker compose

Install docker

To install docker follow the documentation via the following link https://docs.docker.com/get-docker/

Install docker compose

To install docker compose follow the documentation via the following link https://docs.docker.com/compose/install/

Create a directory

Make a new folder somewhere on your pc.

Add docker-compose file

Add a file called docker-compose.yml

Open docker-compose file

Open the docker-compose.yml inside an editor. Add the following
note that you can change the name of the containers to whatever you like.

version: '3.8'

services:
php:
container_name: 'blog_docker_php'
build:
context: './php'
dockerfile: 'Dockerfile'
working_dir: '/var/www'
volumes:
- './www:/var/www'

nginx:
container_name: 'blog_docker_nginx'
image: nginx:latest
ports:
- '8080:80'
- '443:443'
volumes:
- './www:/var/www'
- './nginx/conf.d/:/etc/nginx/conf.d/'
- './nginx/logs/:/var/log/nginx/'

database:
container_name: 'blog_docker_db'
image: mysql:5.7
ports:
- '3307:3306'
environment:
MYSQL_DATABASE: databasename
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: user
MYSQL_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- './data/db:/var/lib/mysql'

Adding directories

Now add the following directories to our project folder like the picture below.

Docker project folder structure

Add Dockerfile to PHP directory

Create a new file called Dockerfile
Add the follwing:

FROM php:8.1-fpm

# Install Dependencies
RUN apt-get
update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
sqlite3 \
memcached \
libzip-dev \
libpng-dev \
libmcrypt-dev \
zip \
libcurl4-openssl-dev \
libedit-dev \
libssl-dev \
libonig-dev \
libpq-dev \
libxml2-dev \
xz-utils \
libsqlite3-dev \
git \
vim \
nano \
net-tools \
pkg-config \
iputils-ping

# Install PHP Extensions
RUN docker-php-ext-install
gd \
bcmath \
calendar \
iconv \
ctype \
intl \
xml \
pcntl \
mysqli \
opcache \
pdo \
pdo_mysql \
pgsql \
zip

# Install Composer
RUN curl -
sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN ln -s $(composer config --global home) /root/composer
RUN rm -rf /tmp/*

Add Nginx config file

Go to the nginx/conf.d directory and add a file called site.conf
Add the following:

server {
listen 80;
server_name localhost;

index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass blog_docker_php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

To change the root of your project you can change the root property

server {
...
root /var/www/;

...
}

If you have changed the container name inside the docker-compose.yml
you need to change the name called blog_docker_php to your container name inside the site.conf

server {
location ~ \.php$ {
...
fastcgi_pass blog_docker_php:9000;
...
}
}

Add your PHP project

Now you can add your php project to the www directory in my case i will add an file called index.php with the following

<?php
phpinfo
();

If add your own project don’t forget to change the root inside the nginx/conf.d/site.conf

Build docker containers

Now we can build the docker containers based on the docker-compose.yml
First we open the terminal and go to the root of the project folder.
Execute the following command

docker-compose build

After some time it is successfully build now we can start our container by the following command

docker-compose up -d

Now you can visit http://localhost:8080/

To shutdown the containers you simply run

docker-compose down

--

--