我目前正在docker容器上运行REST API,其中包含以下 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" ]
Python REST API在 settings.yaml 中具有以下DATABASE配置:
DATABASE: {
engine: 'django.contrib.gis.db.backends.postgis',
name: 'name',
user: 'user',
password: 'pass',
host: '192.168.99.100',
port: '5432',
}
我设置了 host:'192.168.99.100'
因为这是 docker-machine ip 输出 .
当我运行没有映射端口 5432
的docker容器时,我收到以下错误:
OperationalError:无法连接到服务器:连接被拒绝服务器是否在主机“192.168.99.100”上运行并接受端口5432上的TCP / IP连接?
但是我在运行docker容器时映射端口:
docker run -p 8000:8000 -p 5432:5432 img
我收到以下错误:
OperationalError:服务器意外关闭连接这可能意味着服务器在处理请求之前或处理时异常终止 .
我不知道我是否错过了一些配置 . 但是根据这个解决方案,我将IP地址范围 172.17.0.0/16
添加到了 pg_hba.conf
并且还将PostreSQL配置为侦听所有IP上的连接: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