7

I have a sample django app that I am trying to get up and running using docker.

docker-compose up brings up the web, db and other containers along with links between them. But there are pre and post scripts that might need to be run..

example of pre-scripts in my scenario:

git
pip
docker
docker-compose
wget

example of post-scripts :

Database migrations, usually done manually using docker run web... after the containers are up and running.

Currently I have a deploy.sh at the root of app which follows logic like this..(I choose a ubuntu image when launching)

#assuming I always choose ubuntu base image
sudo apt-get install x
sudo apt-get install y
sudo apt-get install z
docker-compose build .; docker-compose up -d;
docker-compose run web "python manage.py makemigrations"

My questions:

1) what is the best way to run these commands?

2) Are database migrations run each time you deploy (from scratch?) - or is this issue taken care of by volumes?

1 Answer 1

4

You have two options:

  1. You can run these commands in the dockerfile for your images; as each dockerfile is run when compose is running - your images will have the results of these commands. This is particularly useful when you are doing os-level upgrades and configuration bootstrapping (like your apt-get commands).

  2. For runtime-level configuration (things you need to do once the system is up), use the command directive in your docker-compose.yml file. These would be your migrations (if you need to run them each time).

If you want to persist your data across runs of docker compose (that is, your data should remain when you restart the container); then you need either persistent mapping against your host or a data volume that's shared - which you can configure in your docker-compose.yml as well.

docker-compose will happily run whatever script you provide - it doesn't know if it needs to run it, its just executing commands. It is up to you to make sure your pre, post, bootstrap scripts are intelligent enough that they can be repeated even if their effective results are already applied.

Sign up to request clarification or add additional context in comments.

3 Comments

So what do you do if you have several application containers that depend on migrations? Does compose assume that your migrations should be able to run concurrently? Is there no way to say "wait for task db_migrations to stop before standing up service1"?
Since I wrote this answer (in 2015), much has changed; including docker-compose, which now supports many features such as setting dependencies of services which controls their start order.
I think depends_on is subject to the same problem. It only controls the order containers start, not whether or not some dependent task has finished. So if I have a 'setup db' container, that needs to run before the application containers are launched, I don't see a good way to do that. Your link cites things like 'wait-for' for waiting on TCP connections, but that's not really helpful for the case I cited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.