Symfony 5 project templates for different architectures, running on Docker (utilizing docker-compose) with PHP 8.0 + nginx 1.18 + PostgreSQL 11. By default, includes xdebug extension and PHP_CodeSniffer for easy development and basic configuration for opcache for production. Includes instruction for setting it in PhpStorm. It contains branches with skeletons for:
- Symfony default file-structure:
master - Symfony bare skeleton project
skeleton - Domain-Driven Design
skeleton-dddWIP
Clone and tweak it to your needs. Tested on Linux (Ubuntu 20.04):
- Docker version 20.10.9, build 79ea9d3
- docker-compose version 1.29.2, build 5becea4c
and Windows 10:
- Use
Docker for Windows, at least version3.2.1. - Switch to
Linux containers. - Go to
Settings->Docker Engineand setexperimental modetotrue.
- Clone repository,
cdinside. - Create
.envfile indocker/phpdirectory according to your environment, one of -dev,test,prod- just copy correct template.env.dist, but remember to define your ownAPP_SECRET! - Review
docker-compose.ymland change according to the comments inside. - You can change PHP memory limit in
docker/php/config/docker-php-memlimit.initfile if you want.
Afterwards run:
docker-compose build docker-compose up
After that log into container with docker exec -it symfony.php bash, where symfony.php is the default container name from docker-compose.yml. Then run:
composer install
From this point forward, application should be available under http://localhost:8050/, where port 8050 is default defined in docker-compose.yml.
Environment variable APP_ENV must be set to test to be able to run Kernel-/Web-TestCases based tests because
Real environment variables win over .env files and this is the case in docker-based environments.
Solution implemented in https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing lets you test
only services which are used in application, eg. injected in controller - otherwise they will be removed from container,
even in test environment. To overcome this and be able to test services not-yet used file config/services_test.yaml
should be created. But the disadvantage of this is that all services' definitions from config/services.yaml must be
duplicated to services_test.yaml as well but including setting _defaults.public config to true.
All PHP extensions can be installed via docker-php-ext-install command in docker/php/Dockerfile. Examples and usage:
https://gist.github.com/giansalex/2776a4206666d940d014792ab4700d80.
Based on PhpStorm version: 2021.1.4
Open directory including cloned repository as directory in PhpStorm.
Settings->PHP->Servers: create server with namedocker(the same as in ENV variablePHP_IDE_CONFIG), hostlocalhost, port8050(default fromdocker-compose.yml).- Tick
Use path mappings-> setFile/Directory<->Absolute path on the serveras:</absolute/path>/symfony-docker/app<->/var/www/app(default from docker-compose.yml). Settings->PHP: three dots next to the fieldCLI interpreter->+button ->From Docker, Vagrant(...)-> fromdocker-compose, from servicephp, serverDocker, configuration files./docker-compose. After creating inLifecyclesection ensure to pickAlways start a new container (...), inGeneralrefresh interpreter data.
Settings->PHP->Debug->Xdebug->Debug port:9003(set by default) and checkCan accept external connections.- Click
Start Listening for PHP Debug connections->+button, set breakpoints and refresh website.
- Copy
app/phpcs.xml.distand name itphpcs.xml. Tweak it to your needs. Settings->PHP->Quality Tools->PHP_CodeSniffer->Configuration: three dots, add interpreter with+and validate paths. By default, there should be correct path mappings and paths already set to/var/www/app/vendor/bin/phpcsand/var/www/app/vendor/bin/phpcbf.Settings->Editor->Inspections->PHP->Quality tools-> tickPHP_CodeSniffer validation-> tickShow sniff name-> set coding standard toCustom-> three dots and type/var/www/app/phpcs.xml(path in container).
Open Database section on the right bar of IDE -> Data Source -> PostgreSQL -> set host to localhost, set user to app_user, pass app_pass, database to app (defaults from docker-compose.yml) Set url to jdbc:postgresql://localhost:5432/app.
- Copy
phpunit.xml.distintophpunit.xml. - Login into
symfony.phpcontainer wheresymfony.phpis the default container name fromdocker-compose.yml, and run./bin/phpunit. Settings->PHP->Test frameworks. Click+andPHPUnit by Remote Intepreter-> pick interpreter. InPHPUnit librarytickPath to phpunit.pharand typebin/phpunit. Click refresh icon. InTest runnersection setDefault configuration filetophpunit.xmlandDefault bootstrap filetotests/bootstrap.php.
Although there are present different files for prod and dev environments these are only stubs and this repo is not suitable to run on prod environment. The idea was to create as much integral, self-contained and flexible environment for development as possible and these files are here merely to easily mimic prod env and point out differences in configuration.