How to connect to a MongoDb of a docker container - mongodb

I've created the following docker-compose.yml:
version: "3"
services:
mongo:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
ports:
- '27017:27017'
I then start my containers:
docker-compose up
then I try to connect into MongoDb Compass(also tried through c# code), with the following:
mongodb://admin:admin#localhost:27017/?authSource=admin
mongodb://admin:admin#localhost:27017
mongodb://admin:admin#127.0.0.1:27017
But I always get a "Authentication failed" message:
I really don't understand what is going on. What am I missing.
Sorry for the dumb question...

The behavior you're seeing suggests that there is already another mongodb instance running on your system (with different authentication credentials). Stop the Docker container and check to see if there is still a mongodb service listening on port 27017.

I think it might be better to set ports to
ports:
- '27012:27017'
So you don't need to delete anything from your system.
Now you can connect as ussual, just change port to 27012 instead of 27017
Also this way you can run multiple monogdb's using more ports wthout any issues. In my case I still needed local mongodb running and because of that port 27017 was automatically connecting to it.

Related

DataGrip Cannot Connect to Local PostgreSQL Running in Docker Desktop for Windows with WSL2 Backend

I have Docker Desktop installed on windows 10. It uses WSL2 back-end. I have 3 databases running on docker. One Mongo, One Clickhouse, and one PostgreSQL. DataGrip can easily connect to the Clickhouse on localhost:8123, and also to the Mongo on localhost:27017 but for some reason it cannot connect to the PostgreSQL running on 5432.
The peculiar thing about this is that pgAdmin can connect to the PostgreSQL on localhost:5432.
DataGrip can easily connect to the two other containers in this docker-compose file.
This is my docker compose, which I use to run the three containers:
version: "3.9"
services:
postgres:
image: postgres:15.1-alpine
restart: on-failure
ports:
- "5432:5432"
volumes:
- fpm_pg:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_DB=arm
- POSTGRES_PASSWORD=postgres
mongo:
image: "mongo:latest"
ports:
- "27017:27017"
env_file:
- .env
restart: "no"
clickhouse:
image: "clickhouse/clickhouse-server"
ports:
- "8123:8123"
- "9000:9000"
- "9004:9004"
depends_on:
- postgres
env_file:
- .env
restart: "no"
volumes:
fpm_pg:
driver: local
Error:
DBMS: Case sensitivity: plain=mixed,
delimited=exact
Driver: (ver. , JDBC)
Effective version: PostgreSQL (ver. 0.0)
The connection attempt failed.
Has anyone encountered this?
I also cannot establish this connection from within "Goland", which was the first thing I tried.
I did read this: DataGrip [08001] The connection attempt failed. The connection attempt failed, but it does not help.
OK. I solved it. After reading the log file myself, I noticed that it's a socket exception which led me to believe that there's something wrong with the port 5432. I still maintain that because pgAdmin could connect to that DB, DataGrip should have been able to do the same, but let's go down the rabbit hole shall we?
I used this video to inspect my ports:
How to find which application is using your TCP ports
I inspected the ports using:
netstat -an | findstr 5432
which showed me that even when the DB container is not running and even Docker itself is closed, something is listening on 5432.
I used:
netstat -aon | findstr 5432
I found out that a PID:4846 is running on 5432. Going into TaskManager>Details, finding PID 4846 led me to find out that "Windows IP Helper service" is listening on this port. A quick search led me to this answer:
Windows IP Helper Service (iphlpsvc) - is it possible to change port?
And I also remembered that for a previous project on an older version of docker in WSL2 which had a lot of problems forwarding ports, I had used port forwarding on this port. So, a quick:
netsh interface portproxy show all
netsh interface portproxy delete v4tov4 listenport=5432 listenaddress=0.0.0.0
netsh interface portproxy delete v4tov4 listenport=5432 listenaddress=127.0.0.1
solved the issue.
Now, I should again stress, that for some reason, pgAdmin didn't ahve any problem with the port forwarding and could connect to the DB with no problem, which led me to believe the port and connection should be fine. Hope this helps someone in the future.

Nestjs with postgres and redis on docker connection refused

I've dockerize nestjs app with postgres and redis.
How can I fix this issue?
Postgres and redis are refusing tcp connections.
This is docker-compose.yml and console result.
I am using typeorm and #nestjs/bull for redis.
Hope much help.
Thanks
When using docker-compose, the containers do not necessarily need to specify the network they are in since they can reach each other from the container name (e.g. postgres, redis in your case) because they are in the same network. See Networking in Compose for more info.
Also, expose doesn't do any operation and it is redundant. Since you specify ports, it should be more than enough to tell Docker which ports of the container are exposed and bound to which ports of the host.
For redis-alpine, the startup command is redis-server and it is not necessary to specify it again. Also, the environment you specified is redundant in this case.
Try the following docker-compose.yaml with all of the above suggestions added:
version: "3"
services:
postgres:
image: postgres:alpine
restart: always
ports:
- 5432:5432
volumes:
- ./db_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
- POSTGRES_DB=lighthouse
redis:
image: redis:alpine
restart: always
ports:
- 6379:6379
Hope this helps. Cheers! 🍻

How to run a docker mongo?

I have a local mongod running where I developed my app.
Now i created a Mongo Docker Image and run the container.
I use docker-compose:
version: '3.1'
services:
mongo:
image: mongo:latest
restart: always
ports:
- "27017:27017"
Somehow my local mongo instance is always being used (probably because both are being accessed by localhost:27017). Only when i go to windows task manager and really kill the mongod instance, the docker mongo is used. What do i need to change to have both running?
Thanks!
Change the port 27017 to another port so that you can access the second instance on that port:
ports:
- "27018:27017"
This exposes the docker internal port 27017 to your machine's 27018 port.
So your first local mongo instance will run on 27017 and this docker instance will run on 27018 (This can be any free port). Make sure no other local service is using this port.

docker link resolves to localhost

I'm stuck on a very strange docker problem that I've not encountered before. What I want to do is to use docker-compose to make my application available from the internet. It's currently running on a instance on DigitalOcean and I'm currently working with the following docker-compose.yml:
version: '2.2'
services:
mongodb:
image: mongo:3.4
volumes:
- ./mongo:/data/db
ports:
- "27017"
mongoadmin: # web UI for mongo
image: mongo-express
ports:
- "8081:8081"
links:
- "mongodb:mongo"
environment:
- ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
- ME_CONFIG_BASICAUTH_USERNAME=user
- ME_CONFIG_BASICAUTH_PASSWORD=pass
app:
image: project/name:0.0.1
volumes:
- ./project:/usr/src/app
working_dir: /usr/src/app
links:
- "mongodb:mongodb"
environment:
- NODE_ENV=production
command: ["npm", "start"]
ports:
- "3000:3000"
Mongoadmin connects properly and is able to connect to the database, while the database itself cannot be connected to from outside the host.
The problem is that the app won't connect to the right address. It is a express server using mongoose to connect to the database. Before connecting I'm logging the url it will connect to. In my config.js I've listed mongodb://mongodb/project, but this is resolved to localhost thus resulting in MongoError: failed to connect to server [localhost:27017] on first connect. The name of the container is resolved, but not to the proper address.
I've tried to connect to the IP (in the 172.18.0.0 range) that docker addressed to the container, but that also resolved to localhost. I've looked into /etc/hosts but this does not show anything related to this. Furthermore, I'm baffled because the mongo-express container is able to connect.
I've tried changing the name of the container, thinking it might be block for some reason due to previous runs or something like that, but this did not resolve the issue
I've tried both explicit links and implicit using dockers internal DNS resolve, but both did not work.
When binding port 27017 to localhost it is able to connect, but because of security and easy configuration via environment variables, I rather have the mongodb instance not bound to localhost.
I've also tried to run this on my local machine and that works as expected, being that both mongoadmin and app are able to connect to the mongodb container. My localmachine runs Docker version 1.12.6, build 78d1802, while the VPS runs on Docker version 17.06.2-ce, build cec0b72, thus a newer version.
Could this be a newly introduced bug? Or am I missing something else? Any help would be appreciated.
Your docker-compose file seems not have linked the app and mongodb container.
You have this:
app:
image: project/name:0.0.1
volumes:
- ./project:/usr/src/app
working_dir: /usr/src/app
environment:
- NODE_ENV=production
command: ["npm", "start"]
ports:
- "3000:3000"
While I think it should be this:
app:
image: project/name:0.0.1
volumes:
- ./project:/usr/src/app
working_dir: /usr/src/app
links:
- "mongodb:mongodb"
environment:
- NODE_ENV=production
command: ["npm", "start"]
ports:
- "3000:3000"

docker run mongo image on a different port

The short question is can I run mongo from mongo:latest image on a different port than 27017 (for example on 27018?)
If yes, how can I do this inside a docker-compose.yml file in order ro be able to type the following command:
docker-compose run
The longer story:
I have an app running in AWS EC2 instance. The app consists of a mongodb and a web application. Now I decided to separate part of this app into its own microservice running in the same AWS inside docker container (two containers one for another mongo and one for a web app). I think the problem is I can not have mongodb running on port 27017 and at the same time another mongodb running inside a docker container on port 27017. Right? I have this assumption because when I stop the first mongo (my app mongo), my docker mongo works.
So I am trying to make the second mongo (the one that is inside the docker container), to run in a different port and my second web app (the one inside another docker conianter), to listen to mongo on a different port. Here is my attempt to change the docker-compose file:
version: '2'
services:
webapp:
image: myimage
ports:
- 3000:3000
mongo:
image: mongo:latest
ports:
- 27018:27018
And inside my new app, I changed the mongo url to:
monog_url = 'mongodb://mongo:27018'
client = MongoClient(monog_url, 27018)
Well, the same if I say:
monog_url = 'mongodb://mongo:27018'
client = MongoClient(monog_url)
But when I run docker-compose run, it still does not work, and I get the following errors:
ERROR: for mongo driver failed programming external
connectivity on endpoint: Error starting userland proxy:
listen tcp 0.0.0.0:27017: bind: address already in use
Or
pymongo.errors.ServerSelectionTimeoutError:
mongo:27018: [Errno -2] Name or service not known
You can tell MongoDB to listen on a different port in the configuration file, or by using a command-line parameter:
services:
mongo:
image: 'mongo:latest'
command: mongod --port 27018
ports:
- '27018:27018'
You can run processes inside a container and outside on the same port. You can even run multiple containers using the same port internally. What you can't do is map the one port from the host to a container. Or in your case, map a port that is already in use to a container.
For example, this would work on your host:
services:
webapp:
image: myimage
ports:
- '3000:3000'
mongo:
image: 'mongo:latest'
ports:
- '27018:27017'
mongo2:
image: mongo:latest
ports:
- '27019:27017'
The host mongo listens on 27017. The host also maps ports 27018 and 27019 to the container mongo instances, both listening on 27017 inside the container.
Each containers has its own network namespace and has no concept of what is running in another container or on the host.
Networks
The webapp needs to be able to connect to the mongo containers internal port. You can do this over a container network which allows connections between the container and also name resolution for each service
services:
webapp:
image: myimage
ports:
- '3000:3000'
networks:
- myapp
depends_on:
- mongo
mongo:
image: 'mongo:latest'
ports:
- '27018:27017'
networks:
- myapp
networks:
myapp:
driver: bridge
From your app the url mongo://mongo:27017 will then work.
From your host need to use the mapped port and an address on the host, which is normally localhost: mongo://localhost:27018
Default communication between different containers running on the same host
I solved the problem, not by running the container in a different port though, but by learning one new feature in docker-compose version 2 and that is we do not need to specify links or networks. The newly created containers by default will be part of docker0 network and hence they can communicate with each other.
As Matt mentioned, we can run processes inside containers on the same port. They should be isolated. So the problem can not be that the docker container and the host are using the same port. The problem is perhaps there is an attempt to forward a used port in host to another port in container.
Below is a working docker-compose file:
version: '2'
services:
webapp:
image: myimage
ports:
- 3000:3000
mongo:
image: mongo:latest
I looked at the mongo:latest docker file in github, and realized they exposed 27017. So we do not need to change the port or forward host ports to the running mongo container. And the mongo url can stay on the same port:
monog_url = 'mongodb://mongo:27017'
client = MongoClient(monog_url, 27017)
Docker run an image on a different port
So the above solution solved the problem, but as for the question title 'docker run mongo image on a different port', the simplest way is just to change the docker-compose to:
version: '2'
services:
web:
image: myimage
mongo:
image: mongo:latest
command: mongod --port 27018
Mongo is now running on 27018 and the following url is still accessible inside the web:
monog_url = 'mongodb://mongo:27018'
client = MongoClient(monog_url, 27018)
I had the same problem
I changed ports in my docker-compose.yml file and this work for me
In doing so, i don't change port in my connection string
mongo:
image: mongo
ports:
- "27018:27018"