How to use Robo3T to connect mongodb server running as a docker container - mongodb

I have a mongodb running as a docker container which created by docker-compose.
Here is my docker-compose.yml:
verion:'3.7'
services:
mongo_env:
image: mongo:4.2.1-bionic
ports:
- "27017:27017"
volumes:
- $PWD/DBVOL/mongo/data:/data/db:rw
environment:
MONGO_INITDB_ROOT_USERNAME: <rootuser>
MONGO_INITDB_ROOT_PASSWORD: <mypassword>
MONGO_INITDB_DATABASE: <mydatabase>
I want to connect mongodb from outside using Robo 3T,But It always tell me that 'Failed to load database'.Here is my Robo 3T config:
Please Help me It's emergency!

Are your sure it started successfull? Your Docker Compose has a typo in "version:3.7".
correct version:
version: '3.7'
services:
mongo_env:
image: mongo:4.2.1-bionic
ports:
- "27017:27017"
volumes:
- $PWD/DBVOL/mongo/data:/data/db:rw
environment:
MONGO_INITDB_ROOT_USERNAME: <rootuser>
MONGO_INITDB_ROOT_PASSWORD: <mypassword>
MONGO_INITDB_DATABASE: <mydatabase>
Please check after starting if the container is running with
docker ps

Related

Getting "MongoServerError: Authentication failed" when connecting to MongoDB in Docker container

Here is my docker-compose file:
version: '3' services: mongo:
image: mongo
container_name: mongo
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME: admin
- MONGO_INITDB_ROOT_PASSWORD: root
ports:
- 27017:27017
volumes:
- ./mongo-data:/data/db
command: --wiredTigerCacheSizeGB 1.5
I try to connect to Mongo container but constantly receive the same error:
MongoServerError: Authentication failed
Here are 2 variants of connection strings I've used, neither worked:
mongodb://admin:root#localhost:27017/admin
mongodb://admin:root#localhost:27017/admin?authSource=admin
What can I try next?

MongoDb : Cannot connect database on Docker container

I use the following docker-compose.yml to build a MongoDb database in a Docker container:
version: "3"
services:
mongodb:
image: mongo:latest
container_name: mongodb
volumes:
- mongo_data:/data/db
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: pass
MONGODB_USERNAME: user
MONGODB_PASSWORD: pass
MONGO_INITDB_DATABASE: social-network-db
volumes:
mongo_data:
Then I restarted the container and try to connect database via MongoDb Compass app by using these values:
Connection parameters
So, what is wrong and why I cannot connect to my MongoDb database? If you have some suggestion regarding to adocker-compose.yml parameter or creating used while building container, please inform me.

Can't connect to MongoDB inside docker container

I'm trying to connect to a mongodb container inside docker but I'm getting this error:
getaddrinfo ENOTFOUND mongo.ks.local
The database works just fine when I access it from docker but I'm not able to access it from MongoDBCompass
This is my docker-compose.yml file:
version: '3.8'
services:
mongo:
image: mongo
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: <username>
MONGO_INITDB_ROOT_PASSWORD: <password>
ports:
- 27017:27017
volumes:
- ks_mongodb:/data/db
I'm quite new to docker and will appreciate any help I can get
You need the ip address of the container:
try:
docker inspect <CONTAINER NAME>
And search for the ip address. Then use the ip address to connect to mongodb
I have tried connecting to the container using mongo compass. It failed but when I removed the line of mounting the volume and tried again it connects to the database.
Then tried to mount the volume like below it worked. I have changed the username and password and specified the INITDB env variable.
version: "3.8"
services:
mongo:
image: mongo:latest
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 1234
MONGO_INITDB_DATABASE: admin
ports:
- 27017:27017
volumes:
- /data/db

How to attach persistent volume in docker-compose file for mongodb?

I have a docker-compose file that will bring up mongo and mongo-express containers in the same network "mynet".
I have created a network by:
docker network create mynet
I have created a volume named "demo-vol" externally by docker command.
docker volume create demo-vol
Inside the container, I have created a sample mongo collection.
When I do docker-compose up I'm able to see the container running but I'm not able to find the mongo data in that specified volume.
Below is my docker-compose.yaml file
version: '3'
services:
mongo:
image: mongo
container_name: mymongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- "/demo-vol:/data/db"
networks:
- mynet
ports:
- 27017:27017
mongoexpress:
image: mongo-express
container_name: mymongoexpress
ports:
- 8081:8081
volumes:
- "/demo-vol:/data/db"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
depends_on:
- mongo
networks:
- mynet
volumes:
demo-vol:
external: true
networks:
mynet:
external: true
What I need is:
Even after deleting the container, I want my data to be persistent.
How to do that and please explain. Where i'm going wrong?
Note:I'm a beginner to Docker concepts.
Thanks in advance.
You can to use local driver for volume
volumes:
demo-vol:
driver: local
and try to remove slash
volumes:
- demo-vol:/data/db

docker: create a mongodb volume that is still saved after docker-compose down?

I have the following docker-compose.yml file:
version: "3"
services:
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerstats
My issue is that when I run docker-compose down all the data within my mongo database is lost.
How can I create a mongo volume that persists even when the mongo container goes down?
Per the image documentation the database volume needs to be /data/db. This is also seen in the Dockerfile volume.
Since the volume is defined in the Dockerfile, if you do not create a volume at that directory, even if you created a volume in the parent like /data, docker will create an anonymous volume at /data/db which will show up as a long guid volume name in docker volume ls. Depending on how the container is run, those may be left behind.
Therefore the fix is to adjust your volume mount to that path:
version: "3"
services:
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data/db
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerstats
Note that unless you need direct access to this data on the host, I'd recommend using a named volume instead. It includes initialization steps that helps with permission issues you may encounter with host volumes, particularly when running directly on a Linux host.
To use a named volume, that would look like:
version: "3"
services:
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- dbdata:/data/db
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerstats
volumes:
dbdata:
Try to change volumes for MongoDb container:
volumes:
- "./database:/data/db"