Interact with Docker containers without leaving PhpStorm
In the previous article, I wrote about a particular setup you can use to configure Docker on WSL through SSH for performance boosts and the ability to keep using your native Windows apps without having to move them to WSL: Docker on WSL with PhpStorm - Best of both worlds.
In this article, we are going to see how we can use a PHP CLI interpreter from our Docker compose file and, finally, how we can interact with containers within PhpStorm. One thing to note is that I'm using Windows 10 and Docker Desktop, which is integrated with WSL, and on WSL, I'm running my project with Laravel Sail, but general guidelines from this post can be applied to any OS.
Requirements
First things first, in order for the following setup to work, make sure that the Docker
and PHP Docker
plugins are enabled in PhpStorm. These plugins are bundled and enabled by default, but if you disabled them for any reason, now would be a good time to re-enable them. Also, make sure that your Docker containers are up and running.
Setting up the CLI interpreter
Now, inside PhpStorm, go to File > Settings > PHP
and add a new CLI interpreter by clicking on the ellipsis (...):
To configure the CLI interpreter from the Docker compose file, click the plus icon to add a new interpreter, select From Docker, Vagrant, VM, WSL, Remote...
and go to the Docker Compose
tab:
Setting up a Docker server
While in that tab, we are going to create a new Docker server configuration in another modal. Click on the New...
button, add a server name, and choose a connection.
There are several connections to choose from, and depending on your OS, for the first option, you might get something like Docker for Windows
, Docker for Mac
, or Unix socket
(for Linux). In this example, I'm going to use WSL
, but if you have another setup or, for some reason, WSL integration doesn't work for you in the end, remember to return to this modal (I'll explain where to find it later on) and change the connection. Docker for Windows
and SSH
(you can refer to the previous article on how to setup an SSH connection in PhpStorm) options worked well for me in some other cases. Here is what that setup might look like in the end:
After you save all of the settings in the Docker server connection modal, we are back in the Docker compose
tab of the CLI interpreter.
What is left in our current modal is to select the local docker-compose.yml
file, which is in my case in the root of my project, and select a service/container that is running PHP, which is laravel.test
by default when using Laravel Sail. Save all of the settings, and you should be good to go.
The Services tab should open automatically, and there you can connect to the Docker and, for example, access the PHP container and interact with it:
Troubleshooting
If you can't connect to Docker containers, or the PHP CLI interpreter is telling you it can't find the PHP executable, go back to Docker server settings (File > Settings > Build, Execution, Deployment > Deployment > Docker
) and try using another connection, like SSH
or Docker for Windows/Mac/Unix socket
:
Conclusion
We made it! We are able to setup PhpStorm integration with Docker to easily start and stop Docker containers and access their terminals directly from the services tab inside PhpStorm. Also, this setup allowed us to use the PHP from our Docker container as our project's PHP CLI interpreter. Pretty neat!
Enjoyed the article? Consider subscribing to my newsletter for future updates.
Originally published at geoligard.com.
Subscribe to my newsletter
Read articles from Goran Popović directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Goran Popović
Goran Popović
Software developer, enjoys working with Laravel, blogging at https://geoligard.com.