我有一个docker-compose.yml文件,该文件使三个mongo容器包含三个副本集,我的容器构建没有任何问题,但是当我的服务正常运行时setupmongo.sh不起作用。我的意思是rs.initiate()函数在bash文件上不起作用,但是当我尝试手动运行rs.initiate()函数及其参数(在docker交互式终端中)时,它在正常工作,我的副本按应有的方式运行。结果,我需要在bash文件上运行rs.initiate()函数
这里是我的docker-compose.yml文件
version: "3.3"
services:
mongo1:
hostname: mongo1
container_name: mongo1
image: mongo:4.0.4
command: mongod --replSet devrs
volumes:
- ./mongodb/data1/db:/data/db
- ./mongodb/data1/configdb:/data/configdb
networks:
- mongo-dev-net
expose:
- 27017
ports:
- 30001:27017
restart: always
mongo2:
hostname: mongo2
container_name: mongo2
image: mongo:4.0.4
command: mongod --replSet devrs
volumes:
- ./mongodb/data2/db:/data/db
- ./mongodb/data2/configdb:/data/configdb
networks:
- mongo-dev-net
expose:
- 27017
ports:
- 30002:27017
restart: always
mongo3:
hostname: mongo3
container_name: mongo3
image: mongo:4.0.4
command: mongod --replSet devrs
volumes:
- ./mongodb/data3/db:/data/db
- ./mongodb/data3/configdb:/data/configdb
networks:
- mongo-dev-net
expose:
- 27017
ports:
- 30003:27017
restart: always
mongo-config:
container_name: mongo-config
image: alpine
networks:
- mongo-dev-net
depends_on:
- mongo1
- mongo2
- mongo3
volumes:
- ./setupmongo.sh:/setupmongo.sh
entrypoint: ["/setupmongo.sh"]
networks:
mongo-dev-net:
和setupmongo.sh文件
#!/bin/bash
echo "Start"
mongo <<EOF
rs.config()
rs.initiate(
{
"_id" : "devrs",
"members" : [
{
"_id" : 0,
"host" : "mongo1:27017",
"priority": 2
},
{
"_id" : 1,
"host" : "mongo2:27017",
"priority": 0
},
{
"_id" : 2,
"host" : "mongo3:27017",
"priority": 0
}
]
}
)
EOF
最后一个副本集未初始化
按照您的配置,端口30001、30002和30003是HOST
端口,而不是CONTAINER
端口。格式为HOST:CONTAINER
。参见documentation。
因此,您必须连接到每个容器的27017并相应地更改入口点:mongo1:27010
,mongo2:27010
,mongo3:27010
。
这将起作用,因为您已经在每个容器中暴露了该端口。