【问题标题】:Docker - Node.js + MongoDB - "Error: failed to connect to [localhost:27017]"Docker - Node.js + MongoDB - “错误:无法连接到 [localhost:27017]”
【发布时间】:2013-11-02 11:42:11
【问题描述】:

我正在尝试为我的 Node 应用创建一个容器。这个应用程序使用 MongoDB 来确保一些数据持久性。 所以我创建了这个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"]

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

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

但是机器显示如下错误:

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

知道我做错了什么吗?谢谢!

【问题讨论】:

    标签: node.js mongodb port virtual-machine docker


    【解决方案1】:

    如下重新定义你的 Dockerfile;

    COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
    
    # ENTRYPOINT should not be used as it wont allow commands from run to be executed
    
    # Define mountable directories.
    VOLUME ["/data/db"]
    
    # Expose ports.
    #   - 27017: process
    #   - 28017: http
    #   - 9191: web app
    EXPOSE 27017 28017 9191
    
    ENTRYPOINT ["/usr/bin/supervisord"]
    

    supervisord.conf 将包含以下内容;

    [supervisord]
    nodaemon=true
    
    [program:mongod]
    command=/usr/bin/mongod --smallfiles
    stdout_logfile=/var/log/supervisor/%(program_name)s.log
    stderr_logfile=/var/log/supervisor/%(program_name)s.log
    autorestart=true
    
    [program:nodejs]
    command=nodejs /opt/app/server/server.js 
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 感谢您的回答!我忽略了容器是单进程的事实,这很糟糕。我选择了 Supervisor,尽管我目前也在尝试为 mongoDB 和我的应用程序构建单独的容器,以使其更加模块化......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多