我有一个docker-compose.yml文件,该文件使三个mongo容器包含三个副本集,我的容器构建没有任何问题,但是当我的服务正常运行时setupmongo.sh不起作用。我的意思是rs.initiate()函数在bash文件上不起作用,但是当我尝试手动运行rs.initiate()函数及其参数(在docker交互式终端中)时,它在正常工作,我的副本按应有的方式运行。结果,我需要在bash文件上运行rs.initiate()函数
感谢您的支持。
这里是我的docker-compose.yml文件
version: "3.3"
services:
mongo-local:
hostname: mongo-local
container_name: mongo-local
image: mongo:4.0.4
ports:
- 27017:27017
expose:
- 27017
restart: always
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
depends_on:
- mongo-local
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
depends_on:
- mongo-local
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
depends_on:
- mongo-local
mongo-config:
container_name: mongo-config
image: mongo:4.0.4
networks:
- mongo-dev-net
links:
- mongo1:mongo1
- mongo2:mongo2
- mongo3:mongo3
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:30001",
"priority": 2
},
{
"_id" : 1,
"host" : "mongo2:30002",
"priority": 0
},
{
"_id" : 2,
"host" : "mongo3:30003",
"priority": 0
}
]
}
)
EOF
我几乎收到一些错误
mongo-config | connecting to: mongodb://127.0.0.1:27017
mongo-config | 2020-01-07T13:23:26.173+0000 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
mongo-config | connect@src/mongo/shell/mongo.js:257:13
mongo-config | @(connect):1:6
mongo-config | exception: connect failed
mongo-config exited with code 0
按照您的配置,端口30001、30002和30003是HOST
端口,而不是CONTAINER
端口。格式为HOST:CONTAINER
。参见documentation。
因此,您必须连接到每个容器的27017并相应地更改入口点:mongo1:27010
,mongo2:27010
,mongo3:27010
。
这将起作用,因为您已经在每个容器中暴露了该端口。