How to Set Up a Local Symfony Development Environment with Docker Compose

Anton Lytvynov
4 min readMar 8, 2023

Streamline Your Symfony Development Workflow with Docker Compose for Efficient Local Testing and Deployment

Why you should use Docker locally during the development?

  1. Isolation: Docker allows you to isolate your development environment from the rest of your system. You can create containerized environments with specific versions of software and dependencies, ensuring that your application runs in a consistent environment.
  2. Reproducibility: With Docker, you can package your application with all its dependencies, ensuring that it runs the same way on any machine. This makes it easy to reproduce bugs and issues, which can save a lot of time and effort during development.
  3. Flexibility: Docker allows you to easily switch between different versions of software and dependencies. This can be particularly useful when working on projects that require specific versions of software that may not be compatible with other projects on your machine.
  4. Efficiency: Docker containers can be created and destroyed quickly, which can save a lot of time when setting up new development environments or switching between projects.
  5. Collaboration: Docker makes it easy to share development environments with other developers, ensuring that everyone is working in the same environment. This can help prevent compatibility issues and streamline the development process.

Simplest docker-compose for local development

Mainly for any Symfony application you need 3 principal docker images:

  • Db (lets take classic MySQL)
  • Server (nginx for example)
  • Php & application itself

So here is the simple docker-compose with these 3 images

version: '3.3'

networks:
example:

services:
db_example:
container_name: db_example
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- example
expose:
- "3306"
ports:
- "127.0.0.1:3306:3306"
volumes:
- mysql_data:/var/lib/mysql

php_example:
container_name: php_example
build:
context: ./docker/php
ports:
- "127.0.0.1:9000:9000"
volumes:
- ${PWD}/:/var/www/app
depends_on:
- db_example
networks:
- example

nginx_lp:
container_name: nginx_example
image: nginx:latest
restart: always
ports:
- "127.0.0.1:8080:80"
volumes:
- ${PWD}/:/var/www/app
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php_example
- db_example
networks:
- example

volumes:
mysql_data:

You need to put this file directly in the root of your Symfony application.

Docker in Symfony

Php image

A PHP Docker image is a pre-configured container that includes a complete PHP environment, including the necessary libraries, extensions, and runtime. Bellow is the customized PHP Docker image.

You may note that you have detailed php container.

Php container

So here is the Dockerfile itself.

FROM php:8.0-fpm

RUN apt-get update && apt-get install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip pdftk \
&& docker-php-ext-install intl opcache pdo pdo_mysql \
&& pecl install apcu \
&& docker-php-ext-enable apcu \
&& docker-php-ext-configure zip \
&& docker-php-ext-install zip

WORKDIR /var/www/app

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN curl -sS https://get.symfony.com/cli/installer | bash
RUN mv /root/.symfony5/bin/symfony /usr/local/bin/symfony

RUN useradd -d /home/tester -m -s /bin/bash tester && echo "tester:tester" | chpasswd && adduser tester sudo

RUN apt-get install -y iputils-ping

Nginx image

Nginx (pronounced “engine-x”) is a popular open-source web server and reverse proxy software. It is known for its high-performance, scalability, and low resource consumption. Nginx can handle thousands of concurrent connections with minimal memory usage and is commonly used for serving static content, load balancing, and caching.

We took the this docker image https://hub.docker.com/_/nginx and we need to tell it, how trait our site.

Pay attension on this config if you plan to rename _example siffix fastcgi_pass php_example:9000;

Docker / nginx folder

default.conf file:

server {
listen 80;
index index.php;
server_name localhost;
root /var/www/app/public;
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
client_max_body_size 100M;

location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}

location ~ ^/index\.php(/|$) {
fastcgi_pass php_example:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;

fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}

location ~ \.php$ {
return 404;
}

error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}

Thank you for your attension

Thank you for reading my response. I hope it was informative and helpful to you. If you found this interesting and would like to learn more, please check out the link, we have a lot of symfony related content there. Your feedback is appreciated, so please clap or provide a comment if you found this useful. Thanks again for your time and attention.

--

--

Anton Lytvynov

CEO & Founder of Lytvynov Production, Senior web developer, architect, cryptocurrencies trader, https://lytvynov-production.com