【docker】mongodb镜像制作

1.文件准备 

mkdir -p /data/mongodb 
cd /data/mongodb 
--下载一个二进制目录。
mongodb-linux-x86_64-rhel70-5.0.9.tgz    
dockerfile-mongodb.yml  
mongodb.conf 

2.创建镜像 

chmod u+x docker-build-mongodb.sh
./docker-build-mongodb.sh
........................
Removing intermediate container 81853da7408d
 ---> 8b0373bbbea0
Successfully built 8b0373bbbea0
Successfully tagged mongodb:5.0.9

--创建卷。
docker volume create mongodata

ls /data/docker/data/volumes/mongodata/_data/
 

 3.创建容器 

--后台窗口一个容器。
docker run -it -d --name mongodb27017 -h mongodb27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-p 27017:27017 mongodb:5.0.9

--上面这些参数是docker里面针对mongodb定义的参数。

4.登陆并使用容器

docker exec -it mongodb27017 bash



--使用本地IP创建后才可以远程登陆。
mongo --host 127.0.0.1:27017
[root@mongodb27017 local]]# mongo --host 127.0.0.1:27017
MongoDB shell version v5.0.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9a8035df-e8d9-4ab5-950c-892201f541ea") }
MongoDB server version: 5.0.9
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
>

--创建管理员。
use admin
db.createUser(
{
user: "admin",
pwd: "admin123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

--创建集群管理员 
use admin
db.auth("admin","admin123")

> use admin
switched to db admin
> db.auth("admin","admin123")
1
> db.createUser(
... {
... user: "root",
... pwd: "root123",
... roles: [ { role: "clusterAdmin", db: "admin" } ]
... }
... )
Successfully added user: {
	"user" : "root",
	"roles" : [
		{
			"role" : "clusterAdmin",
			"db" : "admin"
		}
	]
}


--回显
> use sspudb
switched to db sspudb
> db.createUser(
... {
... user: "sspu",
... pwd: "sspu123",
... roles: [{ role: "readWrite", db: "sspudb" }]
... }
... )
Successfully added user: {
	"user" : "sspu",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "sspudb"
		}
	]
}

--宿主机登陆使用
宿主机:PATH 路径添加:/data/mongodb/mongodb-linux-x86_64-rhel70-5.0.9/bin
mongo --host 192.168.1.7:27017
[root@es1 bin]# mongo --host 192.168.1.7:27017
MongoDB shell version v5.0.9
connecting to: mongodb://192.168.1.7:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a2169aa3-27c0-464c-a100-5df259ea013d") }
MongoDB server version: 5.0.9
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
> 

> db.serverStatus().connections
{
	"current" : 1,
	"available" : 499,
	"totalCreated" : 2,
	"active" : 1,
	"threaded" : 1,
	"exhaustIsMaster" : 0,
	"exhaustHello" : 0,
	"awaitingTopologyChanges" : 0
}

--向SSPUDB数据库插入数据
use sspudb
db.auth("sspu","sspu123")
db.ssputab1.insert({"sspu01":"student1"})
for(var i =0; i <11; i ++){db.ssputab1.insert({userName:'sspu'+i,age:i})}
show collections
--回显。
> use sspudb
switched to db sspudb
> db.auth("sspu","sspu123")
1
> db.ssputab1.insert({"sspu01":"student1"})
WriteResult({ "nInserted" : 1 })
> for(var i =0; i <11; i ++){db.ssputab1.insert({userName:'sspu'+i,age:i})}
WriteResult({ "nInserted" : 1 })
> show collections;
ssputab1

db.ssputab1.find()
> db.ssputab1.find()
{ "_id" : ObjectId("6688a7c162946c3d05351f88"), "sspu01" : "student1" }
{ "_id" : ObjectId("6688a7cc62946c3d05351f89"), "userName" : "sspu0", "age" : 0 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8a"), "userName" : "sspu1", "age" : 1 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8b"), "userName" : "sspu2", "age" : 2 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8c"), "userName" : "sspu3", "age" : 3 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8d"), "userName" : "sspu4", "age" : 4 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8e"), "userName" : "sspu5", "age" : 5 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8f"), "userName" : "sspu6", "age" : 6 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f90"), "userName" : "sspu7", "age" : 7 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f91"), "userName" : "sspu8", "age" : 8 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f92"), "userName" : "sspu9", "age" : 9 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f93"), "userName" : "sspu10", "age" : 10 }

