码头工作人员与mongodb一起运行django [英] docker-compose to run django with mongodb

查看:168
本文介绍了码头工作人员与mongodb一起运行django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用docker-compose来连接django容器和postgres和mongo容器的问题?我试图使用docker-compose up启动mongo和postgres容器(因为我需要链接两者),但仍然django应用程序不能连接到mongodb默认设置。我的django-compose.yml文件内容复制如下:

  db1:
image:postgres
db2 :
image:mongo
ports:
- 27017:27017
web:
build:。
命令:python manage.py runserver 0.0.0.0:8000
volumes:
- 。:/ code
ports:
- 8000:8000
链接:
- db1
- db2

它与postgres连接默认设置。我也可以telnet到当地的mongodb端口。不过,我在启动Web容器时收到此错误:


文件
/usr/local/lib/python2.7 /site-packages/mongoengine/connection.py,
第124行,在get_connection web_1 |中引发ConnectionError(不能
连接到数据库%s:\\\
%s%(别名,e))web_1 |
mongoengine.connection.ConnectionError:无法连接到数据库
default:web_1 | [Errno 111]连接拒绝


PS:我已经成功地在本地主机上启动了连接到django-postgres的应用程序,但连接失败db,在AWS实例上。这是另一个问题,我仍然需要得到根。

解决方案

我碰到一个类似的问题,但是另一个服务MongoDB的)。我不知道我在做什么错,但这是我如何解决的:

  import os 
import mongoengine

MONGODB_HOST = os.environ.get('DB2_PORT_27017_TCP_ADDR','127.0.0.1')
mongoengine.connect(host = MONGODB_HOST)




  • 使用 DB2 作为您的服务的名称 docker-compose.yml

  • 27017 是公开服务的端口。

  • 更多关于docker-compose环境变量

  • 我把它放在我的设置文件中。但是,根据您的项目架构,您可以自由地将其放在任何地方。



更新



现在,其他服务可以使用类似于别名的主机名来达到docker-comp容器。 链接文档


与别名相同的主机名
可以访问链接服务的容器,如果没有指定别名,则可以访问服务名称。


< blockquote>

这样你可以像这样连接到MongoDB:

  import mongoengine 

mongoengine.connect(host =db2)


I am facing problem using docker-compose to link a django container with postgres and mongo containers? I am trying to use "docker-compose up" which starts up the mongo and postgres containers (as I need to link both) but still the django app is not able to connect to mongodb on default settings. My django-compose.yml file contents are copied below:

    db1:
      image: postgres
    db2:
      image: mongo
      ports:
        - "27017:27017"
    web:
      build: .
      command: python manage.py runserver 0.0.0.0:8000
      volumes:
        - .:/code
      ports:
        - "8000:8000"
      links:
        - db1
        - db2

It does connect with postgres with default settings. I can also telnet to the mongodb port locally. Still, I get this error on starting the web container:

File "/usr/local/lib/python2.7/site-packages/mongoengine/connection.py", line 124, in get_connection web_1 | raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e)) web_1 | mongoengine.connection.ConnectionError: Cannot connect to database default : web_1 | [Errno 111] Connection refused

PS: I had successfully started a django-postgres connected app on my localhost, but it failed connecting to db, on an AWS instance. That is another problem I still need to get to root of.

解决方案

I ran into a similar problem but with another service (not MongoDB). I'm not sure of what I'm doing wrong but this is how I could solve it :

import os
import mongoengine

MONGODB_HOST = os.environ.get('DB2_PORT_27017_TCP_ADDR', '127.0.0.1')
mongoengine.connect(host=MONGODB_HOST)

  • With DB2 being the name of your service in docker-compose.yml
  • 27017 being the port of the exposed service.
  • More about docker-compose environment variables
  • I'd put that in my settings file. But you are free to put it wherever you think it's appropriate depending on your project architecture

UPDATE

Now docker-compose containers are reachable by other services using a hostname similar to their alias. link documentation :

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

And that way you can connect to MongoDB like this:

import mongoengine

mongoengine.connect(host="db2")

这篇关于码头工作人员与mongodb一起运行django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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