无法从docker连接到mongodb实例:连接被拒绝

时间:2017-04-15 16:47:54

标签: mongodb docker docker-compose pymongo mongokit

我正在使用docker-compose来创建一个多容器环境,其中我有一个mongodb实例和两个python应用程序。问题是,第一个应用程序能够与mongodb建立连接,而第二个应用程序失败并出现以下错误:

session_start

我的项目结构:

File "/usr/local/lib/python2.7/site-packages/pymongo/mongo_client.py", 
            line 377, in __init__ notification_1   | 
            raise ConnectionFailure(str(e)) notification_1   | 
            pymongo.errors.ConnectionFailure: [Errno -2] Name or service not known

这是我的 [已更新] docker-compose.yml文件:

.
├── docker-compose.yml
├── form
│   ├── app.py
│   ├── Dockerfile
│   ├── requirements.txt
│   ├── static
│   └── templates
│       ├── form_action.html
│       └── form_sumbit.html
├── notify
│   ├── app.py
│   ├── Dockerfile
│   ├── requirements.txt
└── README

第一个应用程序基于Flask并使用mongokit连接到数据库。以下是建立连接的代码:

version: '3'

services:
  db:
    image: mongo:3.0.2
    container_name: mongo
    networks:
      db_net:
        ipv4_address: 172.16.1.1


  web:
    build: form
    command: python -u app.py
    ports:
      - "5000:5000"
    volumes:
      - form:/form
    environment:
      MONGODB_HOST: 172.16.1.1
    networks:
      db_net:
        ipv4_address: 172.16.1.2

  notification:
    build: notify
    command: python -u app.py
    volumes:
      - notify:/notify
    environment:
      MONGODB_HOST: 172.16.1.1
    networks:
      db_net:
        ipv4_address: 172.16.1.3

networks:
  db_net:
    external: true

volumes:
  form:   
  notify:

第二个应用程序是一个简单的python应用程序。连接的代码如下:

MONGODB_HOST = os.environ['DB_PORT_27017_TCP_ADDR']
MONGODB_PORT = 27017

app = Flask(__name__)
app.config.from_object(__name__)

# connect to the database
try:
    connection = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])
except ConnectionFailure:
    print("Connection to db failed. Start MongoDB instance.")
    sys.exit(1)

3 个答案:

答案 0 :(得分:4)

您是否有理由明确指定所有服务的IP地址,以及数据库服务的容器名称?我建议删除它们并使用服务名称连接同一网络上的容器。

version: '3'

services:
  db:
    image: mongo:3.0.2
    networks:
      - db_net

  web:
    build: form
    command: python -u app.py
    ports:
      - "5000:5000"
    volumes:
      - form:/form
    environment:
      MONGODB_HOST: db
    networks:
      - db_net

  notification:
    build: notify
    command: python -u app.py
    volumes:
      - notify:/notify
    environment:
      MONGODB_HOST: db
    networks:
      - db_net

networks:
  db_net:

volumes:
  form:   
  notify:

答案 1 :(得分:0)

您的docker-compose.yml文件格式不正确


notification:
  build: notify
  command: python -u app.py
  volumes:
    - notify:/notify
  links:
    - db

本节中有太多空格。它应该是2个空格,就像文件的其余部分一样

$('.display').click(function(event) {
    event.preventDefault();
    //get category from h4's parent div
    let $parent = $('h4').parent();
    let $cat = $parent.attr('class');
    let cat = String($cat);
    //get id to search for...currently only returning id_1, not unique id
    let $id = $('h4').get();  //need correct method here
    console.log($id);
    //slice off num to use in ajax url req
    if($id.length == 4) {
        id_num = $id.slice(-1) + '/';
    } else if ($id.length == 5) {
        id_num = $id.slice(-2) + '/';
    } else {
        id_num = $id.slice(-3) + '/';
    }
    console.log(id_num);

    $.ajax({
        type: 'GET',
        url: 'https://swapi.co/api/' + $cat + id_num,
        success: function(result) {
            console.log(result);
        }
    })

yaml对格式化非常挑剔

答案 2 :(得分:-1)

我会尝试使用Docker-compose的最新版本3来让您更轻松。

可以将web,db和通知放在同一个docker网络上。 您可以指定每个容器的IP。然后,将这些IP存储在环境变量中。你可以为端口做同样的事情。

enter image description here

connection = Connection(process.env.MONGODB_HOST, app.config['MONGODB_PORT']

以下命令将创建db_net网络:

docker network create db_net --subnet 172.16.0.0/24

这是修订版本3 docker-compose.yml文件。请注意,环境变量使用我们为mongo容器定义的IP地址。

version: '3'

services:
  db:
    image: mongo
    container_name: mongo
    networks:
      db_net:
        ipv4_address: 172.16.1.1 
  web:
    build: form
    command: python -u app.py
    ports:
      - "5000:5000"
    volumes:
      - form:/form
    environment:
      MONGODB_HOST: 172.16.1.1
    networks:
      db_net:
        ipv4_address: 172.16.1.2
  notification:
    build: notify
    command: python -u app.py
    volumes:
      - notify:/notify
    environment:
      MONGODB_HOST: 172.16.1.1
    networks:
      db_net:
        ipv4_address: 172.16.1.3
networks:
  db_net:
    external: true