Can't authenticate mongosh in docker container - mongodb

have my docker-compose.yml:
version: '3.8'
services:
mongodb:
image: mongo
container_name: mongoContacts
environment:
- MONGO_INITDB_DATABASE=admin
- MONGO_INITDB_ROOT_USERNAME=${USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${PASSWORD}
ports:
- 27017:27017
.env file:
USERNAME="user"
PASSWORD="qwerty1234"
but when trying to connect to mongosh with user using:
root#3db279a47eac:/# mongosh -u user -p qwerty1234
Getting the following error
Current Mongosh Log ID: 636ec9254618b361de9ab9e1
Connecting to: mongodb://<credentials>#127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
MongoServerError: Authentication failed.
What could it be?
New in docker, tried to restart container, also tried to reinstall mongo, remove "" and '' and nothing worked

Related

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.

I can not connect to mongodb container with mongodb compass

Here is my docker-compose file
version: "3.8"
services:
mongodb:
image: mongo
restart: always
container_name: mongodb_container_gold_jar
ports:
- 27017:27017
environment:
MONGO_INITDB_DATABASE: root
MONGO_INITDB_ROOT_USERNAME: haolamongodb
MONGO_INITDB_ROOT_PASSWORD: haolamongodb1210
volumes:
# named volumes
- mongodb_gold_jar:/data/db
- mongoconfig_gold_jar:/data/configdb
And here is my connection string: mongodb://haolamongodb:haolamongodb1210#127.0.0.1:27017/root.
I also try to use localhost instead of 127.0.0.1 but it still does not work
the error message:
I have searched a lot but still got stuck on this, so pls help me figure it out. Thanks
I finally figured it out.
My connection string is missing the authenticationDatabase.
I tried to connect with mongosh and succeeded.
docker run -it --rm --network server_default mongo \
mongosh --host mongodb_container_gold_jar \
-u haolamongodb -p haolamongodb1210 --authenticationDatabase admin \
root
so the connection string should be
mongodb://haolamongodb:haolamongodb1210#127.0.0.1:27017/root?authSource=admin

Error: timeout expired on trying to connect in Docker Postgres using pgAdmin

I've been created a docker container of postgres service, but on start it and try to connect in database I get erros like I didn't defined a user and database to Postgres instance, I already tried to change the docker-compose and find the poblem but I didn't find.
Follow the attachments:
Dockerfile:
FROM wyveo/nginx-php-fpm:latest
RUN chmod -R 775 /usr/share/nginx/
RUN export pwd=pwd
docker-compose.yml:
version: '3'
services:
laravel-app_prm:
build: .
ports:
- "8099:80"
volumes:
- ${pwd}/.docker/nginx/:/usr/share/nginx
postgres_prm:
image: postgres
restart: always
environment:
- POSTGRES_USER=db_usr
- POSTGRES_PASSWORD=postgres_password
- POSTGRES_DB=db_prm
ports:
- "5432:5440"
volumes:
- ${pwd}/.docker/dbdata:/var/lib/postgresql/data/
**when I try to connect to the database directly through the container's bash, I get an error that user and database, both being inserted in the same way as defined in docker-compose.yml, do not exist.
sudo docker container <postgres_container_id> bash
psql -h localhost -U db_usr
... and so on...
And to set up connection in pgAdmin I got the container IP using:
sudo docker container inspect <postgres_container_id>
and getting the value from IPAddress atribute.

Docker compose getting connect ECONNREFUSED 127.0.0.1:3333 with AdonisJS, Postgres

i am trying to setup an application with docker/adonis/postgres/mongo,
but I am getting an ECONNREFUSED error
Error: connect ECONNREFUSED 127.0.0.1:3333
here is my dockerfile
FROM node
WORKDIR /usr/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm i -g #adonisjs/cli && npm install
COPY . .
EXPOSE 3000
CMD ["node", "ace", "serve"]
dockercompose
version: '3.4'
services:
app:
build: .
ports:
- 3000:3000
volumes:
- .:/usr/app
db:
image: postgres
environment:
POSTGRES_PASSWORD: ${PG_PASSWORD}
POSTGRES_USER: ${PG_USER}
POSTGRES_DB: ${PG_DB_NAME}
ports:
- 5432:5432
mongo:
image: mongo
environment:
MONGO_PASSWORD: 1234
MONGO_USER: MONGO
ports:
- 27017:27017
Your error message mentions port 3333. But nowhere in your docker-compose file is a port 3333 exposed to the Host.
Where do you try to connect from?
If from app container, then services like 'db' and 'mongo' are not on localhost. Looking from inside 'app' service, the db service is at db:5432 and the mongo service is at mongo:27017.
If from the host, then db is on localhost:5432 and mongo is at localhost:27017.
Is becomes more clear is you use other ports on the host than internally.

Docker-compose postgresql password authentication failed

I'm trying to get a setup going with a webservice that consumes a postgres database. Should be simple to setup, but I'm getting errors. So, first thing I want to make sure is that the database I set up is actually there and running.
To test this I substitute the "consumer" or "client" for an alpine interactive shell like so:
version: '3'
services:
db:
image: postgres:10.1-alpine
container_name: db
expose:
- 5432
volumes:
- "dbdata:/var/lib/postgresql/data"
environment:
- POSTGES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=db
web:
image: alpine:latest
stdin_open: true
tty: true
entrypoint: /bin/sh
depends_on:
- db
volumes:
dbdata:
Then I run the following command to get into the interactive shell:
docker-compose run web
and the following command to get in the database:
apk --update add postgresql-client && rm -rf /var/cache/apk/*
psql -h db -U user db
I get a plain denial from postgresql:
psql: FATAL: password authentication failed for user "user"
Same error message for each combo of username/password/databasename I try. Not much helpful.
What am I doing wrong here?
You have a typo in your docker-compose file. You mispelled POSTGRES here:
POSTGES_USER=user
That means the user user isn't being created. If I correct that typo, so that I have:
version: '3'
services:
db:
image: postgres:10.1-alpine
expose:
- 5432
volumes:
- "dbdata:/var/lib/postgresql/data"
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=db
web:
image: alpine:latest
stdin_open: true
tty: true
entrypoint: /bin/sh
depends_on:
- db
volumes:
dbdata:
Start the environment:
docker-compose up -d
Attach to the web contained and install the postgresql client:
$ docker attach project_web_1
/ # apk add --update postgresql-client
Then I can connect without a problem:
/ # psql -h db -U user db
Password for user user:
psql (11.2, server 10.1)
Type "help" for help.
db=#