问题描述
我正在设置一个 docker 容器来使用 docker compose 初始化 Mongo DB 副本集.我无法完成这项工作.我创建了 docker-compose 来调用三个 mongo 实例,但我遇到了解析器问题.下面是我的 Docker-Compose 文件.
I am setting up a docker container to initialize the Mongo DB replica set using docker compose. I am not able to get this done. I have created docker-compose to invoke three mongo instance but i am getting parser issue. Below is my Docker-Compose file.
version: '3'
services:
mongo-vauto-1:
image: "mongo-start"
build: ./mongo-vAUTO-1
ports:
- "30000:27017"
volumes:
- ./mongo-vAUTO-1/data:/data/db
depends_on:
- "mongo-vauto-2"
- "mongo-vauto-3"
mongo-vauto-2:
image: "mongo"
command: --replSet vAUTO --smallfiles --oplogSize 128
ports:
- "40000:27017"
volumes:
- ./mongo-vAUTO-2/data:/data/db
mongo-vauto-3:
image: "mongo"
command: --replSet vAUTO --smallfiles --oplogSize 128
ports:
- "50000:27017"
volumes:
- ./mongo-vAUTO-3/data:/data/db
mongo-setup-vauto:
image: "mongo-setup-vauto"
build: ./setup
depends_on:
- "mongo-vauto-1"
调用 ./setup 文件夹中的 Dockerfile.下面是我的设置Dockerfile
which invokes Dockerfile which is in ./setup folder. Below is my set upDockerfile
FROM mongo
# Create app directory
WORKDIR vauto-docker/src/config
# Install app dependencies
COPY replicaSet.js .
COPY setup.sh .
CMD ["./setup.sh"]
调用 replicaSet.js 和 setup.sh.下面提供replicaSet.js 和setup.sh 文件
which invokes replicaSet.js and setup.sh. The replicaSet.js and setup.sh files are provided below
# replicaSet.js
rsconf = {
_id : "vAUTO",
members: [
{_id : 0, host : "mongo-vauto-1:27017"},
{_id : 1, host : "mongo-vauto-2:27017"},
{_id : 2, host : "mongo-vauto-3:27017"}
]
}
rs.initiate(rsconf);
rs.conf();
# setup.sh
echo *********************************
echo Starting the replica set
echo *********************************
sleep 10 | echo waiting for the replica set to be intialized
mongo mongodb://mongo-vauto-1:27017 replicaSet.js
Dockerfile 第一个副本集如下所示
Dockerfile for first replica set is given below
FROM mongo
WORKDIR vauto-docker/src/config
copy mongo.conf .
EXPOSE 27017
CMD ["--config","./mongo.conf"]
我的 mongo.conf 文件有以下代码
and my mongo.conf file has the following code
replication:
oplogSizeMB: 1024
replSetName: vAUTO
我收到以下错误,
mongo-vauto-1_1 | parse error: Invalid numeric literal at line 1, column 14
mongo-vauto-1_1 | parse error: Invalid numeric literal at line 1, column 14
mongo-vauto-1_1 | Error parsing YAML config file: yaml-cpp: error at line 3, column 1: illegal tab when looking for indentation
推荐答案
下面是我如何在 3 个 mongodb 容器运行后使用第 4 个容器初始化副本集:
Here is how I did this using a 4th container to initialize the replica set after 3 mongodb containers are running:
Docker-compose 文件
version: '3'
services:
mongodb1:
image: mongo:latest
networks:
- alphanetwork
volumes:
- data1:/data/db
- config1:/data/configdb
ports:
- 30001:27017
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongodb2:
image: mongo:latest
networks:
- alphanetwork
ports:
- 30002:27017
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongodb3:
image: mongo:latest
networks:
- alphanetwork
ports:
- 30003:27017
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongoclient:
image: mongo
networks:
- alphanetwork
depends_on:
- mongodb1
- mongodb2
- mongodb3
volumes:
- ./deployment_scripts:/deployment_scripts
entrypoint:
- /deployment_scripts/initiate_replica.sh
networks:
alphanetwork:
volumes:
data1:
config1:
initiate_replica.sh
#!/bin/bash
echo "Starting replica set initialize"
until mongo --host mongodb1 --eval "print("waited for connection")"
do
sleep 2
done
echo "Connection finished"
echo "Creating replica set"
mongo --host mongodb1 <<EOF
rs.initiate(
{
_id : 'rs0',
members: [
{ _id : 0, host : "mongodb1:27017" },
{ _id : 1, host : "mongodb2:27017" },
{ _id : 2, host : "mongodb3:27017" }
]
}
)
EOF
echo "replica set created"
别忘了chmod +x ./deployment_scripts/initiate_replica.sh
给docker执行它的权限.
And don't forget to chmod +x ./deployment_scripts/initiate_replica.sh
to give docker the permission to execute it.
这篇关于docker-compose 在 mongoDB 中创建复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!