I would like to run a dockerized Django app with a dockerized postgres.
I run the dockerized Django app by using:
docker run --rm --env-file /path/to/variables -d -p 8000:8000 django_app:test
I run a dockerized postgres by using:
docker run --rm -d --env-file /path/to/secrets/variables -p 5432:5432
-v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf
--mount src=/path/to/db/data,dst=/var/lib/postgresql/data,type=bind
postgres:alpine -c 'config_file=/etc/postgresql/postgresql.conf'
my postgres config is the default config that is suggested in the postgres docker container documentation. It is essentially a config file that contains listen_addresses = '*'
I use the same environment variables for both containers:
DJANGO_SETTINGS_MODULE=settings.module
PROJECT_KEY=xxyyzzabcdefg
DB_ENGINE=django.db.backends.postgresql
POSTGRES_DB=db_name
POSTGRES_USER=db_user
POSTGRES_PASSWORD=verydifficultpassword
POSTGRES_HOST=localhost # I've also tried to use 0.0.0.0
POSTGRES_PORT=5432
My Django settings module for the database is:
DATABASES = {
'default': {
'ENGINE': os.environ.get('DB_ENGINE'),
'NAME': os.environ.get('POSTGRES_DB'),
'USER': os.environ.get('POSTGRES_USER'),
'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
'HOST': os.environ.get('POSTGRES_HOST'),
'PORT': os.environ.get('POSTGRES_PORT')
}
}
However, I keep on getting:
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
The Dockerfiles for my django app looks like:
FROM python:alpine3.7
COPY --from=installer /app /app
# required for postgres
COPY --from=installer /usr/lib /usr/lib
COPY --from=installer /usr/local/bin /usr/local/bin
COPY --from=installer /usr/local/lib /usr/local/lib
ARG SETTINGS_MODULE
WORKDIR /app
ENTRYPOINT python manage.py migrate &&
python manage.py test &&
python manage.py create_default_groups &&
python manage.py set_screen_permissions &&
python manage.py create_test_users &&
python manage.py init_core &&
python manage.py runserver 0.0.0.0:8000
Another interesting fact is that if I run the app locally python manage.py runserver
and have the postgres container running, the app seems to be working.
Can anyone help me try to figure out why am I getting a connection refused? Thanks in advance!
See Question&Answers more detail:
os