CoreOS:dockerized mongo实例连接被另一个dockeried mongo

时间:2016-09-12 14:40:13

标签: mongodb docker coreos

我在CoreOS实例上设置了两个服务。简而言之,第一个是官方mongo容器,另一个是定制图像,试图连接到“mongo”实例。

  - name: living-mongo.service
    command: start
    enable: true
    content: |-
      [Unit]
      Description=Mongo
      Author=Living
      After=docker.service

      [Service]
      Restart=always
      RestartSec=10s
      ExecStartPre=-/usr/bin/docker stop mongo
      ExecStartPre=-/usr/bin/docker rm mongo
      ExecStart=/usr/bin/docker run --name mongo -p 27017:27017 --hostname mongo mongo:2.6
      ExecStop=/usr/bin/docker stop mongo
      ExecStopPost=-/usr/bin/docker rm mongo
  - name: living-mongo-seed.service
    command: start
    enable: true
    content: |-
      [Unit]
      Description=Mongo Seed
      Author=Living
      Requires=living-mongo.service
      After=living-mongo.service

      [Service]
      User=core
      Type=oneshot
      ExecStartPre=-/usr/bin/docker stop mongo-seed
      ExecStartPre=-/usr/bin/docker rm mongo-seed
      ExecStart=/usr/bin/docker run --name mongo-seed --link mongo:mongo registry.living-digital-way.com/mongo-qa:v1
      ExecStop=/usr/bin/docker stop mongo-seed

基本上,首先我启动mongo实例,然后我尝试连接它以在那里提供一些数据:

# docker run --name mongo -p 27017:27017 --hostname mongo mongo:2.6
# docker run --name mongo-seed --link mongo:mongo registry.living-digital-way.com/mongo-qa:v1

当第二个服务开始时,它告诉我:

Sep 12 14:12:21 core-01 docker[1672]: Status: Downloaded newer image for registry.living-digital-way.com/mongo-qa:v1
Sep 12 14:12:21 core-01 docker[1672]: 2016-09-12T14:12:21.704+0000 warning: Failed to connect to 172.17.0.4:27017, reason: errno:111 Connection refused
Sep 12 14:12:21 core-01 docker[1672]: couldn't connect to [mongo] couldn't connect to server mongo:27017 (172.17.0.4), connection attempt failed
Sep 12 14:12:21 core-01 docker[1672]: 2016-09-12T14:12:21.728+0000 warning: Failed to connect to 172.17.0.4:27017, reason: errno:111 Connection refused
Sep 12 14:12:21 core-01 docker[1672]: couldn't connect to [mongo] couldn't connect to server mongo:27017 (172.17.0.4), connection attempt failed

系统启动后,我在shell上手动执行第二项服务:

docker run --name mongo-seed --link mongo:mongo registry.living-digital-way.com/mongo-qa:v1

它工作正常。

我在做什么?

修改

自定义泊坞窗图像Dockerfile:

FROM mongo:2.6
MAINTAINER Living Digital Way
COPY ./clients.init.json .
COPY ./users.init.json .
COPY ./import.sh .

RUN ["chmod", "+x", "./import.sh"] # -> only required, if import.sh is not executable
CMD ["./import.sh"]

import.sh

mongoimport --host mongo --db lvdb --collection clients --type json --file ./clients.init.json --jsonArray --upsert --upsertFields client_id
mongoimport --host mongo --db lvdb --collection users --type json --file ./users.init.json --jsonArray --upsert --upsertFields username

1 个答案:

答案 0 :(得分:0)

您需要找出连接被丢弃的位置。

首先检查mongo容器以确保公共IP正确。

docker inspect mongo_container | egrep "IPAddress[^,]+"

这将打印IP地址 - 确保这是正确的。

将Container作为守护进程站起来(如果还没有)

docker run -it -d mongo

获取容器ID

docker ps -a

登录容器

docker exec -it mongo_container_id bash

安装curl

sudo apt-get update
sudo apt-get curl

Ping MongoDB端点

curl localhost:27017

应打印

It looks like you are trying to access MongoDB over HTTP on the native driver port.

控制+ d退出容器。

在容器仍处于运行状态时运行相同的命令但不使用localhost而是使用此命令中的IP。

docker inspect mongo_container | egrep "IPAddress[^,]+"

curl 172.17.4.124:27017

应该看到相同的输出

 It looks like you are trying to access MongoDB over HTTP on the native driver port.

IP,端口,端口暴露,服务器或容器未启动。