Template project for PHP with Docker with basic composer setup. By default, includes xdebug extension and PHP_CodeSniffer for easy development. Includes instruction for setting it in PhpStorm.
- https://www.docker.com/
- https://docs.docker.com/compose/
- https://www.php.net/
- https://xdebug.org/
- https://github.com/squizlabs/PHP_CodeSniffer
- https://www.jetbrains.com/phpstorm/
Clone and tweak it to your needs. Tested on Linux (Ubuntu):
- Docker version 20.10.8, build 3967b7d
- docker-compose version 1.29.2, build 5becea4c
Clone repository, cd
inside, take a look at docker-compose.yml
file, change it according if needed.
Then go to directory docker/php
and create .env
file based of .env.dist
template.
As a last step, take a look docker/php/config/docker-php-memlimit.ini
- you can change memory limit for PHP if needed.
Afterwards run:
docker-compose build docker-compose up
After that log into container with docker exec -it php.docker bash
, where php.docker
is the default container name from docker-compose.yml
file.
File app/src/run.php
acts as entry file, to use it run command:
php src/run.php
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
file).- Tick
Use path mappings
-> setFile/Directory
<->Absolute path on the server
as:</absolute/path>/php_docker_cli/app
<->/var/www/app
(default fromdocker-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 inLifecycle
section ensure to pickAlways start a new container (...)
, inGeneral
refresh 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.dist
and 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/phpcs
and/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).
- Copy
phpunit.xml.dist
intophpunit.xml
. - Login into
app.php
container whereapp.php
is the default container name fromdocker-compose.yml.dist
, and run./bin/phpunit
. Settings
->PHP
->Test frameworks
. Click+
andPHPUnit by Remote Intepreter
-> pick interpreter. InPHPUnit library
tickPath to phpunit.phar
and typebin/phpunit
. Click refresh icon. InTest runner
section setDefault configuration file
tophpunit.xml
andDefault bootstrap file
totests/bootstrap.php
.