I want to run multiple/long commands for my service(s) in my
file, like below:
However it won’t work since it’s not possible.
One solution is that putting all your commands in a script file -let’s say
start.sh, put it into your image, build with it and call it directly.
As an example here is a
docker compose file:
Don’t bind your host’s
0.0.0.0 wildcard adress in production environment.
(Actually instead of deploying via
docker-composein production you should
consider to use more reliable, and robust distributed systems like
but this is a topic for another post.)
0.0.0.0:6379:6379, so you open your redis instance to whole
open world. And if you don’t use
AUTH or don’t take required security
precaution -like TLS etc. you may get some headache. So use it only if you
know the related consequences.
For more redis security topic look at this: Redis Security
And as an example here is content of
#!/usr/env/bin bash # start set -o errexit set -o pipefail set -o nounset python /app/manage.py collectstatic --noinput python /app/manage.py migrate # WARNING: Not a good idea for prod gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app
Migration is not a command you want to run every container start but since it’s staging environment let’s assume it’s OK.
Running in a script file is OK however maybe it’s local or staging environment which means you need to debug and tweak these commands one by one; maybe some flags are not working etc. and you don’t want to re-build the image every time.
How are we gonna do this?
The answer lines in the multiline capability of
command: > bash -c "python /app/manage.py collectstatic --noinput && python /app/manage.py migrate && gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app"
This is called Folded Style. The indention in each line will be ignored. A
line break (
\n) will be inserted at the end.
Or you can use Literal Style:
command: - /bin/bash - -c - | python /app/manage.py collectstatic --noinput python /app/manage.py migrate gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app
This turns every newline within the string into a literal newline, and adds one at the end.
More on these styles:
Key: > this is my very very very long command
this is my very very very long command\n
Mind new line-
\n (end of line-
Key: | this is my very very very long command
this is my very very very\nlong command\n
Mind new lines-
\n, and compare to it above output.
Fore more detailed style and
yaml syntax you can look at this amazing
explanation in this link.
These were the solutions for running multiple or long commands in your docker-compose file.