DEV Community

Nicolas Bonnici
Nicolas Bonnici

Posted on

Dockerized Symfony 6.4 project boilerplate

The perfect Dockerized Symfony 6.4 boilerplate doesn't exists... But wait, what if I create and share my own vision of it?!!

Image description

Leggooo

We gonna build a classic stack that i personaly love, with such cutting edge technologies like PostgreSQL 16, Redis and Nginx HTTP server and also PHP 8.3 and FPM and Symfony framwork in LTS version 6.4.

First of all, I want something really easy to use, so let's leverage docker compose plugin without any extra argument to start and initialize all needed containers.

composer run setup
Enter fullscreen mode Exit fullscreen mode

Project treeview

  • logs
  • docker
  • symfony

Everything is at project root to do so, no fancy parameter needed since everything use default project path convention.

Simply 3 folders, the first one "docker/" to store all Docker related configurations, one other "logs/" for the whole containers logs and a "symfony/" last one directory to put your Symfony project's source code.

Go further

Good but wait I have other developers to sync in that project and do they can scaffold it on their local development env without knowing any clue about Symfony and server side development or never work on containerized projects?

Image description

Nope just kidding, ain't no voodoo involved in that process, we're not gonna reinvent the wheel and make a revolution. Not at all, just a simple leverage of composer and more specially his scripts section.

Image description

Ok if you don't rage quit this post or punch directly your screen you'll enjoy a little magic, with this simple one line command to run all needed containers then setup the database, create the data model before populating it if you agree the prompt by responding "yes" at the end.

All that using one dead simple command:

composer run setup
Enter fullscreen mode Exit fullscreen mode

Explanation we first build and launch all needed containers, the run them in demon mode, this the first part of the command: docker compose up --build -d. The second part will then run the "install-project" composer script. See the symfony/composer.json "scripts" section.

{
    "scripts": {
        "setup": [
            "composer run build",
            "composer run up",
            "composer run install-deps",
            "composer run migrate",
            "composer run fixtures"
        ],
        "up": [
            "docker compose up -d"
        ],
        "build": [
            "docker compose build"
        ],
        "install-deps": [
            "docker exec -it php-fpm composer install -o"
        ],
        "migrate": [
            "docker exec -it php-fpm bin/console doctrine:migration:migrate -n"
        ],
        "fixtures": [
            "docker exec -it php-fpm bin/console doctrine:fixtures:load && sync"
        ],
        "logs": [
            "docker compose logs -f"
        ],
        "cache-clear": [
            "docker exec -t php-fpm bin/console cache:clear"
        ]
    }
}
Enter fullscreen mode Exit fullscreen mode

It will first install all needed composer dependencies and optimize classes autoloader. Then execute migrations up to the latest version then populate database with fixtures respectively with "doctrine/doctrine-migrations-bundle" and "doctrine/doctrine-fixtures-bundle" bundles.

The actual composer scripts available:

composer run [
    setup 
    build
    install-deps
    migrate
    fixtures
    logs
    cache-clear
]
Enter fullscreen mode Exit fullscreen mode

Talk is cheap, show me the code

Linus Torvalds famous quote: Talk is cheap, show me the code

Feel free to fork, contribute and maintain this boilerplate using this Gitlab repository: nicolasbonnici/symfony-docker-boilerplate

Top comments (0)