Logo

dev-resources.site

for different kinds of informations.

Interact with Docker containers without leaving PhpStorm

Published at
3/5/2024
Categories
docker
windows
wsl
phpstorm
Author
geoligard
Categories
4 categories in total
docker
open
windows
open
wsl
open
phpstorm
open
Author
9 person written this
geoligard
open
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 (...):ย 

PHP settings

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

Add remote interpreter

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

Docker server setup

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

Docker services

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

Update Docker server

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.

phpstorm Article's
30 articles in total
Favicon
How to configure PHPStorm to work with Vite - Aliases
Favicon
Cursor AI Editor vs PhpStorm - a quick Laravel comparison
Favicon
Snapshots for AI: A โ€œRAG-Likeโ€ solution for programming with LLMs
Favicon
Como instalar o PHPStorm no WSL2
Favicon
Enhance PhpStorm File Templates with Velocity ๐Ÿงช
Favicon
Simple DX improvement in PHPStorm
Favicon
Interact with Docker containers without leaving PhpStorm
Favicon
Docker on WSL with PhpStorm - Best of both worlds
Favicon
Phpstorm Intellij search mode when focusing
Favicon
Simplify API Testing with PhpStorm HTTP Requests
Favicon
Running HTTP requests in PhpStorm
Favicon
PhpStorm with Docker
Favicon
How to select a File Using Ctrl+P In PHPstorm as I did in VSCode and VSCodium
Favicon
Automatic Git Commit and Push on Save using PHPStorm
Favicon
XDebug, PHPStorm, Docker, macOS Ventura
Favicon
Laravel Sail & XDebug
Favicon
Atalhos para ser mais produtivo no PHPStorm
Favicon
Comment Publier Rapidement un Projet sur GitHub
Favicon
Impressions on GitHub Copilot and PHPStorm - March 2023
Favicon
Using Laravel Sail alongside PhpStorm
Favicon
Pair programming in PHPStorm
Favicon
PHPStorm Tips #10 : Last Edit Location
Favicon
PHPStorm Tips #9 : Zoom ton code
Favicon
PHPStorm Tips #7 : Naviguer dans le YAML
Favicon
My barebones React component snippet
Favicon
PHPStorm Tips #5 : Toute la pouissance des Live Templates
Favicon
PHPStorm Tips #6 : Les contextes
Favicon
PHPStorm Tips #8 : Rechercher et ouvrir plusieurs fichiers
Favicon
PHPStorm Tips #2 : Select Occurences
Favicon
PHPStorm Tips #4 : Extend Selection

Featured ones: