I'm currently running a REST API on a docker container with the following Dockerfile:
FROM python:2.7
WORKDIR /app
RUN pip install uwsgi
COPY awf/requirements.txt ./awf/requirements.txt
RUN pip install -r awf/requirements.txt
COPY ./ ./
# Call collectstatic (customize the following line with the minimal environment variables needed for manage.py to run):
RUN python manage.py collectstatic --noinput
EXPOSE 8000
ENTRYPOINT [ "uwsgi" ]
CMD [ "--wsgi-file", "awf/wsgi.py", "--ini", "uwsgi.ini" ]
The Python REST API has the following DATABASE config in settings.yaml:
DATABASE: {
engine: 'django.contrib.gis.db.backends.postgis',
name: 'name',
user: 'user',
password: 'pass',
host: '192.168.99.100',
port: '5432',
}
I have set this host:'192.168.99.100'
because this is the docker-machine ip output.
When I run the docker container without mapping port 5432
, I get the following error:
OperationalError: could not connect to server: Connection refused
Is the server running on host "192.168.99.100" and accepting TCP/IP connections on port 5432?
But then I map ports while running the docker container:
docker run -p 8000:8000 -p 5432:5432 img
And I get the following error:
OperationalError: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.
I don't know if I missed some configuration. But I added the IP address range 172.17.0.0/16
to pg_hba.conf
and also configured PostreSQL to listen for connections on all IP, according to this solution:
Allow docker container to connect to a local/host postgres database
EDIT
pg_hba.conf:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.99.0/16 trust
host all all 172.17.0.0/16 trust