I have a Django project with PostGIS database and I need to dockerize this project. This is what I have for now. In settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': os.environ.get('pgdatabase'),
'USER': os.environ.get('pguser'),
'PASSWORD': os.environ.get('pgpassword'),
'HOST': os.environ.get('pghostname'),
'PORT': '5432',
}}
Dockerfile:
FROM python:2.7
WORKDIR /orion-amr/
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt ./requirements.txt
RUN pip install -r requirements.txt
RUN apt-get update
RUN apt-get install -y binutils libproj-dev gdal-bin
COPY . /orion-amr/
docker-compose.yml :
version: '3'
services:
db:
image: kartoza/postgis:12.0
environment:
- POSTGRES_DB=amr_or
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
ports:
- "5432:5432"
web:
build: ./
command: bash -c "python manage.py migrate && python manage.py collectstatic --noinput --i rest_framework && gunicorn orion-amr.wsgi:application --bind 0.0.0.0:8000 --workers=2"
ports:
- "8000:8000"
env_file:
- ./.env.dev
depends_on:
- db
.env.dev :
SECRET_KEY=z)mf(y#w7-_8y1)0(v*n@w@lzf)!0=g_rj5$%1w6g-t!7nbk05
PGDATABASE=amr_or
PGPASSWORD=root
PGHOSTNAME=db
PGUSER=postgres
DEBUG=
Now db service works correctly, but web service raises this error:
django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I tried many solutions (adding "sleep 10" or changing volumes,etc.) but nothing worked for me. Ho could help me, please?
As mentioned in the comment you need to use the upper case in the ENV.
'USER': os.environ.get('postgres'),
'PASSWORD': os.environ.get('PGPASSWORD'),
'HOST': os.environ.get('PGHOSTNAME'),
Or the other option is to use hardcoded value for debugging purpose to verify the connection.
'USER': "postgres"
'PASSWORD':"root",
'HOST': "db",
You have something wrong trying to connect to your db like the username, the password or the databasename. Check that they are what postgres ask for you to connect it and that is the db_name that you want to access to.
django.db.utils.OperationalError Could not connect to server