This is how I do it
version: '3.4'
services:
eventstoresample:
image: eventstoresample
container_name: "es_api"
build:
context: .
dockerfile: EventStoreSample/Dockerfile
networks:
clusternetwork:
ipv4_address: 172.16.0.12
eventstore:
image: eventstore/eventstore
container_name: "es"
environment:
- EVENTSTORE_INT_IP=172.16.0.13
- EVENTSTORE_EXT_HTTP_PORT=2113
- EVENTSTORE_EXT_TCP_PORT=1113
- EVENTSTORE_EXT_HTTP_PREFIXES=http://*:2113/
ports:
- "1113:1113"
- "2113:2113"
networks:
clusternetwork:
ipv4_address: 172.16.0.13
mongodb:
image: mongo:latest
container_name: "mongodb"
environment:
- MONGO_INITDB_ROOT_USERNAME=test
- MONGO_INITDB_ROOT_PASSWORD=test
ports:
- "27014:27017"
networks:
clusternetwork:
ipv4_address: 172.16.0.14
networks:
clusternetwork:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.16.0.0/24
and the MongoDb code in c#
using MongoDB.Driver;
using System;
using System.Threading.Tasks;
namespace EventStoreSample.MongoDbConfigs
{
public class MongoDb : IMongoDb
{
private IMongoCollection<T> GetCollection<T>()
{
var server = new MongoServerAddress(host: "172.16.0.14", port: 27017);
//var server = new MongoServerAddress(host: "localhost", port: 27014);
var credentials = MongoCredential.CreateCredential(
databaseName: "admin",
username: "test",
password: "test"
);
var mongodbClientSettings = new MongoClientSettings
{
Credential = credentials,
Server = server,
ConnectionMode = ConnectionMode.Standalone,
ServerSelectionTimeout = TimeSpan.FromSeconds(3)
};
MongoClient dbClient = new MongoClient(mongodbClientSettings);
var database = dbClient.GetDatabase("EventSampleApiDB");
var collection = database.GetCollection<T>(typeof(T).Name);
return collection;
}
public async Task InsertOneAsync<T>(T entity)
{
await GetCollection<T>().InsertOneAsync(entity);
}
}
}
And the docker file in Api project
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /src
COPY . .
RUN dotnet restore "EventStoreSample/EventStoreSample.csproj"
RUN dotnet build "EventStoreSample/EventStoreSample.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "EventStoreSample/EventStoreSample.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "EventStoreSample.dll"]