[英]Spin Up Docker Mongo With Replica Set
For automation tests, both locally and on build servers, I'm trying to spin up a mongo image with a replica set (I need the oplog). 对于本地和构建服务器上的自动化测试,我正在尝试使用副本集启动mongo映像(我需要oplog)。
The replica set setup requires me to get into the mongo shell and call " rs.initiate() ". 副本集设置要求我进入mongo shell并调用“ rs.initiate() ”。 I want this all done in code. 我希望这一切都在代码中完成。
public void SpinUpMongoWithReplicaSet()
{
Process.Start("docker", "run -p 123:27017 --name test_mongo -d mongo:latest mongod --replSet rs0").WaitForExit();
var replicaSetProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "docker",
Arguments = "exec -it test_mongo mongo",
UseShellExecute = false,
RedirectStandardInput = true
}
};
replicaSetProcess.Start();
using(StreamWriter writer = replicaSetProcess.StandardInput)
{
writer.WriteLine("rs.initiate()");
writer.WriteLine("exit");
}
replicaSetProcess.WaitForExit();
}
However, the problem with the docker exec command. 但是,docker exec命令的问题。
When passing in -it or just -t , I get the following error: 传入-it或只是-t时 ,我收到以下错误:
the input device is not a TTY. 输入设备不是TTY。 If you are using mintty, try prefixing the command with 'winpty' 如果您使用mintty,请尝试使用'winpty'为命令添加前缀
The only solution I could find is to only pass in the -i flag. 我能找到的唯一解决方案是只传入-i标志。 For whatever reason, this then prevents me from connecting to the mongo container: 无论出于何种原因,这会阻止我连接到mongo容器:
MongoDB shell version v3.4.9 connecting to: mongodb://127.0.0.1:27017 2017-10-06T18:25:06.765+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused 2017-10-06T18:25:06.765+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:237:13 @(connect):1:6 exception: connect failed MongoDB shell版本v3.4.9连接到:mongodb://127.0.0.1:27017 2017-10-06T18:25:06.765 + 0000 W NETWORK [thread1]无法连接到127.0.0.1:27017,在(检查socket是否有错误)投票后),原因:连接被拒绝2017-10-06T18:25:06.765 + 0000 E QUERY [thread1]错误:无法连接到服务器127.0.0.1:27017,连接尝试失败:连接@src / mongo / shell / mongo.js:237:13 @(连接):1:6异常:连接失败
Does anyone have any experience in executing commands on docker images within C# code? 有没有人有在C#代码中执行docker镜像命令的经验?
For reference: this is a good guide to setting up a mongo replica set server with docker, but from the command line. 供参考:这是使用docker设置mongo副本集服务器的一个很好的指南,但是从命令行。 Docker Replica Set Setup Docker副本集设置
Turns out that Mongo has a way of evaluating text using the --eval flag. 事实证明,Mongo有一种使用--eval标志评估文本的方法。
This allows us to avoid using both -i and -t flags. 这允许我们避免使用-i和-t标志。 In this manner, the Process.Start() function will be sufficient. 通过这种方式, Process.Start()函数就足够了。
Solution: 解:
Process.Start("docker", "exec test_mongo mongo --eval \"rs.initiate()\"").WaitForExit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.