How do I execute multiple commands using docker-compose

+1 vote

I have been trying to run multiple commands:

db:
  image: postgres
web:
  build: .
  command: python manage.py migrate
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db

Can anyone help me with this?
Thanks
Sep 20, 2018 in Docker by Damon Salvatore
• 5,510 points
9,049 views

What commands do you want to run? 

  command: python manage.py migrate
  command: python manage.py runserver 0.0.0.0:8000

Is it these two? Because this should work IMO. If you're getting an error while running this compose file, do share the log here..

3 answers to this question.

0 votes

It can be solve by using bash -c command.

Here is how it can be done.

command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"

An example in which we are giving commands in multiple line in a docker-compose file:

command: >
    bash -c "python manage.py migrate
    && python manage.py runserver 0.0.0.0:8000"

I hope it will resolve your query.

answered Sep 20, 2018 by shubham
• 6,890 points
This solution only works if your image has bash installed. I was stuck on it for a while until I figured this out.
Yeah, most of the Alpine-based images do not have bash installed. You can use @Jigger's solution. sh -c works perfectly fine.
0 votes

If your image does not have bash installed as mentioned by @Bonsa, you can try  this:

sh -c "command"
answered Apr 22 by Jiggar
0 votes

You can also use the depends_on option in docker-compose. It's probably not a very clean approach but just an idea.

db:
  image: postgres
web:
  image: app
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db
  depends_on:
    - migration
migration:
  build: .
  image: app
  command: python manage.py migrate
  volumes:
    - .:/code
  links:
    - db
  depends_on:
    - db

answered Apr 22 by Kavya
What does the depends_on do?
depends_on makes sure you execute commands in the correct order. This is especially useful when yiu execute multiple commands at the same time.

For example, you want to start a database and then deploy a webpage, this order can be controlled.

Related Questions In Docker

0 votes
1 answer

How do I install docker-compose on linux?

Docker Compose is a tool for defining ...READ MORE

answered Jul 2 in Docker by Sirajul
• 19,670 points
170 views
0 votes
1 answer
+1 vote
11 answers

How to run multiple commands in docker at once?

Try this: docker run image bash -c "cd ...READ MORE

answered Apr 17 in Docker by anonymous

edited Apr 17 by Kalgi 14,595 views
0 votes
1 answer

How do I pass environment variables to Docker containers?

You can pass environment variables to your ...READ MORE

answered Jul 20, 2018 in Docker by Kalgi
• 39,330 points
58 views
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 56,520 points
136 views
0 votes
1 answer

How do I scale in Docker Swarm Mode W/Terraform Digital Ocean Load Balancing

The solution you could build for Digital ...READ MORE

answered Jun 19, 2018 in Docker by shubham
• 6,890 points
188 views
+2 votes
6 answers