Cannot connect from host to mongo using docker-compose - mongodb

I'm running my mongo db with docker-compose
version: '3'
services:
db:
image: mongo:4
ports:
- "27017:27017"
volumes:
- ./storage/mongo:/data/db:rw
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin123
command: --smallfiles --bind_ip_all --port 27017
When I do
$> docker-compose exec db bash
I can simply connect to my db
$> mongo -u admin -p admin123
However, when I do the same thing from my host system I cannot connect
(master) ✗ mongo -u admin -p admin123
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("8689631e-ac9c-40f8-aa2a-e55f6103224f") }
MongoDB server version: 4.0.9
2019-05-11T17:44:19.181+0200 E QUERY [js] Error: Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1685:20
#(auth):6:1
#(auth):1:2
exception: login failed
I also tried with --host 0.0.0.0 but same result. Below is the listing of ps
$> docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------------------------------
db_1 docker-entrypoint.sh --sma ... Up 0.0.0.0:27017->27017/tcp
I get the feeling that mongo doesn't accept remote connections, which is why I added command: --smallfiles --bind_ip_all --port 27017 but it didn't help. Any suggestion what I'm doing wrong?

Related

how to connect one of my mongo container to another?

I want to use Typesense with Mongodb. To do so I first need to enable replicaset.
From what I read here and there, to achieve it with docker compose, it must be done in two steps, like this :
version: '3.8'
services:
mongodb:
image: mongo
container_name: 'mongodb'
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_INITDB_DATABASE: testDb
volumes:
- ./mongodb-config/mongod.test.conf:/etc/mongod.conf:ro
- ./mongodb-config/keyfile:/test/keyfile
ports:
- '27017:27017'
expose:
- '27017'
entrypoint:
- bash
- -c
- |
chmod 400 /test/keyfile
chown 999:999 /test/keyfile
exec docker-entrypoint.sh $$#
command: mongod --replSet testRs --keyFile /test/keyfile --bind_ip localhost;
mongosetup:
image: mongo
depends_on:
- mongodb
restart: 'no'
entrypoint:
[
'bash',
'-c',
"sleep 10 && mongo admin --host mongodb:27017 -u root -p example --verbose --eval 'rs.initiate()'",
]
log from mongosetup :
MongoDB shell version v5.0.9
connecting to: mongodb://mongodb:27017/admin?compressors=disabled&gssapiServiceName=mongodb
{"t":{"$date":"2022-07-20T14:58:50.437Z"},"s":"D1", "c":"NETWORK", "id":20109, "ctx":"js","msg":"Creating new connection","attr":{"hostAndPort":"mongodb:27017"}}
{"t":{"$date":"2022-07-20T14:58:50.440Z"},"s":"D1", "c":"-", "id":23074, "ctx":"js","msg":"User assertion","attr":{"error":"InternalError: couldn't connect to server mongodb:27017, connection attempt failed: SocketException: Error connecting to mongodb:27017 (192.168.0.2:27017) :: caused by :: Connection refused","file":"src/mongo/scripting/mozjs/mongo.cpp","line":745}}
Error: couldn't connect to server mongodb:27017, connection attempt failed: SocketException: Error connecting to mongodb:27017 (192.168.0.2:27017) :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:372:17
#(connect):2:6
{"t":{"$date":"2022-07-20T14:58:50.441Z"},"s":"D1", "c":"-", "id":23074, "ctx":"js","msg":"User assertion","attr":{"error":"Location12513: connect failed","file":"src/mongo/shell/shell_utils.cpp","line":537}}
{"t":{"$date":"2022-07-20T14:58:50.441Z"},"s":"I", "c":"QUERY", "id":22787, "ctx":"js","msg":"MozJS GC heap stats","attr":{"phase":"prologue","total":4799817,"limit":0}}
{"t":{"$date":"2022-07-20T14:58:50.444Z"},"s":"I", "c":"QUERY", "id":22787, "ctx":"js","msg":"MozJS GC heap stats","attr":{"phase":"epilogue","total":153,"limit":0}}
{"t":{"$date":"2022-07-20T14:58:50.444Z"},"s":"D1", "c":"-", "id":23074, "ctx":"main","msg":"User assertion","attr":{"error":"Location12513: connect failed","file":"src/mongo/scripting/mozjs/proxyscope.cpp","line":310}}
exception: connect failed
exiting with code 1
There must be something wrong with my way to try to connect, because I can do that manually without any problem :
docker exec -it 0188651cfb4b5789e832226094dae48fb8efd92fc318ce7b0286312f9a3d81de bash
root#0188651cfb4b:/# mongosh -u root -p example
test> rs.initiate()
{
info2: 'no configuration specified. Using a default configuration for the set',
me: 'localhost:27017',
ok: 1
}
testRs [direct: other] test>
I must be missing something, and since I'm a beginner with docker & mongo, I'm stuck :(
Thanks for your help
--bind_ip localhost in the command of mongodb container tells mongo to listen on local interface only. Try to connect to it from mongosetup manually and you will face the same error.
Use --bind_ip_all option instead.

MONGO_INITDB_ROOT_USERNAME is not effective in docker-compose file

I'm running an app with mongo db with docker.
Here is the mongodb part of the docker-compose file.
mongodb:
restart: always
image: mongo:3.6.3
container_name: "mongodb"
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/nul
- MONGO_INITDB_ROOT_USERNAME=MasterUser
- MONGO_INITDB_ROOT_PASSWORD=MasterPassword
ports:
- 27017:27017
when i start the apps with docker-compose i see that mongodb is not detecting the root username and password therefore i can't login to mongodb and create user for my applications.
Strange thing is that, when i create a compose file only for mongodb it runs and uses the credentials i gave (MasterUser/MasterPassword)
i can also see that master user password and username are set in ENV in the container
docker exec -it mongodb env | grep -i master
MONGO_INITDB_ROOT_USERNAME=MasterUser
MONGO_INITDB_ROOT_PASSWORD=MasterPassword
however they dont work to login to db
docker exec -it mongodb mongo -u MasterUser -p MasterPassword --authenticationDatabase admin
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
2020-07-18T19:27:22.378+0000 E QUERY [thread1] Error: Authentication failed. :
#(auth):6:1
#(auth):1:2
exception: login failed

GitLab CI: docker container cannot connect to MongoDB service

In a GitLab CI pipeline I need to connect my own C++ code from a docker container to a MongoDB running as servive but I cannot connect.
This is a minimal gitlab-ci.yml example showing the problem:
stages:
- connect1
- connect2
image: docker:latest
variables:
MONGOIMAGE: "mongo:4.2.3-bionic"
MONGO_INITDB_ROOT_USERNAME: "root"
MONGO_INITDB_ROOT_PASSWORD: "geheim"
connect1:
stage: connect1
services:
- name: $MONGOIMAGE
image: mongo
script:
- mongo --host mongo --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --eval "db.help()"
connect2:
stage: connect2
services:
- name: $MONGOIMAGE
script:
- docker run --rm mongo mongo --host mongo --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --eval "db.help()"
The error message of connect2 is that the host cannot be found:
connecting to: mongodb://mongo:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-06-04T16:20:47.614+0000 E QUERY [js] Error: couldn't connect to server mongo:27017, connection attempt failed: HostNotFound: Could not find address for mongo:27017: SocketException: Host not found (authoritative) :
connect#src/mongo/shell/mongo.js:341:17
#(connect):2:6
2020-06-04T16:20:47.615+0000 F - [main] exception: connect failed
2020-06-04T16:20:47.615+0000 E - [main] exiting with code 1
I already tried with --host localhost but this is working neighter. How can I achive that a container started with docker run like in connect2 can connect to the MongoDB service?
Finally I found a solution:
connect2:
stage: connect2
services:
- name: $MONGOIMAGE
script:
- ping mongo -c 5
- docker run --rm mongo mongo --add-host mongo:`cat /etc/hosts | grep mongo | awk '{print $1}'` --net host --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --eval "db.help()"
Docker is client/server application. You're running the client, but there is no server. So you need to run a Docker daemon using Docker-in-Docker.
You can see an example GitLab CI config here: https://stackoverflow.com/a/61106578/6214034
And the relevant documentation: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

Connecting to mongo from a different container - connection refused

I'm trying to create a docker file that mongo running in a different container, to execute commands from a file, however the second container receives a connection refused error:
mongo-testdata_1 | MongoDB shell version: 3.2.9
mongo-testdata_1 | connecting to: mongo:27017/admin
mongo-testdata_1 | 2019-12-15T15:57:30.067+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
mongo-testdata_1 | 2019-12-15T15:57:30.067+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
mongo-testdata_1 | #./testData.js:1:20
mongo-testdata_1 |
mongo-testdata_1 | failed to load: ./testData.js
How would I be able to connect to the mongoDB running on the first container, using the mongo cli in the second?
commands used
docker-compose up -d mongo
docker exec my-test-mongo mongo --eval "db.createUser({ user: 'admin', pwd: 'password', roles: [ 'dbAdmin', 'readWrite' ] });" admin
docker-compose up --build mongo-testdata
mongo.dockerfile
FROM mongo:3.2.9
ADD testData.js .
ENTRYPOINT ["mongo"]
docker-compose.yml
version: '2'
services:
mongo-testdata:
build:
context: .
dockerfile: mongo.dockerfile
command: --host mongo --port 27017 --username='admin' --password='password' admin ./testData.js
depends_on:
- mongo
networks:
- backend
mongo:
image: mongo:3.2.9
container_name: my-test-mongo
ports:
- 27017:27017
networks:
- backend
networks:
backend:
driver: bridge
testData.js
const myDb = new Mongo().getDB('mydb');
myDb.getCollection('test').insert({
foo: 'bar',
});
You need to specify host and port when you connect to mongo in testData.js.
new Mongo(<host:port>)
In your case this would be new Mongo('mongo').
https://docs.mongodb.com/v3.2/reference/method/Mongo/#Mongo
Instantiation Options
Use the constructor without a parameter to instantiate a connection to the localhost interface on the default port.
Pass the parameter to the constructor to instantiate a connection to the and the default port.
Pass the <:port> parameter to the constructor to instantiate a connection to the and the .
The only depends_on will not work, as the mongo DB container process is not ready to accept to container while CLI get connection refused. You can try two option
- simply put sleep for some second the DB container will ready to accept connection
- Increase wait timeout in mongo shell connection
Here is way to deal with connection refused by adding 10 second seelp in import.
version: '2'
services:
mongo_import:
image: mongo
command: /bin/bash -c "sleep 10 && mongo --host mongo --port 27017 --username='admin' --password='password' admin /root/testData.js"
volumes:
- ./testData.js:/root/testData.js
depends_on:
- mongo
mongo:
image: mongo
container_name: my-test-mongo
ports:
- 27017:27017
Btw the above task you can perform in the same container without the need of any cli container.
Initializing a fresh instance
When a container is started for the first time it will execute files
with extensions .sh and .js that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order. .js files will be executed by mongo using the database
specified by the MONGO_INITDB_DATABASE variable, if it is present, or
test otherwise. You may also switch databases within the .js script.
Initializing a fresh instance

MongoError: connect ECONNREFUSED in Docker

I have my docker-compose orchestration and I'm getting this error:
connection error: { MongoError: failed to connect to server [172.17.0.2:27018] on first connect [MongoError: connect ECONNREFUSED 172.17.0.2:27018]
This is the code in server.js:
mongoose.connect('mongodb://mongodb:27018');
This happen only when I customize the command of my docker container:
docker-compose.yml:
version: "3"
services:
app:
build: ./my-node-app
depends_on:
- mongodb
mongodb:
image: mongo:3.5
command: mongod --port 27018
If I remove the --port 27018 and point to the default 27017 the error gets fixed.
What could be happening?
You're missing: --bind_ip_all
It seems that newer versions (>3.5) of the mongod daemon listen only to localhost by default.
Your are overriding this:
CMD ["mongod", "--bind_ip_all"]
So, also put "--bind_ip_all" in your docker-compose.yml:
mongodb:
image: mongo:3.5
command: mongod --port 27018 --bind_ip_all