Run tests on existing docker container

Answered

It's possible to use an existing docker container to run the tests?

I followed the instructions in here to configure the docker integration, but when I run the tests PHPStorm creates a new container, like the image shows. The problem is that my app uses two containers, one for the php + apache, another for the oracle database. I'm using docker-compose to run those containers and connect them. When phpstorm creates a new container the app cannot access the database. 

What can I do?

Thanks

 

7
15 comments

You can link them using "Links" section in Container tab in Docker run configuration. Here's a doc where this can be found:

https://www.jetbrains.com/help/phpstorm/2016.3/run-debug-configuration-docker-deployment.html

See "Links" item.

 

0
Avatar
Permanently deleted user

Hi, Dmitry. Thanks for the fast reply.

The problem is I don't have control over which container PHPStorm will use to run the tests, so I cannot link it with other containers. Looks to me that this doc is not applicable to my problem. This configuration works nice with docker-compose, but is not used when running behat.

What I want is be able to provide the container where I want the test run. 

The screenshot show the command that will be used. I want something like:

docker://<my-container>/php /opt/.phpstorm_helpers [...]

Its possible?

 

3

Hi Luís, I've checked with our team. The configuration you're running isn't yet supported but is on a TODO list: https://youtrack.jetbrains.com/issue/WI-33800

 

Meanwhile you can try a workaround posted at http://binary-data.github.io/2016/06/15/running-integration-tests-phpstorm-phpunit-docker/

0
Avatar
Permanently deleted user

Hi Dmitry,

We too need to run our Behat from within an existing container, that's part of the app. Is there any update on when this might be possible from within PhpStorm?

Thanks,

 

Will

2
Avatar
Permanently deleted user

Leaving comment here to help others.

As of today, IntelliJ (with PHP plugin) still stops the service after running tests (using docker-compose).

I got around this by declaring "restart: always" on the "app" service - the service I use for working on the web app in dev.

version: "3"
services:

web:
...

app:
build:
context: ./
dockerfile: dockerfile.app.dev
working_dir: /var/www
volumes:
- ./:/var/www
restart: always

db:
...

Hope this helps someone.

 

1
Avatar
Permanently deleted user

The solution is to use a different docker-compose.yml, for exemple docker-compose-test.yml and use the same bridge network, (e.g. `my_app_net`)

Start the initial containers with `docker-compose.yml`

Inside PhpStorm, use the remote CLI Interpreters from 'Docker Compose' and use the `docker-compose-test.yml`

[docker-compose.yml]

version: "3.5"
services:
web:
image: <your-docker-image-for-the-app>
...
working_dir: /var/www
networks:
- my_app_net
ports:
- 80:80
volumes:
- ./:/var/www/

db:
image: ...
...
networks:
my_app_net:
aliases:
- mssql
ports:
- 1433

networks:
my_app_net:
driver: bridge
name: my_app_net

[docker-compose-phpstorm.yml]

version: "3"
services:
phpstorm_app:
image: <your-docker-image-for-the-app>
...
working_dir: /var/www
environment:
- PHP_IDE_CONFIG=serverName=local-app
volumes:
- ./:/var/www/

networks:
default:
external:
name: my_app_net

 

0
Avatar
Permanently deleted user

To make sure that "Docker Compose" integration for phpunit testing would start all needed containers needed set depends_on for appropriate service in docker-compose.yml:

version: "3.1"

services:
api:
container_name: app-api
build:
context: docker/api
dockerfile: dev.Dockerfile
depends_on:
- db
ports:
- 80:80
volumes:
- "./:/www"
- "./docker/logs:/logs"
db:
container_name: app-db
image: mysql:latest
command: "--innodb_file_per_table=1 --skip-name-resolve --max_allowed_packet=64M --lower_case_table_names=2"
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: "root"
volumes:
- "./docker/db-data:/var/lib/mysql"
- "./:/mnt/local" # just for db dump/restore
0

Yep, without this, Behat that uses any linked container does not work with Docker.

Behat talks to a MySQL container? Nope, you can't use PHPStorm to run your tests. Awesome.

2

Hi, any update on this? Is there any way to run tests with docker which connect to a database without need to install php or the database locally?
Btw @Dmitry Tronin  the link of the workarround is broken: http://binary-data.github.io/2016/06/15/running-integration-tests-phpstorm-phpunit-docker/

0

Running docker-compose works now: https://youtrack.jetbrains.com/issue/WI-33800

Re-use of containers isn't supported yet: https://youtrack.jetbrains.com/issue/WI-37986

 

>  the link of the workarround is broken: http://binary-data.github.io/2016/06/15/running-integration-tests-phpstorm-phpunit-docker/

Too bad, that's a 3rd party resource so I can't recover/adjust it in any way. The link was most probably related to a docker-compose support & you should no longer use any workarounds when using docker-compose.

1
Avatar
Permanently deleted user

In my case I use custom networks. I had to set the 'Settings>Languages>PHP>Docker Container>Network mode' to my network defined for my project. Make sure you don't add empty spaces in that input.

Use `$ docker network ls` to get a list of available networks and search for your network. Be careful, the network defined in docker compose is not the same as the network created by the docker itself so you can't use the network from the docker-compose.yml file. 

1

Is there any updates?

0

There actually is an update - since 2019.1, you can run your Docker Compose services in the docker-compose exec mode: https://youtrack.jetbrains.com/issue/WI-37986

0
Avatar
Permanently deleted user

In 2020.3, the "Connect to existing container" option doesn't seem to work: https://youtrack.jetbrains.com/issue/WI-59139

I've made a workaround tool called PhpStorm Docker proxy that I'm using until PhpStorm supports this use case.

0

Please sign in to leave a comment.