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
I run pre-startup stuff like migrations in a separate ephemeral container, like so (note, compose file has to be of version '2' type):
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:
Same example in multilines:
Another idea:
If, as in this case, you build the container just place a startup script in it and run this with command. Or mount the startup script as volume.
I recommend using
sh
as opposed tobash
because it is more readily available on most unix based images (alpine, etc).Here is an example
docker-compose.yml
:This will call the following commands in order:
python manage.py wait_for_db
- wait for the db to be readypython manage.py migrate
- run any migrationspython manage.py runserver 0.0.0.0:8000
- start my development server