How to Develop Symfony App with Docker Compose

Updated: January 13, 2024 By: Guest Contributor Post a comment

Introduction

Docker is a platform that allows you to encapsulate your application and its environment into a container, ensuring consistency across multiple development and deployment stages. Symfony is a high-performance PHP framework for developing web applications.

Developing with Symfony and Docker Compose can significantly streamline your workflow, allowing for seamless development, testing, and deployment processes. This tutorial will guide you through the setup of a Symfony application using Docker Compose, starting with setting up a basic environment and gradually moving towards a more advanced configuration. Before diving into the tutorial, make sure Docker and Docker Compose are installed on your machine.

Steps to Get Symfony and Docker Compose Up and Running

Step 1 – Setting Up Your Project

Create a new Symfony application by running the Symfony CLI.

symfony new my_symfony_app --full

Next, initialize a new Docker environment by creating a docker-compose.yml file in the root directory of your Symfony project.

version: '3'

 services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/var/www/html
    ports:
      - '8000:8000'

This basic configuration sets up a single service for your app, mounts the current directory to your container, and exposes port 8000 to your host machine.

Step 2 – Configuring PHP Environment

Create a Dockerfile in the root directory with the necessary instructions to build your PHP environment.

FROM php:7.4-fpm

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

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

WORKDIR /var/www/html

RUN PATH=$PATH:/var/www/html/bin:/var/www/html/vendor/bin

CMD ["php-fpm"]

This Dockerfile starts with a base PHP image, installs the necessary extensions and Composer, sets a working directory, and adjusts the PATH.

Step 3 – MySQL Service

To add a MySQL service to your docker-compose.yml, include the following service definition:

version: '3'

services:
  # ... existing services

  mysql:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: symfony
      MYSQL_USER: symfony
      MYSQL_PASSWORD: symfony

volumes:
  db_data:

This service uses the MySQL image, sets up a persistent volume, and provides environment variables to configure the database.

Step 4 – Nginx Service

You’ll also need an Nginx service to serve your Symfony application. Below is a simple Nginx configuration:

version: '3'

services:
  # ... existing services

  nginx:
    image: nginx:latest
    volumes:
      - .:/var/www/html:ro
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app
    ports:
      - '80:80'

And a corresponding Nginx configuration file default.conf:

server {
    listen 80;
    server_name localhost;

    root /var/www/html/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass app:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        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/app_error.log;
    access_log /var/log/nginx/app_access.log;
}

This file establishes the server settings, document root, and defines the behavior for handling PHP files via FastCGI.

Step 5 – Symfony Environment Variables

Managing environment variables for Symfony under Docker is straightforward. You can set Symfony’s environment variables directly in the docker-compose.yml under the app service:

version: '3'

services:
  # ... existing services

  app:
    # ... existing configuration
    environment:
      DATABASE_URL: mysql://symfony:symfony@mysql:3306/symfony

  # ... other services

Step 6 – Running the Containers

To start all services defined in your docker-compose.yml, run:

docker-compose up -d

This will build the images and start the containers in the background. Visit http://localhost in your web browser to view your Symfony application.

Step 7 – Advanced Configuration

For more complex projects, you might need additional tools such as Redis, Elasticsearch, or message queues. These would be added similarly as services within your docker-compose.yml and linked to your app as needed.

Conclusion

In this tutorial, you’ve learned how to scaffold a Symfony application using Docker Compose, providing a reliable and repeatable environment for development. By containerizing your development environment, you ensure that your team can work in a consistent setting, regardless of individual machine configurations.