1. Add Xdebug to your PHP application container
Add following lines to your php Dockerfile:
- The Xdebug configuration Normally you would define the configuration inside php.ini file (e.g. /etc/php/7.0/fpm/php.ini or similar). But for the container which I am using it is located inside./etc/php/development.ini (path relative to the docker-compose.yml location). So here is the Xdebug configuration.
- Run/debug a php script on docker To verify that everything is working, open the file app/hello-world.php in PhpStorm, right click in the editor pane and choose 'Run'. PhpStorm will start the configured container and run the script. The output is then visible at the bottom of the IDE.
2. Add necessary environment variables
docker-compose.override.yaml
Here we do following things:
- Enable the xdebug extension.
- Enable automatic start on every request (see note on this below).
- Increase default maximal function nesting level, because it is often not enough.
- Instruct XDebug to connect back to the IP where web request came from.
- Instruct XDebug to connect to
host.docker.internal
for command line execution or whenever “connect back” is not possible. - Set
PHP_IDE_CONFIG
env variable toserverName=localhost
. This will tell your PhpStorm which server configuration to use. See next step for details.
How to Set up Xdebug on a Docker Container with Linux Centos 7, Apache, PHP 7.3, for Web Development.
3. Configure server in PhpStorm
In your PhpStorm Settings go to Languages and Frameworks > PHP > Servers
and add a new server:
- Name: localhost
- Host/Port: whatever host and port you use to open your local website, for example: ‘magento.localhost’ and ‘8080’.
- Debugger: Xdebug
- Use path mappings: yes
Configure the path mapping according to your source code volume mount in docker-compose.yaml
.
I have the following mount: ./magento:/var/www/html
, therefore my local ./magento
directory is mapped to the /var/www/html
path on the server.
Debugging
One important thing you need to do is to start listening for PHP debug connections with a small phone icon in your PhpStorm. Clean app 2020.
Autostart
Normally you would need a browser extension, which adds debug session start flag to your requests when you need it.
However I found it convenient to enable autostart and only control the XDebug via PhpStorm.
The case is that when XDebug tries to start the debugging session but the remote host is not listening, the XDebug does not continue.
So when I don’t need to debug, I just switch listening off. I believe this still adds a small overhead in time for all requests, but for me it is unnoticeable.
If you don’t like this approach, just disable the autostart and start the session your own way (see: Activate debugger).
Creating Run/Debug configurations in PhpStorm
Sometimes it is useful to create and store some specific configuration so you can run it over and over.
I will not describe the whole Run/Debug configurations topic here but only one Docker-specific aspect: you need to teach your PhpStorm to run PHP interpreter inside your container.
For this you need to create a new PHP CLI interpreter configuration:
- in your PhpStorm Settings go to
Languages and Frameworks > PHP
and click the ‘…’ button near the “CLI Interpreter” field. - in new window add a new interpreter “From Docker, Vagrant, VM, Remote…”
- choose “Docker Compose” radiobutton,
- select or create new Server (use Unix socket to connect to Docker daemon)
- choose Docker Compose Configuration files; in my case I choose two in following order:
docker-compose.yaml
docker-compose.override.yaml
- select your PHP app service
- choose “Connect to existing container” instead of starting a new one.
This should be enough, save everything and create your Run/Debug configuration using this CLI interpreter.
Estimated reading time: 5 minutes
There are multiple parts of Compose that deal with environment variables in onesense or another. This page should help you find the information you need.
Substitute environment variables in Compose files
It’s possible to use environment variables in your shell to populate valuesinside a Compose file:
If you have multiple environment variables, you can substitute them by addingthem to a default environment variable file named .env
or by providing apath to your environment variables file using the --env-file
command line option.
The “.env” file
You can set default values for any environment variables referenced in theCompose file, or used to configure Compose, in an environment filenamed .env
. The .env
file path is as follows:
- Starting with
+v1.28
,.env.
file is placed at the base of the project directory - For previous versions, it is placed in the current working directory where the Docker Compose command is executed unless a
--project-directory
is defined which overrides the path for the.env
file. This inconsistency is addressed in+v1.28
by limiting the filepath to the project directory.
When you run docker-compose up
, the web
service defined above uses theimage webapp:v1.5
. You can verify this with theconfig command, which prints your resolved applicationconfig to the terminal:
Values in the shell take precedence over those specified in the .env
file.
If you set TAG
to a different value in your shell, the substitution in image
uses that instead:
You can override the environment file path using a command line argument --env-file
.
Using the “--env-file” option
By passing the file as an argument, you can store it anywhere and name it appropriately, for example, .env.ci
, .env.dev
, .env.prod
. Passing the file path is done using the --env-file
option:
This file path is relative to the current working directory where the Docker Composecommand is executed.
The .env
file is loaded by default:
Passing the --env-file
argument overrides the default file path:
When an invalid file path is being passed as --env-file
argument, Compose returns an error:
For more information, see theVariable substitution section in theCompose file reference.
Set environment variables in containers
You can set environment variables in a service’s containers with the‘environment’ key, just like withdocker run -e VARIABLE=VALUE ..
:
Pass environment variables to containers
You can pass environment variables from your shell straight through to aservice’s containers with the ‘environment’ keyby not giving them a value, just like with docker run -e VARIABLE ..
: Wordpress development services usa.
The value of the DEBUG
variable in the container is taken from the value forthe same variable in the shell in which Compose is run.
The “env_file” configuration option
You can pass multiple environment variables from an external file through toa service’s containers with the ‘env_file’ option,just like with docker run --env-file=FILE ..
:
Set environment variables with ‘docker-compose run’
Similar to docker run -e
, you can set environment variables on a one-offcontainer with docker-compose run -e
:
You can also pass a variable from the shell by not giving it a value:
The value of the DEBUG
variable in the container is taken from the value forthe same variable in the shell in which Compose is run.
When you set the same environment variable in multiple files, here’s thepriority used by Compose to choose which value to use:
- Compose file
- Shell environment variables
- Environment file
- Dockerfile
- Variable is not defined
In the example below, we set the same environment variable on an Environmentfile, and the Compose file:
When you run the container, the environment variable defined in the Composefile takes precedence.
Having any ARG
or ENV
setting in a Dockerfile
evaluates only if there isno Docker Compose entry for environment
or env_file
.
Specifics for NodeJS containers
Phpstorm Docker Xdebug Not Working
If you have a package.json
entry for script:start
likeNODE_ENV=test node server.js
, then this overrules any setting in yourdocker-compose.yml
file. Mar 24 birthday.
Configure Compose using environment variables
Phpstorm Xdebug Docker Cli
Several environment variables are available for you to configure the DockerCompose command-line behavior. They begin with COMPOSE_
or DOCKER_
, and aredocumented in CLI Environment Variables.
Comments are closed.