如何使用 docker-compose 连接到 mongodb?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43162897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:56:20  来源:igfitidea点击:

How to connect to mongodb using docker-compose?

mongodbdockerdocker-compose

提问by Andrey

docker-compose.yml

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

和 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 need to use to connect mongo

连接mongo需要使用什么地址

回答by Ashish Bista

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 mongocontainer from other containers located in the same docker-compose.ymlfile using the connection string like mongodb://mongo:27017.

正如你可能已经注意到,您可以连接到Mongo的服务运行mongo从位于同其他容器容器docker-compose.yml使用连接字符串,如文件mongodb://mongo:27017

In case you want to connect from the host, you can use mongodb://localhost:27017if you have exposed mongo port as shown above.

如果你想从主机连接,你可以使用,mongodb://localhost:27017如果你已经暴露了 mongo 端口,如上所示。

回答by unos baghaii

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#

和 C# 中的 MongoDb 代码

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

以及 Api 项目中的 docker 文件

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