Interact with Docker containers without leaving PhpStorm
Tags: docker, windows, wsl, phpstorm
Published: 2024-03-05 00:19:24

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: <a title="Docker on WSL with PhpStorm - Best of both worlds" href="https://dev.to/geoligard/docker-on-wsl-with-phpstorm-best-of-both-worlds-194f" target="_blank">Docker on WSL with PhpStorm - Best of both worlds</a>.

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.

<br>
<h2 class="subheading" id="requirements">Requirements</h2>
<br>

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.

<br>
<h2 class="subheading" id="setup-cli-interpreter">Setting up the CLI interpreter</h2>
<br>

Now, inside PhpStorm, go to `File > Settings > PHP` and add a new CLI interpreter by clicking on the ellipsis (...): 


<a title="PHP settings" href="https://geoligard.com/storage/posts/December2023/php-settings.png" target="_blank"><img title="PHP settings" src="https://geoligard.com/storage/posts/December2023/php-settings.png" alt="PHP settings" width="100%" height="auto"></a>


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: 


<a title="Add remote interpreter" href="https://geoligard.com/storage/posts/December2023/add-remote-interpreter.png" target="_blank"><img title="Add remote interpreter" src="https://geoligard.com/storage/posts/December2023/add-remote-interpreter.png" alt="Add remote interpreter" height="auto" style="max-width: 100%"></a>


<br>
<h2 class="subheading" id="setup-docker-connection">Setting up a Docker server</h2>
<br>

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: 


<a title="Docker server setup" href="https://geoligard.com/storage/posts/December2023/docker-server.png" target="_blank"><img title="Docker server setup" src="https://geoligard.com/storage/posts/December2023/docker-server.png" alt="Docker server setup" height="auto" style="max-width: 100%"></a>


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: 


<a title="Docker services" href="https://geoligard.com/storage/posts/December2023/docker-terminal.png" target="_blank"><img title="Docker services" src="https://geoligard.com/storage/posts/December2023/docker-terminal.png" alt="Docker services" width="100%" height="auto"></a>


<br>
<h2 class="subheading" id="troubleshooting">Troubleshooting</h2>
<br>

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`: 


<a title="Update Docker server" href="https://geoligard.com/storage/posts/December2023/docker-server-update.png" target="_blank"><img title="Update Docker server" src="https://geoligard.com/storage/posts/December2023/docker-server-update.png" alt="Update Docker server" width="100%" height="auto"></a>


<br>
<h2 class="subheading" id="conclusion">Conclusion</h2>
<br>

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 <a href="https://geoligard.com/newsletter" target="_blank">newsletter</a> for future updates._

---

_Originally published at <a href="https://geoligard.com/interact-with-docker-containers-without-leaving-phpstorm" target="_blank">geoligard.com</a>._