I can not connect to mongodb container with mongodb compass - mongodb

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

Related

Can't authenticate mongosh in docker container

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

Pgadmin does not start with docker-compose-file

I started working with postgres and discovered pgadmin.
I started like this:
Postgres:
$ docker run --name admin -e POSTGRES_PASSWORD=admin -p 5432:5432 -d postgres:latest
Pgadmin:
docker run -p 5050:80 -e "PGADMIN_DEFAULT_EMAIL=admin#admin.com" -e "PGADMIN_DEFAULT_PASSWORD=root" -d dpage/pgadmin4
So this worked perfectly fine, I started the postgres container and afterwarda the pgadmin cointainer and got on the site http://localhost:80/login and could login
The problem now is the docker-compose.yml that I wrote. As I deploy my docker-compose-file, the containers are both running but I can't access the login page of pgadmin
Docker-compose.yml:
version: '3.8'
services:
db:
image: postgres:latest
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
ports:
- "5432:5432"
pgadmin:
image: dpage/pgadmin4:latest
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
When I run it, it works as it should. So I think your issue is the URL you try to access it on. You need to access it on the mapped port 5050. Not 80, as you've written. So the URL is http://localhost:5050/login.

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

Create Mongo docker image and add user to database

I am trying to find a way to create a docker container that when created has the db user profile created automatically. I am using a dockerfile and thought i could add the commands to create the user in it.
Anyone done this before and know what I am doing wrong here. I am no expert at Docker.
FROM mongo:latest
RUN mongo &&\
use tewtdb &&\
db.createUser({user: '<user>', pwd: '<pwrd>', roles[{role: 'dbOwner', db: 'tewtdb'}]})
EXPOSE 27017
CMD ["mongod"]
shell
docker run --name mongodb -d -e MONGO_INITDB_ROOT_USERNAME=user -e MONGO_INITDB_ROOT_PASSWORD=pwd -v D:/mdb:/data/db -p 27117:27017 mongo:5.0
dockerfile-compose
version: '3'
services:
standalone:
image: mongo:5.0
ports:
- 27117:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=pwd
volumes:
- D:/mdb:/data/db
docker-compose -f docker-compose.yml up -d

Dump mongo database running on a docker container

I have this docker-compose.yml file where I run a mongo container
version: '3'
services:
appapi:
container_name: appapi
image: strapi/strapi:3.1.3
environment:
DATABASE_CLIENT: ${APPAPI_DATABASE_CLIENT}
DATABASE_HOST: ${APPAPI_DATABASE_HOST}
DATABASE_PORT: ${APPAPI_DATABASE_PORT}
DATABASE_NAME: ${APPAPI_DATABASE_NAME}
DATABASE_USERNAME: ${APPAPI_DATABASE_USERNAME}
DATABASE_PASSWORD: ${APPAPI_DATABASE_PASSWORD}
ports:
- 1337:1337
volumes:
- ./app:/srv/app
depends_on:
- appmongo
appmongo:
container_name: appmongo
image: mongo:4.4.0
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${APPDB_MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${APPDB_MONGO_INITDB_ROOT_PASSWORD}
ports:
- "27027:27017"
volumes:
- ./data/db:/data/db
I want to backup the database running a dump
docker run -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=admin --rm mongo mongodump --host mongoapp:27027 --archive --gzip | cat > ./mongodumps/dump_$(date '+%d-%m-%Y_%H-%M-%S').gz
I tried to modify the previous command but I am not able to connect and do the dump, I am getting
2020-08-15T19:27:04.870+0000 Failed: can't create session: could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: mongoapp:27027, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : dial tcp: lookup mongoapp on 192.168.65.1:53: no such host }, ] }
I was able to dump/restore with the following commands
dump
docker exec defymongo sh -c 'mongodump --archive -u {{mongouser}} -p {{mongopass}}' > ./mongodumps/dump_$(date '+%d-%m-%Y_%H-%M-%S').gz
restore
docker exec -i defymongo sh -c 'mongorestore --archive -u {{mongouser}} -p {{mongopass}}' < ./mongodumps/dump_$(date '+%d-%m-%Y_%H-%M-%S').gz
The difference is here the commands use sh -c to execute mongorestore and pass parameters with authentication values.
This is not enough to backup Strapi. Probably there are some values inside the /src/app folder in Strapi that should also be backed up
Hopefully this will helps someone else