我正在尝试使用docker-compose在构建过程中播种mongodb,但导入丢失因此我在github上创建了一个小示例种子项目以缩小Url:Github Mongodb Seed。 在构建过程之后,dockerimport似乎已经工作了,我可以告诉我因为我执行了一个小的shell脚本(restorecheck.sh)来验证这一点。一旦" docker-compose up"结束了,mongodb里面的数据丢失了。
这是构建过程的摘录:
...
Step 7/7 : RUN mkdir -p /data/db && mongod --fork --logpath=$RESTORE/mongodb.log && sleep 10 && mongoimport --host=127.0.0.1 --db analysedb --collection configdata --type json --file $RESTORE/init.json --jsonArray --verbose && chmod +x $RESTORE/restorecheck.sh && $RESTORE/restorecheck.sh
---> Running in 815fa42de32b
about to fork child process, waiting until server is ready for connections.
forked process: 10
child process started successfully, parent exiting
2017-10-09T04:22:40.754+0000 filesize: 248 bytes
2017-10-09T04:22:40.754+0000 using fields:
2017-10-09T04:22:40.755+0000 connected to: 127.0.0.1
2017-10-09T04:22:40.755+0000 ns: analysedb.configdata
2017-10-09T04:22:40.755+0000 connected to node type: standalone
2017-10-09T04:22:40.755+0000 using write concern: w='1', j=false, fsync=false, wtimeout=0
2017-10-09T04:22:40.755+0000 using write concern: w='1', j=false, fsync=false, wtimeout=0
2017-10-09T04:22:40.831+0000 imported 1 document
"name" : "analysedb",
Database is existing
[ "configdata" ]
collection found
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017/analysedb
MongoDB server version: 3.4.9
{ "_id" : ObjectId("59daf99018666b44d21a378f"), "filename" : "myfile.dat", "id" : "73a5926d-2973-4b55-a3ea-d5933e5ac51c", "name" : "0.10.06", "acronym" : "ACK", "snapshotCreationDate" : "2016-04-21T18:48:21.614+02:00", "analyzed" : "true", "active" : true }
---> faee16091e62
Removing intermediate container 815fa42de32b
Successfully built faee16091e62
Successfully tagged mymongo:1.0.0
所以要么我忘了某种提交,要么是因为"删除中间容器"。但这并不是我理解中间容器的工作方式。我错过了什么?
你有其他想法甚至是解决方案吗?
根据要求,这是我的docker-compose文件:
version: '2'
services:
mongonew:
build:
context: .
dockerfile: dockerfiles/mongodb/Dockerfile
command: bash -c "chown `whoami` /tmp/mongodb-27017.sock && mongod"
volumes:
- ./log:/log/
image: mymongo:1.0.0
ports:
- "27017:27017"
这是dockerfiles / mongodb文件夹中的Dockerfile:
FROM mongo:latest
ENV RESTORE=/home/restorefolder/
RUN mkdir $RESTORE
COPY dockerfiles/mongodb/analysedb/* $RESTORE
RUN pwd
RUN cd $RESTORE
RUN mkdir -p /data/db && mongod --fork --logpath=$RESTORE/mongodb.log && sleep 10 && mongoimport --host=127.0.0.1 --db analysedb --collection configdata --type json --file $RESTORE/init.json --jsonArray --verbose && chmod +x $RESTORE/restorecheck.sh && $RESTORE/restorecheck.sh
#RUN chmod +x $RESTORE/restorecheck.sh && $RESTORE/restorecheck.sh
#CMD ["mongod"]
答案 0 :(得分:0)
将答案用作评论,因为我需要格式化
问题正在发生,因为VOLUMES
中定义的Dockerfile
现在已自动挂载。内容是空白的,所以你没有得到已经存在的内容。
不幸的是没有复制数据。因此,我能想到的唯一解决方案就是像docker run -v /data/db ....
或docker volume create mongodata and then use docker run -v mongodata:/data/db ...
docker-compose
中的等价物是
使用匿名卷
version: '2'
services:
mongonew:
build:
context: .
dockerfile: dockerfiles/mongodb/Dockerfile
command: bash -c "chown `whoami` /tmp/mongodb-27017.sock && mongod"
volumes:
- ./log:/log/
- /data/db
image: mymongo:1.0.0
ports:
- "27017:27017"
或
使用命名卷
version: '2'
services:
mongonew:
build:
context: .
dockerfile: dockerfiles/mongodb/Dockerfile
command: bash -c "chown `whoami` /tmp/mongodb-27017.sock && mongod"
volumes:
- ./log:/log/
- mymongodata:/data/db
image: mymongo:1.0.0
ports:
- "27017:27017"
volumes:
mymongodata: {}