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

Photo by Miguel Castellanos on Unsplash

Create a new Symfony project

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

Add docker configuration files

Nginx container

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

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

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

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

References:

--

--

--

Software Engineer | Technical Writer | IT Enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Deploying a Node.js app to AWS Elastic Beanstalk

Overengineering: Why We Do It and 10 Ways to Tackle It

Be SASE with Cisco SD-WAN

12 Weeks as a iOS Developer Intern — Week Five

New features coming in Fedora 36

Compilation steps in C

How to get Azure REST APIs access tokens using PowerShell

Nowadays, one multi-tab Sheet is the engine that

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

More from Medium

Install WordPress with Docker Nginx Reverse Proxy to Apache with SSL

Docker & Nginx & Logs

The fat docker container

How to Install NGINX on Ubuntu Server 18.04