Use composer and bin/console from your Docker image directly within PHPSTORM

Okay, you are like me. Your machine has nothing installed locally. Not even composer nor PHP in CLI!

While you use PHP and maybe even Symfony (or not), you need to use composer daily. You use PhpStorm every day and think it's a shame not to benefit from all the options of your IDE, especially everything related to composer and the Symfony console. So how do you configure PhpStorm to specify that the composer for your project is located in the docker image?

Here is an example of docker-compose.yml In actuality, you will use yours with your php/composer inside.

version: "3.8"
services:
  apache_server:
    image: 'devpartitech/php:7.4-apache'
    ports:
      - "80:80"
    volumes:
      - "./:/var/www/"
    extra_hosts:
      - host.docker.internal:host-gateway

Know that installing composer in your image is extremely simple, you just need to copy the composer from an already prepared image 😊

COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

So now how do we tell PhpStorm that we want to use /usr/local/bin/composer from our container?

Go to File > Settings > PHP > Composer

image-53

At this point nothing is done yet. So, you will check Synchronize ide Settings with composer.json and Remote Interpreter.

image-54

On the “…” from CLI Interpreter, you will create a new configuration.

image-55

Naturally, choose From Docker.

image-56

You will have several options available to you. You could directly link to a specific docker image. Personally, I choose my docker-compose which allows me to select the service I want to use. My docker-compose is extremely simple. It has only one service and everything I need is there.

image-57

Regarding the configuration, I personally choose to use the connection to the existing service since I use the docker-compose. If I had used a docker container directly, I would have used the “always start a new container” option. Do not forget to click on the refresh button at the end of General > PHP executable to confirm everything is present.

image-58

Eventually, you will have a small banner at the top when you open your composer.json file

image-59

You will have access to the options directly to install / update, show log lets you see what is really happening in your container. After your commands, you have access to options that allow you to launch more commands.

image-60

You also have the possibility to execute your scripts directly from the green arrow to the left of your code.

image-61

Now that our first step is configured, let's add composer and the Symfony console directly to PhpStorm's “Run Anything” system; the famous window that opens when you press CTRL twice.

Open the configuration window File > Settings, go to the configuration of Tools > Command line Tool Support and click on the small “+” to add a new configuration.

A configuration window should appear. Click on the small “+” to create a new configuration.

image-66

You will have a small configuration window like this with several choices, including composer and Symfony that we will both configure.

image-67

Let's start by configuring composer:

image-68

Once registered, you should have a small message indicating that several commands have been found. That's rather a good sign 😺

image-69

Personally, I changed the alias to put composer, by default PhpStorm will put “c”. We do the manipulation again but this time with Symfony.

image-70

As usual, choose your interpreter. I choose my container from my docker-compose, and I choose the desired version of Symfony. Honestly, if you haven't chosen 3.* 4.* there's a serious problem 😺 Note that if you have a hard time upgrading your Symfony application to the latest version, we know how to do it and we'll be delighted to help you ^^

image-71
image-72

Again, I change the alias. I prefer to put “console”. A matter of habit 😺

image-73

That's it! We are ready to test everything. Press CTRL twice. The “Run anything” window will open.

Type console and you will see all your Symfony commands appear. And as it's magic, they will execute directly in your container.

image-74