当前位置: 首页 > 面试题库 >

如何使用默认集合和数据创建Mongo Docker映像?

越扬
2023-03-14
问题内容

我在这里需要支持来构建自己的mongo docker映像。

我有一个脚本列表,用于创建数据并将其插入MongoDB中,这些数据将在我的Dockerfile中调用,以提供具有默认集合和数据的Docker映像。

这是我的Dockerfile当前的样子:

FROM mongo:latest

RUN mkdir -p /data/scripts

COPY . /data/scripts

RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db/

RUN FILES=scripts/*-create.js
RUN for f in $FILES; do mongo mydb $f; done

RUN FILES=scripts/*-insert.js
RUN for f in $FILES; do mongo mydb $f; done

RUN mongod --shutdown

我尝试了不同的选项来启动和停止mongod,并且总是两个失败之一,当前脚本会引发以下错误:

There doesn't seem to be a server running with dbpath: /data/db

更新资料

@Matt回答后,我可以成功运行命令链,但仍然看不到我的数据库(称为my-db),集合和数据。

当前的Dockerfile:

FROM mongo:latest

RUN mkdir -p /data/db/scripts

COPY . /data/db

RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db \
    && CREATE_FILES=/data/db/scripts/*-create.js \
    && for f in $CREATE_FILES; do mongo 127.0.0.1:27017 $f; done \
    && INSERT_FILES=/data/db/scripts/*-insert.js \
    && for f in $INSERT_FILES; do mongo 127.0.0.1:27017 $f; done \
    && mongod --shutdown

docker build命令的输出:

Sending build context to Docker daemon 10.24 kB
Step 1 : FROM mongo:latest
 ---> c08c92f4cb13
Step 2 : RUN mkdir -p /data/db/scripts
 ---> Running in a7088943bb57
 ---> 373c7319927d
Removing intermediate container a7088943bb57
Step 3 : COPY . /data/db
 ---> 8fa84884edb7
Removing intermediate container ae43e2c24fee
Step 4 : RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db     && CREATE_FILES=/data/db/scripts/*-create.js    && for f in $CREATE_FILES; do mongo 127.0.0.1:27017 $f; done    && INSERT_FILES=/data/db/scripts/*-insert.js    && for f in $INSERT_FILES; do mongo 127.0.0.1:27017 $f; done    && mongod --shutdown
 ---> Running in 33970b6865ee
about to fork child process, waiting until server is ready for connections.
forked process: 10
child process started successfully, parent exiting
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:27017/test
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:27017/test
killing process with pid: 10
 ---> 8451e43b7749
Removing intermediate container 33970b6865ee
Successfully built 8451e43b7749

但是正如我所说,我仍然无法使用mongo shell查看数据库中的数据库,集合和数据。我也连接到正在运行的容器,并得到了mongodb.log:

2015-11-06T16:15:14.562+0000 I JOURNAL  [initandlisten] journal dir=/data/db/journal
2015-11-06T16:15:14.562+0000 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2015-11-06T16:15:14.698+0000 I JOURNAL  [initandlisten] preallocateIsFaster=true 2.36
2015-11-06T16:15:14.746+0000 I JOURNAL  [durability] Durability thread started
2015-11-06T16:15:14.746+0000 I JOURNAL  [journal writer] Journal writer thread started
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] MongoDB starting : pid=10 port=27017 dbpath=/data/db 64-bit host=9c05d483673a
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] 
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] 
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] 
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] 
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] db version v3.0.7
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] git version: 6ce7cbe8c6b899552dadd907604559806aa2e9bd
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] build info: Linux ip-10-183-78-195 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2015-11-06T16:15:14.747+0000 I CONTROL  [initandlisten] options: { processManagement: { fork: true }, storage: { dbPath: "/data/db" }, systemLog: { destination: "file", path: "/var/log/mongodb.log" } }
2015-11-06T16:15:14.748+0000 I INDEX    [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes...
2015-11-06T16:15:14.802+0000 I STORAGE  [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
2015-11-06T16:15:14.802+0000 I STORAGE  [FileAllocator] creating directory /data/db/_tmp
2015-11-06T16:15:14.804+0000 I STORAGE  [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB,  took 0 secs
2015-11-06T16:15:14.807+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2015-11-06T16:15:14.830+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:49641 #1 (1 connection now open)
2015-11-06T16:15:14.832+0000 I INDEX    [conn1] allocating new ns file /data/db/my-db.ns, filling with zeroes...
2015-11-06T16:15:14.897+0000 I STORAGE  [FileAllocator] allocating new datafile /data/db/my-db.0, filling with zeroes...
2015-11-06T16:15:14.898+0000 I STORAGE  [FileAllocator] done allocating datafile /data/db/my-db.0, size: 64MB,  took 0 secs
2015-11-06T16:15:14.904+0000 I NETWORK  [conn1] end connection 127.0.0.1:49641 (0 connections now open)
2015-11-06T16:15:14.945+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:49642 #2 (1 connection now open)
2015-11-06T16:15:14.958+0000 I NETWORK  [conn2] end connection 127.0.0.1:49642 (0 connections now open)
2015-11-06T16:15:14.982+0000 I CONTROL  [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2015-11-06T16:15:14.982+0000 I CONTROL  [signalProcessingThread] now exiting
2015-11-06T16:15:14.982+0000 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2015-11-06T16:15:14.982+0000 I NETWORK  [signalProcessingThread] closing listening socket: 6
2015-11-06T16:15:14.982+0000 I NETWORK  [signalProcessingThread] closing listening socket: 7
2015-11-06T16:15:14.982+0000 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2015-11-06T16:15:14.982+0000 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2015-11-06T16:15:14.982+0000 I NETWORK  [signalProcessingThread] shutdown: going to close sockets...
2015-11-06T16:15:14.982+0000 I STORAGE  [signalProcessingThread] shutdown: waiting for fs preallocator...
2015-11-06T16:15:14.982+0000 I STORAGE  [signalProcessingThread] shutdown: final commit...
2015-11-06T16:15:15.008+0000 I JOURNAL  [signalProcessingThread] journalCleanup...
2015-11-06T16:15:15.008+0000 I JOURNAL  [signalProcessingThread] removeJournalFiles
2015-11-06T16:15:15.009+0000 I JOURNAL  [signalProcessingThread] Terminating durability thread ...
2015-11-06T16:15:15.088+0000 I JOURNAL  [journal writer] Journal writer thread stopped
2015-11-06T16:15:15.088+0000 I JOURNAL  [durability] Durability thread stopped
2015-11-06T16:15:15.088+0000 I STORAGE  [signalProcessingThread] shutdown: closing all files...
2015-11-06T16:15:15.090+0000 I STORAGE  [signalProcessingThread] closeAllFiles() finished
2015-11-06T16:15:15.090+0000 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2015-11-06T16:15:15.090+0000 I CONTROL  [signalProcessingThread] dbexit:  rc: 0

我还检查了文件夹/ data / db的内容:

root@fbaf17233182:/data/db# ls -al
total 16
drwxr-xr-x 3 mongodb mongodb 4096 Nov  6 16:15 .
drwxr-xr-x 4 root    root    4096 Nov  6 16:15 ..
drwxr-xr-x 2 root    root    4096 Nov  5 18:55 scripts

可能有帮助:

  • 父Dockerfile
  • 父Dockerfile ENTRYPOINT

问题答案:

问题是信息无法保存在上/db/data,因此我创建了一个解决方案,创建了自己的数据目录。

# Parent Dockerfile https://github.com/docker-library/mongo/blob/982328582c74dd2f0a9c8c77b84006f291f974c3/3.0/Dockerfile
FROM mongo:latest

# Modify child mongo to use /data/db2 as dbpath (because /data/db wont persist the build)
RUN mkdir -p /data/db2 \
    && echo "dbpath = /data/db2" > /etc/mongodb.conf \
    && chown -R mongodb:mongodb /data/db2

COPY . /data/db2

RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db2 --smallfiles \
    && CREATE_FILES=/data/db2/scripts/*-create.js \
    && for f in $CREATE_FILES; do mongo 127.0.0.1:27017 $f; done \
    && INSERT_FILES=/data/db2/scripts/*-insert.js \
    && for f in $INSERT_FILES; do mongo 127.0.0.1:27017 $f; done \
    && mongod --dbpath /data/db2 --shutdown \
    && chown -R mongodb /data/db2

# Make the new dir a VOLUME to persists it 
VOLUME /data/db2

CMD ["mongod", "--config", "/etc/mongodb.conf", "--smallfiles"]

感谢docker -library / mongo Github项目中的@yosifkit
,指出该卷会将数据存储在生成的映像中。我在文档中错过了。



 类似资料:
  • 问题内容: 我正在使用logstash将日志输入ElasticSearch。我将logstash输出配置为: 我注意到,一旦启动logstash,它就会在ES中创建一个映射(日志),如下所示。 如何防止Logstash创建此映射? 更新: 我现在也解决了此错误。 “ [logs]的对象映射试图解析为对象,但是得到了EOF,是否提供了具体的价值?” 正如John Petrone在下面所述,定义映射后

  • 问题内容: 我想创建一个创建数据库的SQL脚本。现在,我有这个: 但是,这会产生以下错误: 我知道问题是我没有为文件名指定完全限定的路径。但是我希望能够运行此脚本,而不管数据库服务器的目录结构如何。有什么方法可以使用默认路径? 问题答案: 您可以在不指定文件详细信息的情况下创建数据库,例如:

  • 问题内容: 当我安装mysql时,它带有两个数据库,即mysql和信息模式。我不小心删除了mysql数据库。有什么办法可以重新创建它? 另外,由于它包含一个包含用户信息的表,如果没有它,我是否可以通过任何方式查看用户信息? 问题答案: 如果您仍然能够登录(由于没有用户表,我不能登录)并且要保存数据库,请使用 可以使用以下命令重新创建MySQL数据库 MySQL文档在这里

  • 我建立了一个具有两个节点和外部Zookeper集合的SOLR集群。该ZK集合有3个节点。我使用参数启动solr实例: 这意味着,我希望SOLR配置在/solr5下,而不是默认情况下的/下。 文件夹 /solr5在ZK中创建: 我还可以毫无问题地将SOLR配置上传到/solr5中。 我的问题是,在创建集合时,如何将生成的文件置于/solr5之下? 我用来创建集合的命令是: 我查看了本页上的文档,但没

  • 一旦你知道如何输入SQL语句的时候,你就可以准备访问一个数据庫。 假设,在你家(你的menagerie)有一些宠物,并且你想对他们的信息保持一个跟踪。这时候,你可以创建表来存储和加载你渴望的信息。然后你就可以通过检索数据庫里的表来回答各种各样的问题,这部分展示如何执行以下操作: 创建数据庫 创建表 加载数据到表 以不同的方式从表中检索数据 使用多个表 menagerie数据庫是简单的,但不难想像到