How to connect to mongodb using docker-compose?

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 should be used to connect mongo

+4
source share
1 answer

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 may have noticed, you can connect to the mongo service launched in the container mongofrom other containers located in the same file docker-compose.ymlusing the connection string, for example mongodb://mongo:27017.

If you want to connect to the host, you can use mongodb://localhost:27017if you opened the mongo port, as shown above.

+8
source

Source: https://habr.com/ru/post/1673790/


All Articles