Building and migrating a django container with compose

There is probably a laughably simple solution to this I’m sure - but it’s had me stumped for the last couple of hours so if anyone can help I’d really be grateful.

I’m trying to set up two containers with docker compose, one postgres and the other django. Postgres is off the shelf, the django one is a fairly simple custom build. I’m using the ‘links’ setting to link the django container to the postgres one:

version: '2' services: db: image: postgres web: build: . privileged: true command: supervisord -n ports: - "8000:80" links: - db depends_on: - db

I’m using these settings in Django to connect to postgres:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'postgres', 'USER': 'postgres', 'HOST': 'db', 'PORT': 5432, } }

And the setup works … except I can’t figure out how to apply the initial migrations when building the django (web) container. If I add the migrate command into the Dockerfile, it complains that the ‘db’ service is unknown because the db service hasn’t been started. django-compose up doesn’t update the build either so adding the commands to the Dockerfile then doesn’t work either.

Is there some way to kick off a service that the container depends on when building it that I’m missing?

1 Like