问题描述
我有一个脚本列表,用于创建和插入数据到MongoDB中,这些脚本将在我的Docker文件用于提供具有默认集合和数据的docker映像。
以下是我的Docker文件如下所示:
FROM mongo :最新
运行mkdir -p / data / scripts
COPY。 / data / scripts
运行mongod --fork --logpath /var/log/mongodb.log --dbpath / data / db /
RUN FILES = scripts / * -create.js
在$ FILES中为f运行;做mongo mydb $ f;完成
RUN FILES = scripts / * - insert.js
运行$文件中的f;做mongo mydb $ f;完成
运行mongod --shutdown
我尝试过不同的选项启动和停止mongod并且总是其中一个失败,当前脚本会引发以下错误:
似乎没有使用dbpath运行的服务器:/ data / db
更新 p>
@Matt答案之后,我可以成功运行命令链,但仍然看不到我的数据库(称为my-db),集合和数据。
目前的Docker文件:
FROM mongo:latest
运行mkdir -p / data / db / scripts
COPY。 / data / db
运行mongod --fork --logpath /var/log/mongodb.log --dbpath / data / db \
&&& CREATE_FILES = / data / db / scripts / * - create.js \
&& for f in $ CREATE_FILES;做mongo 127.0.0.1:27017 $ f;完成\
&& INSERT_FILES = / data / db / scripts / * - insert.js \
&&& for $ in $ INSERT_FILES;做mongo 127.0.0.1:27017 $ f;完成\
&& mongod --shutdown
docker构建命令的输出:
将构建环境发送到Docker守护程序10.24 kB
步骤1:FROM mongo:latest
---> c08c92f4cb13
步骤2:运行mkdir -p / data / db / scripts
--->运行在a7088943bb57
---> 373c7319927d
删除中间容器a7088943bb57
步骤3:复制。 / data / db
---> 8fa84884edb7
删除中间容器ae43e2c24fee
步骤4:运行mongod --fork --logpath /var/log/mongodb.log --dbpath / data / db&&& CREATE_FILES = / data / db / scripts / * - create.js&& for f in $ CREATE_FILES;做mongo 127.0.0.1:27017 $ f;完成&& INSERT_FILES = / data / db / scripts / * - insert.js&& for $ in $ INSERT_FILES;做mongo 127.0.0.1:27017 $ f;完成&& mongod --shutdown
--->运行在33970b6865ee
即将fork子进程,等到服务器准备好连接。
分叉进程:10
子进程成功启动,父退出
MongoDB shell版本:3.0.7
连接到:127.0.0.1:27017/test
MongoDB shell版本:3.0.7
连接到:127.0.0.1:27017/test
杀死进程与pid:10
---> 8451e43b7749
删除中间容器33970b6865ee
成功构建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:没有日志文件存在,不需要恢复
2015-11-06T16:15:14.698 + 0000 I JOURNAL [initandlisten] preallocateIsFaster = true 2.36
2015-11-06T16:15:14.746 + 0000 I JOURNAL [耐用性]耐用性线程
2015- 11-06T16:15:14.746 + 0000 I JOURNAL [journal writer]日记笔者线程开始
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten] MongoDB开始:pid = 10 port = 27017 dbpath = / data / db 64位主机= 9c05d483673a
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten] **警告:您以root用户身份运行此进程,这是不推荐的。
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] **警告:/ sys / kernel / mm / transparent_hugepage / enabled是always。
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten] **我们建议将其设置为'never'
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten] **警告:/ sys / kernel / mm / transparent_hugepage / defrag是always。
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten] **我们建议将其设置为'never'
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten] db版本v3.0.7
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten] git版本:6ce7cbe8c6b899552dadd907604559806aa2e9bd
2015-11-06T16:15:14.747 + 0000 I CONTROL [initandlisten]构建信息: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]选项:{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]分配新的ns文件/data/db/local.ns,填充零...
2015-11-06T16:15:14.802 + 0000 I STORAGE [FileAllocator ]分配新的数据文件/data/db/local.0,填充零...
2015-11-06T16:15:14.802 + 0000 I STORAGE [FileAllocator]创建目录/ data / db / _tmp
2015-11-06T16:15:14.804 + 0000 I STORAGE [FileAllocator]完成分配数据文件/data/db/local.0,大小:64MB,花费0秒
2015-11-06T16:15:14.807+ 0000 I NETWORK [initandlisten]等待端口27017上的连接
2015-11-06T16:15:14.830 + 0000从127.0.0.1:49641#1接受的NETWORK [initandlisten]连接(1连接现在打开)
2015-11-06T16:15:14.832 + 0000 I INDEX [conn1]分配新的ns文件/data/db/my-db.ns,填充零...
2015-11-06T16:15 :14.897 + 0000 I STORAGE [FileAllocator]分配新的数据文件/data/db/my-db.0,填充零...
2015-11-06T16:15:14.898 + 0000 I存储[FileAllocator]完成分配数据文件/data/db/my-db.0,大小:64MB,花费0秒
2015-11-06T16:15:14.904 + 0000我NETWORK [conn1]结束连接127.0.0.1:49641(0个连接现在开放)
2015-11-06T16:15:14.945 + 0000从127.0.0.1:49642#2(1连接现在打开)接受的NETWORK [initandlisten]连接
2015-11-06T16:15:14.958+ 0000 I NETWORK [conn2]结束连接127.0.0.1:49642(0连接现在打开)
2015-11-06T16:15:14.982 + 0000 I CONTROL [signalProcessingThread]得到信号15(终止),将在当前结束cmd结束
2015-11-06T16:15:14.982 + 0000 I CONTROL [signalProcessingThread]现在退出
2015-11-06T16:15:14.982 + 0000 I NETWORK [signalProcessingThread] shutdown: socket ...
2015-11-06T16:15:14.982 + 0000 I NETWORK [signalProcessingThread]关闭监听套接字:6
2015-11-06T16:15:14.982 + 0000 I NETWORK [signalProcessingThread] close监听套接字:7
2015-11-06T16:15:14.982 + 0000 I NETWORK [signalProcessingThread]删除套接字文件:/tmp/mongodb-27017.sock
2015-11-06T16:15:14.982+ 0000 I NETWORK [signalProcessingThread] shutdown:进入flush diaglog ...
2015- 11-06T16:15:14.982 + 0000 I NETWORK [signalProcessingThread] shutdown:关闭sockets ...
2015-11-06T16:15:14.982 + 0000 I STORAGE [signalProcessingThread] shutdown:等待fs预分配器。 ..
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]终止持久性线程...
2015-11-06T16:15:15.088 + 0000 I JOURNAL [期刊作者]日记笔者线程停止
2015-11-06T16:15:15.088 + 0000 I JOURNAL [耐用性]耐用性线程停止
2015-11-06T16:15:15.088 + 0000 I STORAGE [signalProcessingThread] shutdown:关闭所有文件...
2015-11-06T16:15:15.090 + 0000 I存储[signalProcessingThread] closeAllFiles()完成
2015-11-06T16:15:15.090 + 0000 I存储[signalProcessingThread] shutdown:remove 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脚本
可以帮助:
- 父Dockerfile
问题是信息无法保存在 / db / data
,所以我创建了一个创建自己的数据目录的解决方案。
#Parent Dockerfile https://github.com/docker-library/mongo/blob/982328582c74dd2f0a9c8c77b84006f291f974c3/3.0/Dockerfile
FROM mongo:最新
#修改子项mongo以使用/ data / db2作为dbpath (因为/ data / db wont persis
运行mkdir -p / data / db2 \
&&& echodbpath = / data / db2> /etc/mongodb.conf \
&& chown -R mongodb:mongodb / data / db2
COPY。 / data / db2
运行mongod --fork --logpath /var/log/mongodb.log --dbpath / data / db2 --smallfiles \
&& CREATE_FILES = / data / db2 / scripts / * - create.js \
&& for f in $ CREATE_FILES;做mongo 127.0.0.1:27017 $ f;完成\
&& INSERT_FILES = / data / db2 / scripts / * - insert.js \
&&& for $ in $ INSERT_FILES;做mongo 127.0.0.1:27017 $ f;完成\
&& mongod --dbpath / data / db2 --shutdown \
&&& chown -R mongodb / data / db2
#使新的目录成为一个VOLUME来保持它
VOLUME / data / db2
CMD [mongod, --config,/etc/mongodb.conf,--smallfiles]
谢谢来自 Github项目中的@yosifkit,指出卷将将数据存储在生成的图像中。我错过了文件。
I need support here to build my own mongo docker image.
I have a list of scripts to create and insert data into the MongoDB that shall be called in my Dockerfile to deliver a docker image with default collections and data.
Here is how my Dockerfile looks like currently:
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
I've tried different options to start and stop mongod and always one of the two fail, the current script raise the following error:
There doesn't seem to be a server running with dbpath: /data/db
Update
After @Matt answer I could run successfully the command chain, but can't still see my database (called my-db), collections and data there.
The current 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
The output from the docker build command:
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
But as I said, I still can't see the database, collections and data in my database using mongo shell.Also I connected to the running container and got the 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
I also checked the folder /data/db content:
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
May help:
- Parent Dockerfile
- Parent Dockerfile
ENTRYPOINT
The problem was that information could not be saved on /db/data
, so I've created a solution creating my own data directory.
# 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"]
Thanks to @yosifkit from the docker-library/mongo Github project for pointing that the volume would store the data in the resulting image. I missed that on the documentation.
这篇关于如何使用默认集合和数据创建Mongo Docker映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!