How do I execute multiple commands using docker-compose

+3 votes

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,980 points
109,745 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..

4 answers to this question.

+1 vote
Best answer

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
• 7,340 points

selected Dec 14, 2020 by MD
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, 2019 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, 2019 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.
0 votes

Hi,

You can simply use bash -c in your script.

command: bash -c "python manage.py migrate && python manage.py
answered Dec 14, 2020 by MD
• 95,440 points

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, 2019 in Docker by Sirajul
• 59,230 points
9,306 views
0 votes
1 answer
+2 votes
12 answers

How to run multiple commands in docker at once?

In order to run multiple commands in ...READ MORE

answered Jul 19, 2018 in Docker by Sophie may
• 10,610 points
247,836 views
+1 vote
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
• 52,360 points
1,422 views
+2 votes
1 answer
+2 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
• 58,090 points
2,435 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
• 7,340 points
1,258 views
+2 votes
6 answers

copy directories in docker container excluding any one directory

One of the way could be copy ...READ MORE

answered Dec 10, 2018 in Docker by Prateek
32,985 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP