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

时间:2017-04-01 22:57:13

标签: mongodb docker docker-compose

搬运工-compose.yml

mongo:
  image: tutum/mongodb
  environment:
    - AUTH=no
  volumes:
    - /Users/andrey/docker/mongodb:/mongo/db
  ports:
    - "27017:27017"
parser:
  image: nazandr/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"]

连接mongo需要使用哪个地址

2 个答案:

答案 0 :(得分:15)

您可以执行以下操作:

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:

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

如果您想从主机连接,如果您已经公开了如上所示的mongo端口,则可以使用mongodb://localhost:27017

答案 1 :(得分:0)

这就是我的方法

$destination = 'C:\path\of\target\directory'
$sourceFile  = 'path\fileToCopy.txt'           # the path to the file without drive letter

# get (an array of) USB disk drives currently connected to the pc
$wmiQuery1 = 'ASSOCIATORS OF {{Win32_DiskDrive.DeviceID="{0}"}} WHERE AssocClass = Win32_DiskDriveToDiskPartition'
$wmiQuery2 = 'ASSOCIATORS OF {{Win32_DiskPartition.DeviceID="{0}"}} WHERE AssocClass = Win32_LogicalDiskToPartition'

$usb = Get-WmiObject Win32_Diskdrive | Where-Object { $_.InterfaceType -eq 'USB' } | 
    ForEach-Object {
        Get-WmiObject -Query ($wmiQuery1 -f $_.DeviceID.Replace('\','\\'))   #'#  double-up the backslash(es)
    } | 
    ForEach-Object {
        Get-WmiObject -Query ($wmiQuery2 -f $_.DeviceID)
    }

# loop through these disk(s) and test if the file to copy is on it
$usb | ForEach-Object {
    # join the DeviceID (like 'H:') with the file path you need to copy
    $file = Join-Path -Path $_.DeviceID -ChildPath $sourceFile
    if (Test-Path -Path $file -PathType Leaf) {
        Copy-Item -Path $file -Destination $destination
        break  # exit the loop because you're done
    }
}

和c#中的MongoDb代码

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

还有Api项目中的docker文件

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);
    }
  }
}