MongoDb : Cannot connect database on Docker container - mongodb

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.

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?

SpringBoot and MongoDb integration in the Docker

I have a SpringBoot application running in the docker and I am using PostgreSQL as a database for this project and the database also running in the docker.
Now, I want to use MongoDb along with PostgreSQL as database to my SpringBoot application.
I added MongoDb info in the docker-compose.yml file and created new Dockerfile and ran the application. After that MongoDb got installed and running in the docker successfully.
I created a api for insertion of a document into the collection. When I hit the api I am getting the error. I think, I am not able to connect to the MongoDb which is running in the docker.
error:- com.mongodb.MongoSocketOpenException: Exception opening socket
I think I have to do configuration in the MongoDb before doing any CRUD operations.
Can anyone please share a detailed configuration of MongoDb with some examples.
Or provide some information which can help me achieve my task.
Thanks.
Docker-compose.yml
mongodb:
build:
context: mongodb
args:
DOCKER_ARTIFACTORY: ${DOCKER_ARTIFACTORY}
container_name: “mongodb”
image: mongo:6.0.4
restart: always
environment:
- MONGODB_USER=${SPRING_DATASOURCE_USERNAME:-username}
- MONGODB_PASSWORD=${SPRING_DATASOURCE_PASSWORD:-password}
ports:
- “27017:27017”
volumes:
- “/mongodata:/data/mongodb”
networks:
- somenetwork
Dockerfile
ARG DOCKER_ARTIFACTORY
FROM ${DOCKER_ARTIFACTORY}mongo:6.0.4
COPY init/mongodbsetup.sh /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/mongodbsetup.sh
CMD [“mongod”]
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: password
mongodb:
image: mongo
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
You can find above configuration of docker file that includes both MongoDB and PostgreSQL configurations.
I resolve the issue and successfully able to connect and able to perform CRUD operations too.
Below are the changes I did.
1. docker-compose.yml
mongodb:
build:
context: mongodb
args:
DOCKER_ARTIFACTORY: ${DOCKER_ARTIFACTORY}
container_name: mongodb
hostname: mongodb
restart: always
environment:
- MONGO_INITDB_DATABASE=databaseName
- MONGO_INITDB_ROOT_USERNAME=username
- MONGO_INITDB_ROOT_PASSWORD=password
ports:
- 27017:27017
volumes:
- /mongodata:/data/mongodb
networks:
- somenetwork
created below file (filename: mongodbsetup.sh) under projectFolder/builder/mongodb/init
#!/bin/bash
mongosh <<EOF
use code
EOF
created below file (filename: Dockerfile) under projectFolder/builder/mongodb
ARG DOCKER_ARTIFACTORY
FROM ${DOCKER_ARTIFACTORY}mongo:6.0.4
COPY init/mongodbsetup.sh /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/mongodbsetup.sh
CMD ["mongod"]
In application.properties file added below properties
#MongoDB configurations
spring.data.mongodb.database=databasename
spring.data.mongodb.uri=mongodb://username:password#databaseurlOrIpAddress:27017
That's it. It's working.

mongo-express | Could not connect to database using connectionString: mongodb://127.0.0.1:27017/"

When trying to spin up a docker container with docker-compose,
I get the following error message:
(node:8) [MONGODB DRIVER] Warning: Current Server Discovery
and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongo-express | Could not connect to database using connectionString: mongodb://127.0.0.1:27017/"
I noticed that mongo-express always adds a double quote to the end of my connection string, is this intentional or did I mess something up in my docker-compose file?
The file:
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: unless-stopped
networks:
- backbone
expose:
- 27017
ports:
- 27017:27017
env_file:
- ./.env
command: [--auth]
environment:
- MONGO_INITDB_ROOT_USERNAME=${DB_USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- ./data:/data/db
mongo-express:
image: mongo-express
container_name: mongo-express
restart: unless-stopped
networks:
- backbone
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_SERVER=127.0.0.1
- ME_CONFIG_MONGODB_URL="mongodb://127.0.0.1:27017"
- ME_CONFIG_BASICAUTH_USERNAME=${DB_USERNAME}
- ME_CONFIG_BASICAUTH_PASSWORD=${DB_PASSWORD}
networks:
backbone:
driver: bridge
I already tried updating my version of docker/docker-compose and mongodb.
How do I resolve the error?
Or is there a workaround I could use?
In a container, localhost refers to the container itself. So when Mongo Express tries to connect to the database at localhost, it looks for the database inside the Mongo Express container.
Docker compose let's you refer to containers using their container names, so you should change your Mongo Express environment variables to
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_URL="mongodb://mongodb:27017"

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 use Robo3T to connect mongodb server running as a docker container

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