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).
The replica set setup requires me to get into the mongo shell and call "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.
When passing in -it or just -t, I get the following error:
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
The only solution I could find is to only pass in the -i flag. For whatever reason, this then prevents me from connecting to the mongo container:
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
Does anyone have any experience in executing commands on docker images within C# code?
For reference: this is a good guide to setting up a mongo replica set server with docker, but from the command line. Docker Replica Set Setup