我有两个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
这是基于官方镜像搭建的,目前运行良好,我可以从主机连接到Postgresql。
第二个容器是我的Django应用程序。该镜像使用以下Dockerfile构建(基于此镜像):
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
docker ps 命令的输出显示容器已经链接。
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',
}
}
如何使链接生效?谢谢提前。
--link my-postgres:[2nd arg]
中冒号后面的第二个参数将成为有关 Django 的DATABASES
的default
设置中HOST
键的值? - pkaramol--link <容器名称>:<别名>
。 - Thomasleveil