Configure your Symfony 5 local dev env with Docker

Dev Env Symfony Docker

Requirements

  • Docker

Installation

  • Create a folder where you're going to keep your project

    mkdir ~/test
    
  • Move inside of the folder

    cd ~/test
    
  • Create a docker-compose.yml file in that folder with this content

    version: "3.4"
    
    services:
      php:
        image: webcu/symfony-installer:latest
        restart: always
        working_dir: /symfony
        volumes:
          - .:/symfony
        command: tail -f /dev/null
        ports: 
            - 8000:8000
        environment:
          XDEBUG_HOST_SETTING: host.docker.internal
          PHP_IDE_CONFIG: "serverName=localhost"
    
  • Start the container

    docker-compose up -d
    
  • Enter the container

    docker-compose exec php bash
    
  • Create a new symfony project

    symfony new test-project
    
    • If you want to avoid the warning message about your git identity, you can add your git config to your container adding this line to your docker-compose.yml file:

      volumes:
          ...
        # To add your git info to the container
        - ~/.gitconfig:/etc/gitconfig
      

      If you added the .gitconfig file after have created the container don't forget to recreate it ;)

      # Exit your container 
      root@4725a7a23464:/symfony exit
      
      # Recreate your container
      docker-compose up -d --force-recreate
      
      # Enter the new container
      docker-compose exec php bash
      
      # Create a new Symfony project
      symfony new test-project
      
  • Start the Built-in web server of PHP

    php -S 0.0.0.0:8000 -t test-project/public
    
    • php -S 0.0.0.0:8000

      This allows that the web server can be accessed from outside the container.

    • -t test-project/public

      Specifies the Document root of the server.

    For more information about the Built-in web server of PHP check its documentation:

    PHP: Built-in web server - Manual

  • Enjoy your new and shiny Symfony project visiting:

    http://localhost:8000

  • Now start writing your amazing application!

Docker Image details

To simplify the process of the creation of the Dev Env for Symfony using Docker I created an image based on the Official PHP images that include some functionalities/extensions needed/recommended for Symfony 5. Those functionalities/extensions are:

  • OPCache
  • Xdebug
  • Intl
  • Composer
  • Symfony Installer

You can find this image in Docker Hub. To use it you need to reference it in your docker-compose.yml file

services:
    php:
        image: webcu/symfony-installer
        ...
  • Use the official PHP images

    There is a great team behind them to make those images the best possible.

  • Use the helper scripts: docker-php-ext-configure,docker-php-ext-install, docker-php-ext-enable. They are available in the Official PHP images to install more easily the PHP extensions.

  • To add a PHP development config add this line to your Dockerfile

    ...
    RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
    ...
    
  • Add your custom php ini config

    Configure the php.ini options according to your needs.

  • Check this project if you don't want to deal directly with the installation of the php extensions:

    mlocati/docker-php-extension-installer

References