Xdebug Docker For Mac



Create a Dockerfile with xdebug installation for the development, dont use this for production, it will slow down your performance. Build your-wordpress image from that Dockerfile. You might need to create the xdebug.ini file with your remote host details, i'm using phpstrom this is what my xdebug.ini looks like. Xdebug is an extension for PHP to assist with debugging and development. It contains a single step debugger to use with IDEs; it upgrades PHP's vardump function; it adds stack traces for Notices, Warnings, Errors and Exceptions; it features functionality for recording every function call and variable assignment to disk; it contains a profiler.

  1. Xdebug Docker For Mac High Sierra
  2. Docker Xdebug Phpstorm
  3. Phpstorm Docker Xdebug Not Working
  4. Xdebug Docker For Mac Catalina
  5. Docker Php Fpm Xdebug

To use xdebug with macOS and docker is quite, let´s call it tricky ;)

The problem with Docker for Mac and xDebug is that it is mapped to your localhost (127.0.0.1), so PHP/xDebug doesn’t actually know the true IP address of the remote host connecting to xdebug. To get around this what we need to do is configure an alias to the loopback device to get this work. Sudo ifconfig lo0 alias 10.254.254.254. 另外,同事的 XDebug 服务端的配置文件有做过修改,增加了 xdebug.remotehost 参数设置为172.25.160.1,172.25.160.1 的来源是通过 ipconfig 命令查看到的 Docker 与宿主机通信的地址。 在 macOS 上这个地址通常使用 docker.for.mac.localhost 来代替。 最终我本地的配置如下:. To start using Docker, the default Docker machine should be launched using the Docker Quickstart Terminal (Windows / Mac OS X, for Linux you need to use a standard terminal executing a sudo docker run hello-world command).

The following steps need to be proceed to get it working:

  1. use the config from the xdebug.ini wihtin your docker web container. Important: set remote_connect_back to off

UPDATE

As mentioned by some comments (thanks for the feedback), it is not needed to configure the IP manually. Thus, the next step is optional and the configuration (xdebug.ini) is updated to use the dynamic IP.

  1. optional: set up an alias for your local interface (lo)

To bring up the alias at startup, you can either (sudo may be needed here):

  • manually place the file com.manuelselbach.docker_10254254254_alias.plist into directory: /Library/LaunchDaemons/

  • Or use the script set_and_install_autorun_alias_for_lo.sh

Configure PhpStorm

After all that, just configure your PhpStorm: Plants vs zombies offline download.

Xdebug Docker For Mac
  1. set port for xdebug Preferences -> Languages & Frameworks -> PHP -> Debug | Xdebug: Debug port = 9005
  2. configure a configuration in the toolbar
  • use PHP remote Debug
  • add a server to your domain (without protocoll like http:// or https://)
  • set port for http / https (not the xdebug port here)
  • select Debugger: Xdebug
  • if needed: set path mappings
  1. set Ide key (session id): to PHPSTORM

Happy debugging with docker!

This article walks through setting up Xdebug on a Docker container running Apache with PHP 7.1. If you don’t already have it installed, get Docker for your platform here. Some familiarity with the command line is assumed.

Here’s the steps we’ll take:

1. Create our Configuration Files

Let’s create a directory where we’ll store the files that we’re creating. We’ll call it php71-apache .

The first file is optional. It’s a php.ini file. This one-liner will keep PHP from complaining about setting a time-zone. Use your appropriate time zone here. Here’s the contents.

The second configuration file is for Xdebug. Here’s our xdebug.ini file contents.

Xdebug Docker For Mac High Sierra

2. Create our Dockerfile

Now that our configuration files are complete. Let’s move on to creating our Dockerfile .

Here’s a rundown of our Dockerfile .

Xdebug

Line 1: Specifies the base image from which we’re building our custom image. This is the official PHP Apache image.

Line 2: Runs a script that will install the mysqli PHP extension. More information about installing PHP extensions in containers can be found on the PHP repository page for Docker under the How to install more PHP extensions section.

Line 3: We enable the rewrite Apache module because most of my projects need it.

Xdebug Docker For Mac

Line 4: Install the Xdebug extension via PECL.

Line 5: (optional) Copy our php.ini file to the image.

Line 6: Copy the xdebug.ini file to the image.

Line 7: Allow access to the container’s port 80.

3. Build our Image

Xdebug Docker For Mac

Now that our configuration is all set up, it’s time to build the image. From within the php71-apache directory, run the following command.

The -t argument specifies that our image will be named ‘php71-apache’.

4. Run our Container

Our image is all setup and ready for us. Let’s spin her up! Here’s the command to do it. Change the location of your website code as needed.

This is a little complicated so we’ll step through each bit.

  • First we tell docker that we want to run a container with the run sub-command.
  • The -d argument says that the container is going to run in detached mode, meaning we don’t need to interact with it or keep it running in the foreground.
  • We’re going to call our container php71-apache-example with the –name argument.
  • The –v argument tells docker to mount a directory in our home directory called Sites/myexamplesite.com/htdocs to the /var/www/html directory in the container. Both the host and the container will have access to these files at the same time.
  • Next, the -p argument says that our host machine will listen for requests on port 8080 and forward those requests to port 80 of the container. You can use port 80 on the host if you don’t have another web server running on your computer using that port.
  • The -e argument allows us to create environment variables used in the container. Here, we’re setting the XDEBUG_CONFIG environment variable. This allows us to pass any Xdebug parameters through the environment. In this case, we tell Xdebug the IP address to connect back to; in other words, the IP of your machine where the debugger is listening. I use command substitution to grab my IP address from my Mac. You can manually place your IP address here.
  • Finally, we tell Docker to use the php71-apache image we created in the build step above.

Your newly created container should be running and accessible at http://localhost:8080 .

Using Docker Compose

Docker Xdebug Phpstorm

Instead of a complicated run command as given above, we can store all the details in a docker-compose.yml file and use the docker-compose command to run the container instead of docker run . Create a docker-compose.yml file in your php71-apache directory with the contents below. You’ll want to change your volume location and IP address, of course.

Now, from within the same directory, run the following

Unexpected Xdebug Remote Host Behavior

We could specify the IP address Xdebug connects to in our xdebug.ini file with the xdebug.remote_host directive.

The downside of using xdebug.remote_host in xdebug.ini to specify our IP address is that if it our IP changes for any reason, we have to login to the container, change the xdebug.ini file and restart the container. It’s not a huge issue but when using Docker, we avoid making any changes to containers that aren’t done through configuration.

On the other hand, there’s an undocumented side effect of using the XDEBUG_CONFIG environment variable vs. using the configuration file to set the remote host. When using the environment variable to set the IP address, PHP will try to connect to your debugger on each page request without being initiated by a browser plugin or other trigger. This is the same behavior as if you set xdebug.remote_autostart=1 . You can read more about this configuration item here.

Phpstorm Docker Xdebug Not Working

Attempts to avoid initiating debugger sessions with popular browser plugins that use cookies don’t work. The debug session will always start as long as your have a debugger listening. However, appending XDEBUG_SESSION_STOP=1 to the URL manually or via browser plugin will successfully tell Xdebug to not start a debugging session.

A way to around inconsistent xdebug.remote_host behavior is to write a wrapper script that uses sed to replace the IP address in the docker-compose.yml file and then call docker-compose up -d .

Xdebug Docker For Mac Catalina

If you’re lucky enough to be using a Mac, you can simply add the following line to your xdebug.ini file instead of worrying about any environment variables:

Docker Php Fpm Xdebug

I hope you find this article helpful. If so, please share! Have a suggestion to make this article better? Let me know in a comment below.