簡體   English   中英

Docker-Node.js + MongoDB-“錯誤:無法連接到[localhost:27017]”

[英]Docker - Node.js + MongoDB - “Error: failed to connect to [localhost:27017]”

我正在嘗試為我的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]

知道我在做什么錯嗎? 謝謝!

您實際上是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

重新定義您的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"]

administratord.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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM