How to Set Up Symfony with Docker for Local Development - 149 reads

Docker allows you to create isolated, consistent environments for your applications, making it easier to manage dependencies and collaborate across teams. This step-by-step tutorial covers everything from creating a new Symfony project, setting up Docker

Prerequisites:

  1. Basic knowledge of Docker and Symfony.
  2. Docker installed on your machine.
  3. Docker Compose installed.


1.Create a New Symfony Project

First, we need to create a new Symfony project. If you already have a Symfony project, you can skip this step.

composer create-project symfony/skeleton my-symfony-app
cd my-symfony-app


2.Set Up a Dockerfile

Create a Dockerfile in the root of your Symfony project. This file will define the environment in which your Symfony application will run.

FROM php:fpm

RUN apt-get update \
  && apt-get install -y curl zip unzip \
  && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer

RUN docker-php-ext-install pdo pdo_mysql opcache

RUN pecl install xdebug && docker-php-ext-enable xdebug && docker-php-ext-enable opcache

COPY ./php.ini "${PHP_INI_DIR}/conf.d"

WORKDIR /app


2.Set Up "docker-compose.yml"

Docker Compose is used to define and run multi-container Docker applications. We'll create a docker-compose.yml file to configure our Symfony application, database, and other services.

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      - ./app:/app
  php:
    build:
     context: .
     dockerfile: PHP.Dockerfile
    extra_hosts:
     - host.docker.internal:host-gateway
    volumes:
      - ./app:/app
  mysql:
    image: mariadb:latest
    environment:
      MYSQL_ROOT_PASSWORD: 'password'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'password'
      MYSQL_DATABASE: 'my_database'
    volumes:
      - mysqldata:/var/lib/mysql
    ports:
      - 33066:3306
  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
     - 8081:80
    environment:
     - PMA_HOST=mysql
     - PMA_PORT=3306
     - PMA_USER=root
     - PMA_PASSWORD=secret
volumes:
  mysqldata: {}


3.Configure Nginx

Create an nginx directory in your project root and add an nginx.conf file to configure Nginx for serving the Symfony application.

server {
  fastcgi_read_timeout 3600s;
  listen 80;
  listen [::]:80;
  server_name localhost;
  root /app/public;

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

  location ~ ^/.+\.php(/|$) {
    fastcgi_pass php:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}


4.Build and Run the Containers

Now that all configurations are in place, it's time to build and start the Docker containers. To build the containers, run the following command (this has to be done only once):

docker-compose build --no-cache

To stop the containers, you can run:

docker-compose down

And to start them, you can run:

docker-compose up


6.Accessing the Symfony Application

Once the containers are up and running, you can access your Symfony application in the browser at http://localhost:8080. If everything is set up correctly, you should see the Symfony welcome page.


7.Managing Your Database

The MySQL database is running in a Docker container and is accessible in the browser at http://localhost:8081. You can also connect third-party software such as your IDE to it by using the credentials configured in docker-compose.yml (MYSQL_USER, MYSQL_PASSWORD, at port 33066).


Conclusion

With Docker, you can easily create a consistent development environment for your Symfony projects. This setup ensures that all dependencies are isolated and version-controlled, making it easier to collaborate with other developers and deploy your application. As you become more familiar with Docker, you can extend this setup to include additional services like Redis, Elasticsearch, or RabbitMQ.