我正在尝试使用 FluentDocker 对 MongoDB 运行测试,但我无法连接到它,请参阅下面的代码。
[Fact]
public async Task TestMongoDbConnection3()
{
const string root = "root";
const string secret = "secret";
using (
var container =
new Builder().UseContainer()
.UseImage("mongo")
.WithEnvironment($"MONGO_INITDB_ROOT_USERNAME:{root}")
.WithEnvironment($"MONGO_INITDB_ROOT_PASSWORD:{secret}")
.ExposePort(27018)
.WaitForPort("27018/tcp", 30000 /*30s*/)
.Build()
.Start())
{
var config = container.GetConfiguration(true);
Assert.Equal(ServiceRunningState.Running, config.State.ToServiceState());
const string connectionString = "mongodb://root:secret@localhost:27018";
// insert data
const string collectionName = "Users";
var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase ("Users");
var collection = database.GetCollection<User>(collectionName);
const int id = 1;
var data = new Fixture().Build<User>()
.With(x => x.Id, id)
.Create();
// delete if exists, but got timeout after 30000 ms
await collection.DeleteOneAsync(x => x.Id == data.Id);
...
}
}
我尝试从 config (config.NetworkSettings.IPAddress) 替换 IP 的 localhost,但仍然超时。
任何想法?
非常感谢,