我有两个Docker容器.第一个是Postgresql容器,我使用以下命令运行.
sudo docker run -v /home/mpmsp/project/ezdict/postgresql/data:/var/lib/postgresql/data -p 127.0.0.1:5432:5432 -name my-postgres -d postgres
它基于official image,它工作正常,可以从主机连接到Postgresql.
第二个容器是我的Django应用程序的容器.图像使用以下Dockerfile(基于this image)构建:
FROM python:3-onbuild
EXPOSE 8000 5432
CMD ["/bin/bash"]
并使用以下命令运行此容器
sudo docker run --link my-postgres:my-postgres -v /home/mpmsp/project/ezdict/ezbkend:/usr/src/app -name my-app -i -t my-app
NAMES
my-app/my-postgres,my-postgres
但是,当我去localhost:8000时,我看到一个Django的错误页面,输出如下
OperationalError at /api-auth/login/
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Request Method: GET
Request URL: http://127.0.0.1:8000/api-auth/login/
Django Version: 1.6.4
Exception Type: OperationalError
Exception Value:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Exception Location: /usr/local/lib/python3.4/site-packages/psycopg2/__init__.py in connect,line 164
Python Executable: /usr/local/bin/python
Python Version: 3.4.1
Python Path:
['/usr/src/app','/usr/local/lib/python34.zip','/usr/local/lib/python3.4','/usr/local/lib/python3.4/plat-linux','/usr/local/lib/python3.4/lib-dynload','/root/.local/lib/python3.4/site-packages','/usr/local/lib/python3.4/site-packages']
Server time: Птн,10 Окт 2014 12:07:07 +0400
应用程序的settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2','NAME': 'mydb','USER': 'postgres','PASSWORD': '','HOST': '127.0.0.1','PORT': '5432',}
}
如何使链接工作?提前致谢
最佳答案
您的Django映像的Docker文件不应公开端口5432,因为没有Postgresql服务器将在从该映像创建的任何容器中运行:
原文链接:https://www.f2er.com/docker/436362.htmlFROM python:3-onbuild
EXPOSE 8000
CMD ["/bin/bash"]
然后,当您运行连接它的Django容器
–link我的postgres:my-postgres
您的数据库设置不正确.
在Django容器中:127.0.0.1指的是没有运行任何监听端口5432的服务的Django容器.
所以你的settings.py文件应该是:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2','HOST': 'my-postgres',}
}
当您运行您的Django容器:
sudo docker run –link my-postgres:db -v / home / mpmsp / project / ezdict / ezbkend:/usr/src / app-name my-app -i -t my-app
那么你的settings.py文件必须是:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2','HOST': 'db',}
}