Dockerize a Symfony project (Nginx, PHP-FPM, and MariaDB)

Photo by Miguel Castellanos on Unsplash

Create a new Symfony project

We will create a new Symfony project to get started but you can apply the same steps to an existing project. So if you have composer installed simply run:

> composer create-project symfony/skeleton:"^4.4" dockerized_sf_project

Add docker configuration files

Since we will be using multiple Docker containers for our Symfony project we are going to use Docker Compose to define and run them. We first create adocker-compose.yml file in our project directory and we will also create a new docker directory in which we are going to put the Docker files.

Nginx container

Since Symfony is a web application framework we need a web server to run it. We will be using nginx because it is a fast and reliable static web server. Add a Dockerfile-ngnix dockerfile inside thedocker directory with the following content:

FROM nginx:latestCOPY build/nginx/default.conf /etc/nginx/conf.d/RUN echo "upstream php-upstream { server fpm:9000; }" > /etc/nginx/conf.d/upstream.confRUN usermod -u 1000 www-data
server {listen 80;server_name  localhost;root /var/www/project/public;location / {try_files $uri @rewriteapp;}location @rewriteapp {rewrite ^(.*)$ /index.php/$1 last;}client_max_body_size 200M;location ~ ^/index.php(/|$) {fastcgi_pass php-upstream;fastcgi_split_path_info ^(.+\.php)(/.*)$;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param HTTPS off;client_max_body_size 200M;fastcgi_read_timeout 600;}error_log /var/log/nginx/symfony_error.log;access_log /var/log/nginx/symfony_access.log;}
version: "3.7"services:nginx:build:context: ./dockerdockerfile: Dockerfile-nginxvolumes:- ./:/var/www/project/:cachedports:- 10302:80 # Ports that are exposed, you can connect to port 8001 to port 80 of the container.networks:- symfony

PHP-FPM container

FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites. We will use it to install PHP and add configurations. It works perfectly with Nginx. Inside our docker repository we add the Dockerfile-php dockerfile for PHP and add the following:

FROM php:7.4-fpmRUN apt-get update && apt-get install -y --no-install-recommends \git \zlib1g-dev \libxml2-dev \libpng-dev \libzip-dev \vim curl debconf subversion git apt-transport-https apt-utils \build-essential locales acl mailutils wget nodejs zip unzip \gnupg gnupg1 gnupg2 \sudo \ssh \&& docker-php-ext-install \pdo_mysql \soap \zip \opcache \gd \intlCOPY build/php/opcache.ini /usr/local/etc/php/conf.d/COPY build/php/custom.ini /usr/local/etc/php/conf.d/RUN curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composerRUN composer self-update 1.9.0RUN wget --no-check-certificate https://phar.phpunit.de/phpunit-6.5.3.phar && \mv phpunit*.phar phpunit.phar && \chmod +x phpunit.phar && \mv phpunit.phar /usr/local/bin/phpunitRUN usermod -u 1000 www-dataRUN usermod -a -G www-data rootRUN mkdir -p /var/wwwRUN chown -R www-data:www-data /var/wwwRUN mkdir -p /var/www/.composerRUN chown -R www-data:www-data /var/www/.composerWORKDIR /var/www/project/
opcache.enable=1opcache.enable_cli=0opcache.memory_consumption=256opcache.max_accelerated_files=100000
date.timezone = "Europe/Paris"memory_limit = 4096Mupload_max_filesize = 200Mpost_max_size = 200M
fpm:build: # Info to build the Docker imagecontext: ./docker # Specify where the Dockerfile is located (e.g. in the root directory of the project)dockerfile: Dockerfile-php # Specify the name of the Dockerfileenvironment: # You can use this section to set environment variables. But you can also use the .env file.- DATABASE_URL=mysql://root:root@db/docker_sfvolumes:- ./:/var/www/project/:cached # Location of the project for php-fpm. Note this should be the same for NGINX.*networks:- symfony # Docker containers (services) that need to connect to each other should be on the same network.

MariaDB configuration

The database configuration is straight forward, in your docker-compose file under services add the following:

db: #addressimage: mariadb:10.2.29ports:- "3317:3306" #outside:inside docker container from-withinenvironment:- MYSQL_DATABASE=docker_sf- MYSQL_ROOT_PASSWORD=rootvolumes:- persistent:/var/lib/mysql- ./docker/build/db/:/docker-entrypoint-initdb.d/networks:- symfony

Run project

To build the containers, simply run:

docker-compose build
docker-compose up -d
docker-compose exec -u 1000 fpm bash

References:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aicha Fatrah

Aicha Fatrah

Software Engineer | Technical Writer | IT Enthusiast