简体   繁体   English

在Dockerfile中设置mongoDB

[英]Setting up a mongoDB in a Dockerfile

I try to set up a mongodb in a Dockerfile. 我尝试在Dockerfile中设置mongodb。
This is from my dockerfile which is based on Debian: 这来自我的基于Debian的dockerfile:

...mongoDB is installed here
#start the mongoDB
RUN mongod --fork --logpath /var/log/mongod.log
RUN mongo --eval "db.getSiblingDB('db')"

When I try to build this Dockerfile I get the following error: 当我尝试构建此Dockerfile时,出现以下错误:

connecting to: mongodb://127.0.0.1:27017
W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

It would be great, if anyone knew how to fix this issue. 如果有人知道如何解决此问题,那就太好了。

The problem is related to how Docker processes the Dockerfile. 该问题与Docker如何处理Dockerfile有关。 Each statement in the Dockerfile is executed in a container, which was created from the image of the previous line. Dockerfile中的每个语句都在一个容器中执行,该容器是根据上一行的图像创建的。 In case of the RUN statement, Docker runs the code in a container and creates a new image based on the current state of the container. 如果使用RUN语句,则Docker在容器中运行代码,并根据容器的当前状态创建新映像。 The image does not contain any information about running processes. 该映像不包含有关正在运行的进程的任何信息。

In your case, this means you have an image, which contains the mongodb installation. 就您而言,这意味着您有一个映像,其中包含mongodb安装。 Let's call it A (instead of hash values from the docker build log). 让我们将其称为A (而不是docker build日志中的哈希值)。 The first RUN statement 第一条RUN语句

RUN mongod --fork --logpath /var/log/mongod.log

is processed in a container, let's call it a , which is based on image A . 是在容器中处理的,我们称其为a ,它基于图像A The process mongod in a probably appends a few lines to the log file. 进程mongod中的a可能会在日志文件中追加几行。 Once the RUN statement has been processed, the container's file system is frozen and labeled as image B . 处理完RUN语句后,将冻结容器的文件系统并将其标记为映像B A Docker image does not contain any information about running processes. Docker映像不包含有关正在运行的进程的任何信息。 In fact, at this point, there is not mongod process running anymore. 实际上,此时,不再有mongod进程在运行。 However, image B contains the log entries from this previous RUN . 但是,映像B包含来自此先前RUN的日志条目。

The second RUN statement 第二条RUN语句

RUN mongo --eval "use db"

is executed in container b , which is based on image B . 在基于图像B容器b执行。 This commands fails because there is no running process to which mongo could talk to. 该命令失败,因为没有mongo对话的正在运行的进程。

I think there are two solutions to your problem: 我认为您的问题有两种解决方案:

  1. In general, you can run both scripts in one step 通常,您可以一步运行两个脚本

    RUN mongod --fork --logpath /var/log/mongod.log && mongo --eval "use db"

    or move them to a bash script, such that they are executed in the same container. 或将它们移到bash脚本中,以便它们在同一容器中执行。

  2. Use the official mongo image on Docker hub . 在Docker Hub上使用官方的mongo 映像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM