[Fixed]-Linking Django and Postgresql with Docker

17πŸ‘

βœ…

The Dockerfile for your Django image should not expose port 5432 as no Postgresql server will be running in any container created from that image:

FROM python:3-onbuild
EXPOSE 8000
CMD ["/bin/bash"]

Then as you are running the Django container linking it with

--link my-postgres:my-postgres

your settings for the database are incorrect.

In the Django container: 127.0.0.1 refers to the Django container which isn’t running any service listening on port 5432.

So your settings.py file should be:

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

As you run your Django container with:

sudo docker run --link my-postgres:db -v /home/mpmsp/project/ezdict/ezbkend:/usr/src/app -name my-app -i -t my-app

then your settings.py file would have to be:

  DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'mydb',                      
       'USER': 'postgres',
       'PASSWORD': '',
       'HOST': 'db',                      
       'PORT': '5432',                      
    }
  }
πŸ‘€Thomasleveil

0πŸ‘

Ths syncdb only works AFTER both db and django containers are build and started, then you can manually run the syncdb command with fig/docker-compose/docker.
I am thinking of creating an AT job and let the container run the syncdb itself (and creating an admin user after the syncdb – for creating the necessary tables)

πŸ‘€Michiel

Leave a comment