4

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]

私が間違っていることは何ですか?ありがとう!

4

2 に答える 2

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
于 2013-10-24T04:20:50.297 に答える
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 
于 2015-01-07T20:37:59.573 に答える