我想用数据库构建一个基于mongobased的图像,而我只能用数据构建图像,然后在容器中创建数据库,我想知道是否有可能构建图像,当任何人运行它时可以得到一个容器与数据库。
我的docker-build /:
./dockerfile
./doc_info_product.json
./word_sim.json
./importjson.sh
importjson.sh
#!/bin/sh^M
mongoimport --db mydb --collection doc_info_product --type json --file /data/db/doc_info_product.json --jsonArray
mongoimport --db mydb --collection word_sim --type json --file /data/db/word_sim.json --jsonArray
dockerfile:
FROM mongo
RUN mkdir -p /data/db
COPY doc_info_product.json /data/db/doc_info_product.json
COPY word_sim.json /data/db/word_sim.json
RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db --smallfiles
EXPOSE 27017
VOLUME [ "/data/db" ]
CMD [ "mongod", "--smallfiles"]
构建图片:
docker build --rm -f dockerfile -t mymongo:1.0 .
Sending build context to Docker daemon 13.27MB
Step 1/8 : FROM mongo
---> 0f57644645eb
Step 2/8 : RUN mkdir -p /data/db
---> Using cache
---> 5d5ef408cf76
Step 3/8 : COPY doc_info_product.json /data/db/doc_info_product.json
---> Using cache
---> 9c79927bf287
Step 4/8 : COPY word_sim.json /data/db/word_sim.json
---> Using cache
---> 6daeb8789102
Step 5/8 : RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db --smallfiles
---> Using cache
---> 73ab4b186ef5
Step 6/8 : EXPOSE 27017
---> Using cache
---> 66787b323bbf
Step 7/8 : VOLUME [ "/data/db" ]
---> Using cache
---> b8daefe9a984
Step 8/8 : CMD [ "mongod", "--smallfiles"]
---> Using cache
---> 64de133825db
Successfully built 64de133825db
Successfully tagged mymongo:1.0
运行图片:
docker run mymongo:1.0
[initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=d935783199ad
[initandlisten] db version v3.6.2
[initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
[initandlisten] OpenSSL version: OpenSSL 1.0.1t 3 May 2016
[initandlisten] allocator: tcmalloc
[initandlisten] modules: none
[initandlisten] build environment:
[initandlisten] distmod: debian81
[initandlisten] distarch: x86_64
[initandlisten] target_arch: x86_64
[initandlisten] options: { net: { bindIpAll: true }, storage: { mmapv1: { smallFiles: true } } }
[initandlisten]
[initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
[initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
[initandlisten] wiredtiger_open config: create,cache_size=478M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
[initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
[initandlisten]
[initandlisten] ** WARNING: Access control is not enabled for the database.
[initandlisten] ** Read and write access to data and configuration is unrestricted.
[initandlisten]
[initandlisten] createCollection: admin.system.version with provided UUID: 7b49fe9e-5847-4fe1-9b34-2fca27ab4bab
[initandlisten] setting featureCompatibilityVersion to 3.6
[initandlisten] createCollection: local.startup_log with generated UUID: 8f254aea-76ce-4694-a55a-8e96b269a9be
[initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
[initandlisten] waiting for connections on port 27017
在contanier中创建一个数据库:
docker ps
d935783199ad mymongo:1.0 ...
docker exec d935783199ad -it bash
root@d935783199ad:/# mongoimport --db mydb --collection doc_info_product --type json --file /data/db/doc_info_product.json --jsonArray
2018-02-02T03:42:29.581+0000 connected to: localhost
2018-02-02T03:42:30.166+0000 imported 1000 documents
root@d935783199ad:/# mongoimport --db mydb --collection word_sim --type json --file /data/db/word_sim.json --jsonArray
2018-02-02T03:42:45.716+0000 connected to: localhost
2018-02-02T03:42:45.787+0000 imported 1000 documents
检查mydb:
root@d935783199ad:/# mongo
MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-02-02T03:38:57.027+0000 I STORAGE [initandlisten]
2018-02-02T03:38:57.027+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-02-02T03:38:57.027+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-02-02T03:38:57.144+0000 I CONTROL [initandlisten]
2018-02-02T03:38:57.144+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-02-02T03:38:57.144+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-02-02T03:38:57.144+0000 I CONTROL [initandlisten]
2018-02-02T03:42:50.549+0000 E - [main] Error loading history file: FileOpenFailed: Unable to fopen() file /root/.dbshell: No such file or directory
> show dbs
admin 0.000GB
local 0.000GB
mydb 0.005GB
> use mydb
switched to db mydb
> show collections
doc_info_product
word_sim
我想通过构建图片然后运行它来获取mydb.doc_info_product
和mydb.word_sim
,所以我尝试了:
将dockerfile
更改为
FROM mongo
RUN mkdir -p /data/db
COPY doc_info_product.json /data/db/doc_info_product.json
COPY word_sim.json /data/db/word_sim.json
RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db --smallfiles
COPY importjson.sh /data/db/importjson.sh
RUN chmod +x /data/db/importjson.sh
ENTRYPOINT ["/data/db/importjson.sh"]
EXPOSE 27017
VOLUME [ "/data/db" ]
CMD [ "mongod", "--smallfiles"]
然后建立:
docker build --rm -f dockerfile -t mymongo:2.0 .
Sending build context to Docker daemon 13.27MB
Step 1/11 : FROM mongo
---> 0f57644645eb
Step 2/11 : RUN mkdir -p /data/db
---> Using cache
---> 5d5ef408cf76
Step 3/11 : COPY doc_info_product.json /data/db/doc_info_product.json
---> Using cache
---> 9c79927bf287
Step 4/11 : COPY word_sim.json /data/db/word_sim.json
---> Using cache
---> 6daeb8789102
Step 5/11 : RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db --smallfiles
---> Using cache
---> 73ab4b186ef5
Step 6/11 : COPY importjson.sh /data/db/importjson.sh
---> 072a2de84dd9
Step 7/11 : RUN chmod +x /data/db/importjson.sh
---> Running in 4779f52a9a24
Removing intermediate container 4779f52a9a24
---> f262f194508a
Step 8/11 : ENTRYPOINT ["/data/db/importjson.sh"]
---> Running in 7624286181ac
Removing intermediate container 7624286181ac
---> a909f0886298
Step 9/11 : EXPOSE 27017
---> Running in ad28534d97fd
Removing intermediate container ad28534d97fd
---> 2f49377f40fd
Step 10/11 : VOLUME [ "/data/db" ]
---> Running in 429d04e2b24e
Removing intermediate container 429d04e2b24e
---> ed081dc3bf4e
Step 11/11 : CMD [ "mongod", "--smallfiles"]
---> Running in 1c6f53af7bbd
Removing intermediate container 1c6f53af7bbd
---> 6aa20c93a7f4
Successfully built 6aa20c93a7f4
Successfully tagged mymongo:2.0
然后运行:
docker run mymongo:2.0
standard_init_linux.go:195: exec user process caused "no such file or directory"
我的码头信息:
Containers: 79
Running: 5
Paused: 0
Stopped: 74
Images: 95
Server Version: 17.12.0-ce
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: active
NodeID: pb9z7cctp5bx1xzuj125shh69
Is Manager: true
ClusterID: qaip1vuuq1bno94xn8tz6ipql
Managers: 1
Nodes: 1
Orchestration:
Task History Retention Limit: 5
Raft:
Snapshot Interval: 10000
Number of Old Snapshots to Retain: 0
Heartbeat Tick: 1
Election Tick: 3
Dispatcher:
Heartbeat Period: 5 seconds CA Configuration:
Expiry Duration: 3 months
Force Rotate: 0
Autolock Managers: false
Root Rotation In Progress: false
Node Address: 192.168.65.3
Manager Addresses:
192.168.65.3:2377
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 89623f28b87a6004d4b785663257362d1658a729
runc version: b2567b37d7b75eb4cf325b77297b140ea686ce8f
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.9.60-linuxkit-aufs
Operating System: Docker for Windows
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.934GiB
Name: linuxkit-00155d327211
ID: S3WK:QDKK:XGGV:VQJY:Z3XC:5YQD:QQSF:3FSM:WJ3Z:34GU:35FZ:DKMH
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
File Descriptors: 81
Goroutines: 208
System Time: 2018-02-02T04:20:08.6685331Z
EventsListeners: 1
Registry: https://index.docker.io/v1/
Labels:
Experimental: true
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
我不知道是否可以使用数据库构建图像,其他任何人都可以使用它来拉动并运行图像。
非常感谢。
答案 0 :(得分:0)
您的答案在Dockerfile或Bash脚本中看起来像是一个问题,其中包含一些无效路径。也发布你的脚本,以帮助查看实际问题。无论如何,这是达到你想要的一般方法:
只需将一个bash脚本添加到Dockerfile即可完成工作。然后,您可以使用bash脚本创建数据库并向其添加数据。
以下是创建用户和数据库的示例代码:
mongo admin << EOF
use admin;
db.createUser({
user: 'useradmin',
pwd: '${PASS}',
roles:[{
role:'userAdminAnyDatabase',
db:'admin'
}]
});
exit
EOF
mongo admin -u useradmin -p ${PASS} << EOF
use DbName;
db.createUser({
user: ${USER},
pwd: ${PASS},
roles:[{
role:'readWrite',
db:${DATABASE}
}]
});
exit
EOF
以下是添加数据的示例代码:
mongo -u yourUser -p yourPass DbName << EOF
...Your_MongoDB_Commands_Here
EOF
如果这还不够清楚,我做了类似于我的一个旧项目的事情(这些脚本基于我不记得的另一个来源,但无论如何应该有所帮助)。以下是相关文件的链接:Github。