4

I raise a container with my Django-based App and another container with PostgreSQL, linked to each other network-wise, using docker-compose.

If I run the makemigrations outside of a bash session inside my application docker container (linked to the PostgreSQL container), like this:

python manage.py makemigrations myapp

I get the following exception:

django.db.utils.OperationalError: could not
 translate host name "db" to address: nodename 
nor servname provided, or not known

Which makes sense: I don't have any instance (and configuration) of PostgreSQL outside of the container execution context.

From what I understand from past experience with other languages, and also other posts like this one it would be desirable for me to run makemigrations and commit it.

Which would be the best approach to create and commit the migration files:

  • Should I try to expose the PostgreSQL container and change the settings.py DATABASE to be able to run the makemigrations command from a local bash session?
  • Should I copy the makemigrations result executed from inside the container, into my repository and then commit it?
  • Should I not worry about not commiting the migrations, and instead make them and run them with a docker-entrypoint.sh approach (example)?
  • Any other option?
Filipe Freire
  • 823
  • 7
  • 21

1 Answers1

5

it would be desirable for me to run makemigrations and commit it.

You're right, migrations should always be committed to the repo.

Should I try to expose the PostgreSQL container and change the settings.py DATABASE to be able to run the makemigrations command from a local bash session?

I wouldn't do it personally.

How to approach near to seamless integration between local code and the docker is a question which has many answers. I like to mount my code to the container as a volume, and then, when you'll run makemigrations in the container, created migrations will appear on your machine as well.

Something like this in the docker-compose.yml

  app:
    ...
    volumes:
      - path/to/local/src/:/src/
    working_dir: /src

where /src is the folder with your manage.py. Now, when you do

docker exec -it container_name python manage.py makemigrations it'll create migration file in your local machine and you won't have problems with copying it back and forth.

Beware file permissions though, as in this case migrations might be created with root owner depending on your host OS. But it's for another question I guess.

valignatev
  • 6,020
  • 8
  • 37
  • 61