40

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?

2
  • Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified. So, link containers, and access mongodb using service name as hostname. You can check /etc/hosts file inside linked containers that automatically created. Commented Apr 1, 2017 at 23:18
  • Can you paste your entire docker-compose.yml file? Commented Apr 1, 2017 at 23:19

2 Answers 2

56

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I understand that mongo is a service name. But why did you start url with mongodb? it appeared from nowhere.
@S.R that's part of the standard mongodb URI.
Hi, I have a mongo DB in a remote server running on Port 28107, something like below: "mongoDb": { "url": "mongodb://test:test@ip_address:28107/test" }, So, how can I connect from my Docker to this remote mongo DB. I don't want to export the DB data to my local system.
8

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.