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

In order to extract the container’s name, we can use the following set of commands:

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:

services:
php:
image:php:7.4
...

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:

api_php-fpm_1

Bash script

Now that we have the container’s name, we can use it to run any command inside the container.

Create a file and name it sf.sh, 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 sf.sh

now run a Symfony command:

./sf.sh doctrine:migrations:list

Add aliases

Now that we get rid of the docker exec -it [container_name] and we can run a Symfony command like this:

./sf.sh 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

Summary

We created a bash script to simplify the process of running a Symfony command inside a docker container. Although it might not be the best way to do so, it helps us to write shorter commands and learning bash script. You can modify the aliases, or use a completely different method.

--

--

--

My ideas, thoughts, and tutorials about life, internet, and programming. https://github.com/smoqadam

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

http://h5.shijie.io/#/pages/register/register?share_id=284883

Picking the right message encoder

The perfect web stack? (aka: typing your APIs)

The hard truth about the full stack developer. Myths and lies.

React Finland 2018 — Lessons Learned

Integrating LVM with Hadoop and providing Elasticity to Data Node Storage

Making Waves

Creating a Django-Oscar App with Dashboard (Part 1)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Saeed

Saeed

My ideas, thoughts, and tutorials about life, internet, and programming. https://github.com/smoqadam

More from Medium

Build Your Symfony Applications with ease using Ecotone

Calculate PHP cache time with ease

Object Validator for PHP 8

Introduction to RabbitMQ and Symfony