我无法使用集装箱码头连接到mongo

时间:2017-10-22 11:28:13

标签: mongodb docker docker-compose

我是码头工人的新手 所以我跟进了教程here,第6,7和8部分,以便在项目中使用和学习docker。

问题在于,当我使用docker-compose.yml在笔记本电脑上构建图像时, 我的stack_server可以连接到mongo。

但是如果我从docker hub构建图像并且我在笔记本电脑上单独拉出并运行它们,我的stack_server无法连接到mongo。

这是我的docker-compose.yml:

client:
    build: ./client
    restart: always
    ports:
      - "80:80"
    links:
      - server
mongo:
  image: mongo
  command: --smallfiles
  restart: always
  ports:
    - "27017:27017"
server:
    build: ./server
    restart: always
    ports:
     - "8080:8080"
    links:
     - mongo

但是,我的stack_client可以连接到stack_server。

和我的命令来运行我的图像(我的图像是公开的)

docker-run -i -t -p 27017:27017 mongo
docker-run -i -t -p 80:80 mik3fly4steri5k/stack_client 
docker-run -i -t -p 8080:8080 mik3fly4steri5k/stack_server

和我的错误日志

bryan@debian-dev7:~$ sudo docker run -i -t -p 8080:8080 mik3fly4steri5k/stack_server
[sudo] password for bryan: 
Express server listening on 8080, in development mode
connection error: { Error: connect ECONNREFUSED 127.0.0.1:27017
    at Object._errnoException (util.js:1021:11)
    at _exceptionWithHostPort (util.js:1043:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1175:14)
  name: 'MongoError',
  message: 'connect ECONNREFUSED 127.0.0.1:27017' }

这是我的stack_server dockerfile

FROM node:latest

# Set in what directory commands will run
WORKDIR /home/app

# Put all our code inside that directory that lives in the container
ADD . /home/app

# Make sure NPM is not cached, remove everything first
RUN rm -rf /home/app/node_modules/npm \
 && rm -rf  /home/app/node_modules

# Install dependencies
RUN npm install

# Tell Docker we are going to use this port
EXPOSE 8080

# The command to run our app when the container is run
CMD ["node", "app.js"]

第一个解决方案但已弃用

docker-run -i -t -p 27017:27017 --name:mongo mongo
docker-run -i -t -p 80:80 mik3fly4steri5k/stack_client 
docker-run -i -t -p 8080:8080 --link mongo:mongo mik3fly4steri5k/stack_server

我必须在我的mongo上添加一个名称,并在运行我的stack_server时将其与参数链接链接

docker documentation

Warning: The --link flag is a deprecated legacy feature of Docker. 
It may eventually be removed. 
Unless you absolutely need to continue using it,
we recommend that you use user-defined networks to facilitate communication 
between two containers instead of using --link.
One feature that user-defined networks do not support that you can do with 
--link is sharing environmental variables between containers.
However, you can use other mechanisms such as volumes to share environment 
variables between containers in a more controlled way.

4 个答案:

答案 0 :(得分:2)

使用以下命令运行您的stack_server

sudo docker run -i -t -p 8080:8080 --link mongo:mongo mik3fly4steri5k/stack_server

注意: - link flag是不推荐使用的

答案 1 :(得分:1)

您的容器正在运行。但它健康吗?我建议您实施Healthcheck

它实际上看起来几乎相同: (Docker-compose YAML)

healthcheck:
  test: curl -sS http://127.0.0.1:8080 || echo 1
  interval: 5s
  timeout: 10s
  retries: 3

Docker运行状况检查是一个可爱的小功能,它允许将shell命令附加到容器并使用它来检查容器的内容是否足够活跃。

我希望它可以帮到你。

更多信息https://docs.docker.com/compose/compose-file/compose-file-v2/#healthcheck

答案 2 :(得分:0)

要在欲望端口上启动MongoDB,您必须执行以下操作:

client:
    build: ./client
    restart: always
    ports:
      - "80:80"
    links:
      - server
mongo:
  image: mongo
  command: mongod --port 27017
  restart: always
  ports:
    - "27017:27017"
server:
    build: ./server
    restart: always
    ports:
     - "8080:8080"
    links:
     - mongo

答案 3 :(得分:0)

让我们看看错误

  

连接错误:{错误:连接ECONNREFUSED 127.0.0.1:27017

服务器尝试连接到localhost而不是mongo。您需要配置服务器以将mongo连接到mongo:27017

mongo是由docker创建的用于将容器相互链接的别名。