Run Symfony commands inside a docker container

Running a Symfony app inside a docker’s container has a lot of advantages, but the downside is we have to type a longer command if we want to run the command inside the container. For this purpose, I wrote a small bash script to help me with that. By using this script instead of typing:

docker exec -it app_php-fpm_1 bin/console doctrine:migrations:migrate

we only type:

./sf dt:mg

Please note that I’m not a professional bash programmer, so there might be a better way to do that. For example, using the arrow key to find the latest command in your history might work for you, or if you are more pro, you would use CTRL + R to search inside your history.

Extract the container’s name

docker-compose ps php-fpm | tail -n +2 | awk ‘{print $1}’

In this command, the php-fpm is your service name. For example, if your docker-compose.yaml is like this:


You have to use this command:

docker-compose ps php | tail -n +2 | awk ‘{print $1}’

First, docker-compose ps php-fpm gives us some information about the php-fpm service, then it sends the result to the tailcommand. Then tail -n +2 removes the first line which contains NAME, COMMAND, …, and finally awk returns the first column, which is our container name:


Bash script

Create a file and name it, and add the following lines into it:

Our script has a default service name which is stored in $SN variable. You need to change its value if your service name is different.

Now make the file executable:

chmod a+x

now run a Symfony command:

./ doctrine:migrations:list

Add aliases

./ doctrine:migrations:list

But, what if we could create an alias for each Symfony command. By alias I mean instead of doctrine:migrations:migrate we could write ./sf dt:mg?

Open your bash script, and add the following lines in it:

The case expression check the first argument that is passed to the script and map it the corresponding command. It means the first argument can be a real Symfony command, or an alias. All other arguments will be passed to the bin/console command as it is. For example, all the following commands are valid:

$ ./sf dt:mg
$ ./sf doctrine:migrations:migrate
$ ./sf dt:mg –-dry-run --env=prod