PhpStorm with Docker
Tags: php, docker, phpstorm, webdev
Published: 2023-10-09 01:14:09

---
canonical_url: https://seankegel.com/phpstorm-with-docker
---
## Learn how to use Docker in PhpStorm with Laravel Sail

I have come across developers on social media and at various jobs having trouble setting up their editors when Docker is involved. In this article, I will go over [PhpStorm](https://www.jetbrains.com/phpstorm/) and how to use it properly with Docker. When using Docker, it is usually best to run various scripts and binaries from the Docker container versus running locally. This includes linters, formatters, and even tests.

In this article, I will use a [Laravel](https://laravel.com) application with [Laravel Sail](https://laravel.com/docs/10.x/sail) as an example for configuration PhpStorm.

## Laravel Sail Installation

Use the following command to spin up a new Laravel application with Sail:

```bash
curl -s "https://laravel.build/phpstorm-docker" | bash
```

This can take several minutes to complete. Once it is done, run `sail up` to start the application.

```bash
/vendor/bin/sail up
```

> **Quick Tip:** Add a relative folder to your PATH to enable calling binaries from Composer and Node by using the name of the binary instead of the whole path. In your `.zshrc` or `.bashrc`, add the following:
> 
> ```bash
> export PATH="./vendor/bin:$PATH"
> export PATH="./node_modules/.bin:$PATH"
> ```
> 
> Then, instead of calling `/vendor/bin/sail up`, it is now possible to call `sail up`!

## Set the PhpStorm PHP Interpreter

Open the settings window and go to the PHP section. From there, click the `...` next to the CLI Interpreter box.


![Add new interpreter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qm1eagz9pbdp1aiic4fj.png)

You may have a local PHP interpreter set up. Now you’ll want to add the Docker interpreter. Use the `+` in the top left and select the “From Docker, Vagrant, VM, WSL, Remote…” option.

![Add new interpreter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kc34mc6bq3v6xy70nxdn.png)

In the next popup, select the “Docker Compose” option. This shows a list of available services included in the `docker-compose.yml` file. Select the `laravel.test` service, which is the PHP container for Sail.

![Select service from Docker Compose file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uygc22xucn91dkr4084z.png)

Now, the new PHP interpreter should be set as the default for the project.

![New interpreter set](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/giyhh5vc46g00j61oizp.png)

With the interpreter setup, let’s look at running [Laravel Pint](https://laravel.com/docs/10.x/pint) to format files.

## Running Linters and Formatters in Docker Container

For this article, I will look at running Laravel Pint. However, this same method can be used for PHP CS Fixer, PHPStan, and other quality tools. Later, I will also go through how to do this with Node and tools like Prettier.

### Laravel Pint

To start:

1. Go to Settings &gt; PHP &gt; Quality Tools.
    
2. Expand the Laravel Pint section and turn on the Pint.
    
3. Set the configuration to the interpreter pointing to the Docker container, in my case, it is `laravel.test`.
    
4. Click the three dots `...` to set up Pint for the interpreter.

![Set up Laravel Pint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wewbbm4jqaoe516jdv2.png)

After clicking the three dots `...`, a new window opens to allow you to add a new Pint configuration.

![Create a new Laravel Pint configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grkqnjqb718nye1q1d8y.png)

1. Click the plus icon `+` in the top left.
    
2. Select the interpreter pointing to the Docker container.

After selecting the interpreter and hitting "OK", the configuration should be created. The next step is to set the path and validate.

1. In the "Laravel Pint path" field, add `vendor/bin/pint` to point to the Pint binary in the Composer vendor directory.
    
2. Click the "Validate" button.
    
3. If everything is correct, the Pint version should be shown at the bottom.

![Set Laravel Pint path](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ykvzwf9v2mkkkmi5lfx.png)

With this set, Pint needs to be set as the external formatter. Back in Settings &gt; PHP &gt; Quality Tools, scroll to the bottom and click the radio for "Laravel Pint" in the "External formatters" section.

![Set Pint as the external formatter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kab4hka8rf4zm01iu77a.png)

Now, the code can be reformatted using Laravel Pint from the Docker container.

![Reformat code with Docker Laravel Pint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0grvbcih6cradcpng3sx.png)

In the example above, the incorrect code is underlined with a squiggly line because it does not follow the formatting set by Laravel Pint. After formatting, the class and method braces are fixed, braces are added to the conditional, and a blank line is added above the return statement.

To make this even easier, enable formatting on save. To accomplish this, go into Settings &gt; Tools &gt; Actions on Save and check the box for "Reformat Code".

![Reformat code on save](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxne57s7pvddkcul5wiu.png)

Now, anytime a file is saved, it will automatically be formatted by Laravel Pint, running on the Docker container.

### Prettier

First, install Prettier using the following command:

```bash
sail npm install --save-dev --save-exact prettier
```

Notice the command was prefixed with `sail`, which installs Prettier using the version of Node and npm on the Docker container versus the local version.

Next, similar to PHP, create a Node interpreter that points at the Docker version. Go to Settings &gt; Languages & Frameworks &gt; Node.js. From there, click the three dots `...` next to "Node interpreter".

![Create a new Node interpreter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mhesf1113ex3buv4afo.png)

In the new window, click the plus `+` button in the top left. Then select "Add Remote...".

![Add remote Node interpreter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/keixa168bef7fj407oic.png)

Just like before with PHP, select the "Docker Compose" option and select the `laravel.test` service.

![Configure the Node.js Remote Interpreter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlx0s5or6gxae7ps6qtd.png)

Node is now pointing at the version in the Docker container. To continue with Prettier, go to Settings &gt; Languages & Frameworks &gt; JavaScript &gt; Prettier.

![Set Prettier configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zashuxxajua5k4cf1djw.png)

In the Prettier settings:

1. Select "Automatic Prettier configuration".
    
2. Check "Run on save" for automatic formatting when saving a file.
    
Now, Prettier is all setup and running right from the Docker container. These same steps can be followed to run ESLint.

## Running Tests from Docker Container

For this article, I will be using PHPUnit. However, the same steps will also apply to Pest and JavaScript test runners like Jest.

Since `laravel.test` is already set as the default interpreter for the project, the tests should automatically run using PHPUnit from the Docker container. This can be seen by running a test:

![Run a test output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7veucz6wjsbt5m7h748.png)

After running the test, the test output shows the command. You can see it was run using `docker-compose` pointing at the `laravel.test` service of the local `docker-compose.yml` file.

If, for some reason, this is not working, maybe an existing project with other configurations, or an alternate test framework, you can continue with the next steps.

Go into Settings &gt; PHP &gt; Test Frameworks. Then, click the plus `+` button to add a new test framework.

![Create a remote test framework](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wuski3ipp5flfj5h8vbt.png)

Select the `laravel.test` interpreter from the new window:

![PHPUnit remote interpreter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1z04ebkiudd1sxrt5zq2.png)

Now, a new PHPUnit Test Framework is available.

![Remote PHPUnit test framework](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pg179ulu9sb1mnyma7on.png)

Next, modify the run configurations by going to the Run menu and selecting "Edit Configurations...".

![Edit run configurations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyhwy07gwcrtdrdy14e5.png)

From the new window, click the gear icon next to the "Use alternative configuration file" field.

![Set PHPUnit test framework configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42kwmpq1b5uupzomo3vj.png)

This opens a new window to select the new remote PHPUnit configuration.

![Set remote interpreter for tests](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/035fxovj4r0s25id4g24.png)

Finally, make sure the configuration is pointing to the `laravel.test` interpreter or the default interpreter if it is set as `laravel.test`.

![Set remote interpreter for tests](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rb3x6zu25kltgudq9sbl.png)

That's it! Everything should now be set up to run PHPUnit from the Docker container.

## Package Managers

You can even run your package managers like Composer and npm from the Docker container.

### Composer

Go to Settings &gt; PHP &gt; Composer, then:

1. Select the radio for "Remote Interpreter".
    
2. Select `laravel.test` as the "CLI Interpreter".
    

![Run Composer from Remote Interpreter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlys3kphvx1i91tuhce3.png)

With Composer set, go to Tools &gt; Composer, and select a command that will be run from the Docker container.

![Composer commands in PhpStorm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dt6ap0i1jl2g7fgkl9v2.png)

### npm

For npm, the remote Node interpreter should have already been set up from the Prettier steps above. Now, open the npm tool window.

![npm Tool window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67awreg0gahfq31vkqhs.png)

The npm tool window will show all the scripts configured in the `package.json` file. Right-click on the `dev` script and click "Edit 'dev' Settings...".

![Edit npm configurations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifo894kwgsuitym0gjky.png)


With the new window open:

1. Set the "Node interpreter" to the Docker compose `laravel.test` interpreter if it is not already set.
    
2. Add a new environment variable: `WWWUSER=sail`. This prevents PhpStorm from trying to run the npm commands as the `npm` user.
    

![npm run configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9caw8u01671tqknkayz.png)

Now, the `dev` script can be run from the npm tool window and the output is shown in PhpStorm.

![npm script running in PhpStorm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1groqsc8tkp8v591k5en.png)

## Conclusion

I hope this is helpful for anyone working on a project with a Docker configuration. Though running these scripts and binaries from Docker can be slower than running locally, it does have the benefit of making sure all the developers on the project are running the same versions. This should help prevent the infamous "it runs on my computer" type issues. By using Docker, these various languages, scripts, and binaries also do not need to be installed locally. I can have all of this running without even having Node installed on my local machine.

Thanks for reading! Please let me know if you have any questions in the comments.