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
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.
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.
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 .
With Docker compose, you can configure and start multiple containers with a single yaml file.
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:
You have to ensure the correct startup sequence (using depends_on).
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.
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 "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With