gpt4 book ai didi

node.js - Docker - Node.js + MongoDB - "Error: failed to connect to [localhost:27017]"

转载 作者:可可西里 更新时间:2023-11-01 09:14:24 25 4
gpt4 key购买 nike

我正在尝试为我的 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

关于node.js - Docker - Node.js + MongoDB - "Error: failed to connect to [localhost:27017]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19554534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com