Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Docker-Compose, how to execute multiple commands

I want to do something like this where I can run multiple commands in order.

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 
like image 586
RustyShackleford Avatar asked May 05 '15 21:05

RustyShackleford


People also ask

Can I have multiple commands in docker compose?

You can now navigate into the project and list the project files. This article highlighted how one could execute multiple commands in the docker-compose file using sh . Alternatively, you can also use bash -c as shown below.

How do I run two commands in docker?

Multiple commands can be executed in a running Docker container using the docker exec command. If the Docker container is stopped, before running the docker exec command it should be started using the docker run command.

How do you handle multiple docker compose?

Running Multiple Copies of a Single Compose Project For times when you need multiple copies of environments with the same composition (or docker-compose. yml file), simply run docker-compose up -p new_project_name .

Does docker compose run multiple containers?

With Docker compose, you can configure and start multiple containers with a single yaml file.


2 Answers

I run pre-startup stuff like migrations in a separate ephemeral container, like so (note, compose file has to be of version '2' type):

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 

This helps things keeping clean and separate. Two things to consider:

  1. You have to ensure the correct startup sequence (using depends_on).

  2. You want to avoid multiple builds which is achieved by tagging it the first time round using build and image; you can refer to image in other containers then.

like image 30
Bjorn Stiel Avatar answered Sep 21 '22 21:09

Bjorn Stiel


Figured it out, use bash -c.

Example:

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

Same example in multilines:

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

Or:

command: bash -c "     python manage.py migrate     && python manage.py runserver 0.0.0.0:8000   " 
like image 91
RustyShackleford Avatar answered Sep 22 '22 21:09

RustyShackleford