Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to mongodb using docker-compose?

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?

like image 228
Andrey Avatar asked Apr 01 '17 22:04

Andrey


People also ask

How do I access Docker in MongoDB?

You can connect to MongoDB on localhost:27017 . Then use the following command to open the MongoDB shell. I have used mymongo as an arbitrary container name, though you can replace mymongo with test-mongo or any other container name of your choosing. The show dbs command will display all your existing databases.

How do I link my local Docker to MongoDB?

For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.

Can I use MongoDB in Docker?

Can MongoDB Run in a Docker Container? MongoDB can run in a container. The official image available on Docker Hub contains the community edition of MongoDB and is maintained by the Docker team. This image can be used in a development environment.


1 Answers

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.

like image 60
Ashish Bista Avatar answered Sep 22 '22 21:09

Ashish Bista