本文介绍了Docker - Node.js + MongoDB - “错误:无法连接到[localhost:27017]”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Node 应用创建一个容器。此应用程序使用 MongoDB 来确保某些数据持久性。
所以我创建了这个

Dockerfile :

  FROM ubuntu:latest 

#---安装MongoDB
#将10gen官方apt源添加到源列表
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen'| tee /etc/apt/sources.list.d/10gen.list
#在Ubuntu中没有可用的initctl
RUN dpkg-divert --local --rename --add / sbin / initctl
RUN ln -s / bin / true / sbin / initctl
#安装MongoDB
运行apt-get update
运行apt-get install mongodb-10gen
#创建MongoDB数据目录
运行mkdir -p / data / db
CMD [usr / bin / mongod,--smallfiles]

#---安装节点。 js

运行apt-get更新
运行apt-get install -y python-software-properties python python-setuptools ruby​​ ruby​​gems
RUN add-apt-repository ppa:chris- lea / node.js

#修复破坏的依赖关系(nodejs:Depends:rlwrap但是不可安装):
RUN echodeb http://archive.ubuntu.com/ ubuntu精确宇宙>> /etc/apt/sources.list

RUN echodeb http://us.archive.ubuntu.com/ubuntu/ precise universe>> /etc/apt/sources.list
运行apt-get update
运行apt-get install -y nodejs

#删除不必要的包
RUN apt-get purge -y python-software-properties python python-setuptools ruby​​ ruby​​gems
运行apt-get autoremove -y

#清除包存储库缓存
RUN apt-get clean all

#---包应用源
添加。 / src
#安装应用程序依赖项
RUN cd / src; npm安装

EXPOSE 8080
CMD [node,/src/start.js]

然后我通过以下方式构建并启动整个事情:

  $ sudo docker build -t aldream / myApp 
$ sudo docker运行aldream / myApp

但是机器显示以下错误:

  [错误]错误:无法连接到[localhost:27017] 
任何想法我做错了什么?/ / pre>

谢谢!

解决方案

你实际上是否有 docker运行aldream / myApp ?在这种情况下,使用您提供的Docker文件,它应该运行MongodB,而不是您的应用程序。还有另一个 CMD 命令或另一个Dockerfile,或者您是否正在运行 docker run aldream / myApp< somethingelse> ?在后一种情况下,它将覆盖 CMD 指令,MongoDB将不会被启动。



如果你想在单个容器中运行多个进程,您需要一个进程管理器(例如Supervisor,god,monit)或从脚本启动后台进程;例如:

 #!/ bin / sh 
mongod&
node myapp.js&
等待


I am trying to create a container for my Node app. This app uses MongoDB to ensure some data persistence.So I created this Dockerfile:

FROM    ubuntu:latest

# --- Installing MongoDB
# Add 10gen official apt source to the sources list
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list
# Hack for initctl not being available in Ubuntu
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -s /bin/true /sbin/initctl
# Install MongoDB
RUN apt-get update
RUN apt-get install mongodb-10gen
# Create the MongoDB data directory
RUN mkdir -p /data/db
CMD ["usr/bin/mongod", "--smallfiles"]

# --- Installing Node.js

RUN apt-get update
RUN apt-get install -y python-software-properties python python-setuptools ruby rubygems
RUN add-apt-repository ppa:chris-lea/node.js

# Fixing broken dependencies ("nodejs : Depends: rlwrap but it is not installable"):
RUN echo "deb http://archive.ubuntu.com/ubuntu precise universe" >> /etc/apt/sources.list

RUN echo "deb http://us.archive.ubuntu.com/ubuntu/ precise universe" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y nodejs

# Removed unnecessary packages
RUN apt-get purge -y python-software-properties python python-setuptools ruby rubygems
RUN apt-get autoremove -y

# Clear package repository cache
RUN apt-get clean all

# --- Bundle app source
ADD . /src
# Install app dependencies
RUN cd /src; npm install

EXPOSE  8080
CMD ["node", "/src/start.js"]

Then I build and launch the whole thing through:

$ sudo docker build -t aldream/myApp
$ sudo docker run aldream/myApp

But the machine displays the following error:

[error] Error: failed to connect to [localhost:27017]

Any idea what I am doing wrong? Thanks!

解决方案

Do you actually docker run aldream/myApp? In that case, with the Dockerfile that you provided, it should run MongODB, but not your app. Is there another CMD command, or another Dockerfile, or are you running docker run aldream/myApp <somethingelse>? In the latter case, it will override the CMD directive and MongoDB will not be started.

If you want to run multiple processes in a single container, you need a process manager (like e.g. Supervisor, god, monit) or start the processes in the background from a script; e.g.:

#!/bin/sh
mongod &
node myapp.js &
wait

这篇关于Docker - Node.js + MongoDB - “错误:无法连接到[localhost:27017]”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 12:45