Reputation: 663
This is my docker-compose.yml
:
mongo:
image: tutum/mongodb
environment:
- AUTH=no
volumes:
- /Users/andrey/docker/mongodb:/mongo/db
ports:
- "27017:27017"
parser:
image: nazandr/goparser
and Dockerfile goparser:
FROM golang:1.8
WORKDIR /app
ADD parser.go /app/
RUN go get github.com/PuerkitoBio/goquery; go get gopkg.in/mgo.v2; go build -o parser
ENTRYPOINT ["./parser"]
What address do I need to use to connect to MongoDB?
Upvotes: 40
Views: 72126
Reputation: 2679
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"]
Upvotes: 8
Reputation: 4693
You can do something like below:
version: '3'
services:
mongo:
image: 'mongo:3.4.1'
ports:
- '27017:27017'
volumes:
- 'mongo:/data/db'
puma:
tty: true
stdin_open: true
depends_on:
- 'mongo'
build:
context: .
dockerfile: Dockerfile.puma
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- '3000:3000'
volumes:
- '.:/app'
environment:
- SECRET_KEY_BASE=secret
- MONGO_URL=mongodb://mongo:27017/app_development
volumes:
mongo:
As you might have noticed, you can connect to mongo service running on mongo
container from other containers located in the same docker-compose.yml
file using the connection string like mongodb://mongo:27017
.
In case you want to connect from the host, you can use mongodb://localhost:27017
if you have exposed mongo port as shown above.
Upvotes: 56