2013-10-24 58 views
4

我正在嘗試爲我的節點應用程序創建一個容器。這個程序使用MongoDB確保一些數據持久性。 所以我創造了這個DockerfileDocker - Node.js + MongoDB - 「錯誤:無法連接到[localhost:27017]」

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] 

任何想法我做錯了什麼?謝謝!

回答

1

你其實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 
+0

感謝您的回答!我不喜歡容器是單一過程的事實。我選擇了Supervisor,儘管我目前也正在爲mongoDB和我的應用程序構建單獨的容器,以使它更加模塊化...... – Aldream

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 
相關問題