使用Docker链接Django和Postgresql [英] Linking Django and Postgresql with Docker

查看:40
本文介绍了使用Docker链接Django和Postgresql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Docker容器.第一个是Postgresql容器,我使用以下命令运行该容器.

I have two Docker containers. The first one is Postgresql container, which I run using the following command.

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

It is based on official image and it is working perfectly, I can connect to Postgresql from the host.

第二个容器是我的Django应用程序的容器.使用以下Dockerfile构建镜像(基于此镜像):

The second container is a container with my Django application. The image is built using the following Dockerfile (based on this image):

FROM python:3-onbuild
EXPOSE 8000 5432
CMD ["/bin/bash"]

然后我使用以下命令运行该容器

And I run this container with the following command

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输出显示容器已链接

docker ps output shows that containers are linked

NAMES
my-app/my-postgres, my-postgres

但是,当我进入localhost:8000时,我看到了Django的错误页面,其中包含以下输出

However, when I go to localhost:8000, I see an error page from Django, with the following output

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

Application's settings.py

  DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'mydb',                      
       'USER': 'postgres',
       'PASSWORD': '',
       'HOST': '127.0.0.1',                      
       'PORT': '5432',                      
    }
  }

如何使链接有效?预先感谢

How to make linking work? Thanks in advance

推荐答案

您的Django映像的 Dockerfile 不应公开端口 5432 ,因为没有Postgresql服务器将在其中运行从该图像创建的任何容器:

The Dockerfile for your Django image should not expose port 5432 as no Postgresql server will be running in any container created from that image:

FROM python:3-onbuild
EXPOSE 8000
CMD ["/bin/bash"]

然后在运行Django容器并将其与

Then as you are running the Django container linking it with

-链接my-postgres:my-postgres

您的数据库设置不正确.

your settings for the database are incorrect.

在Django容器中:127.0.0.1指的是Django容器,该容器未运行任何在端口5432上侦听的服务.

In the Django container: 127.0.0.1 refers to the Django container which isn't running any service listening on port 5432.

因此您的settings.py文件应为:

So your settings.py file should be:

  DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'mydb',                      
       'USER': 'postgres',
       'PASSWORD': '',
       'HOST': 'my-postgres',                      
       'PORT': '5432',                      
    }
  }

当您使用以下命令运行Django容器时:

As you run your Django container with:

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',
       'NAME': 'mydb',                      
       'USER': 'postgres',
       'PASSWORD': '',
       'HOST': 'db',                      
       'PORT': '5432',                      
    }
  }

这篇关于使用Docker链接Django和Postgresql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