db.ssputab1.insert({"userName":"sspu11"})
> db.ssputab1.insert({"userName":"sspu11"})
WriteResult({ "nInserted" : 1 })
> db.ssputab1.find()
{ "_id" : ObjectId("6688a7c162946c3d05351f88"), "sspu01" : "student1" }
{ "_id" : ObjectId("6688a7cc62946c3d05351f89"), "userName" : "sspu0", "age" : 0 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8a"), "userName" : "sspu1", "age" : 1 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8b"), "userName" : "sspu2", "age" : 2 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8c"), "userName" : "sspu3", "age" : 3 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8d"), "userName" : "sspu4", "age" : 4 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8e"), "userName" : "sspu5", "age" : 5 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8f"), "userName" : "sspu6", "age" : 6 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f90"), "userName" : "sspu7", "age" : 7 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f91"), "userName" : "sspu8", "age" : 8 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f92"), "userName" : "sspu9", "age" : 9 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f93"), "userName" : "sspu10", "age" : 10 }
{ "_id" : ObjectId("6688a81e62946c3d05351f94"), "userName" : "sspu11" }

5.将容器上传到harbor镜像

docker rm -f mongodb27017
docker tag mongodb:5.0.9 192.168.1.11:443/myharbor/mongodb:5.0.9
docker push 192.168.1.11:443/myharbor/mongodb:5.0.9 
[root@es1 bin]# docker rm -f mongodb27017
mongodb27017
[root@es1 bin]# docker tag mongodb:5.0.9 192.168.1.11:443/myharbor/mongodb:5.0.9
[root@es1 bin]# docker push 192.168.1.11:443/myharbor/mongodb:5.0.9 
The push refers to repository [192.168.1.11:443/myharbor/mongodb]
38051ae93ab2: Pushed 
4722af617cd4: Pushed 
f1e5014e52bd: Pushed 
34fc55c434bf: Pushed 
604ddaddc589: Pushed 
53c61a520c85: Mounted from myharbor/mysql 
88765da43086: Mounted from myharbor/mysql 
2ca40431ab7d: Mounted from myharbor/mysql 
caceb42f09c2: Mounted from myharbor/mysql 
5b1d42135d46: Mounted from myharbor/mysql 
174f56854903: Mounted from myharbor/mysql 
5.0.9: digest: sha256:c65dff2739c645447590cc204bb6cc9a667f0a44e6ff55a5c6146baf6aced54d size: 2617

制作一个 MongoDBDocker 镜像,你可以按照以下步骤进行操作: 1. 创建 Dockerfile 文件,在其中定义镜像的构规则。可以使用任何文本编辑器创建该文件。 2. 在 Dockerfile 中指定基础镜像,例如使用官方提供的 MongoDB 镜像作为基础镜像。可以使用以下指令来指定基础镜像: ```Dockerfile FROM mongo:latest ``` 3. 可选:如果你需要在容器中设置环境变量、复制文件或运行其他命令,请使用适当的 Dockerfile 指令进行操作。你可以根据需要添加以下指令来完成这些任务: - `ENV`:设置环境变量。 - `COPY`:复制文件或目录到容器中。 - `RUN`:运行命令。 例如,如果你想要将一个名为 `config.js` 的配置文件复制到容器中,可以使用以下指令: ```Dockerfile COPY config.js /path/to/container/config.js ``` 4. 保存 Dockerfile 文件。 5. 在命令行中,进入 Dockerfile 文件所在的目录,并执行以下命令来构建镜像: ```bash docker build -t my-mongodb:latest . ``` 这将使用 Dockerfile 构建一个名为 `my-mongodb` 的镜像,并且标记为 `latest` 版本。 6. 构建成功后,你可以使用以下命令来运行 MongoDB 容器: ```bash docker run -d --name my-mongodb-container -p 27017:27017 my-mongodb:latest ``` 这将在后台运行一个名为 `my-mongodb-container` 的容器,并将容器内的 MongoDB 服务端口映射到宿主机的 27017 端口。 现在,你已经成功制作了一个 MongoDBDocker 镜像,并且可以通过运行该镜像来创建和管理 MongoDB 容器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值