How to run bash scripts on a docker image using "Run/debug configurations" window?

I successfully setup PHP on docker with PHPStorm.

I successfully starts my docker image using the "Run/debug" window.

I successfully launch composer script from this window, and the composer script is executed on the docker container that I chose and configured.

But I don't know how to configure bash scripts in this window AND how to execute them into a specific container. They only run outside my docker containers, in my hosting machine.

1
2 comments

In fact, Shell Script run configuration does not have a Docker support. However, you still can 'exec' scripts for specific containers in "Services" tool window:

1

To get the effect you might be looking for, use the “Script Text” option. And start your command with the same command that would work in any other terminal for executing a script within a container:

docker exec -it {CONTAINER_NAME} {YOUR_COMMAND}

As a matter of fact, the “Terminal” button you'll find in the container portion of the “Services” panel that opens up a terminal within the container is more than likely just running this under the hood to achieve that:

docker exec -it {CONTAINER_NAME} bash

Keep in mind, the directory path of the command is going to be relative to the directory structure within the container, defined by your mounts. So you may need to supply the absolute path of your script:

docker exec -it php-fpm-1 /var/www/html/my_awesome_project/scripts/my_awesome_script --some-argument

 

Plug that into your Run/Debug -> “Script Text” config and you'll be good to go. You can even do things like tell a PhpUnit config to execute the script “Before” running the tests. This can be handy for things like building a test-database. Or you could use the “Before” section of a “Remote Debug” configuration against a script config like that to activate xDebug, if it's not running by default.

0

Please sign in to leave a comment.