I have the following docker image built by the next docker-compose
services:
give_sync_test:
image: mongo:4.2.10
restart: always
environment:
MONGO_INITDB_DATABASE: mybase
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: password
ports:
- 27017:27017
which i am using through the following gitlab step :
test:
stage: test
dependencies:
- maven_build_snapshot
image: 'my_image:latest'
before_script:
- apt-get update
- apt-get -y install git
- mongo "mongodb://localhost:27017/mybase" < ./utils/test
the content of utils/test is simply :
use myBase;
no action in this, the real problem is that i'm getting the following error from gitlab-ci :
connecting to:
mongodb://localhost:27017/give?compressors=disabled&gssapiServiceName=mongodb
2020-12-09T14:44:21.828+0000 E QUERY [js] Error: couldn't connect
to server localhost:27017, connection attempt failed: SocketException:
Error connecting to localhost:27017 (127.0.0.1:27017) :: caused by ::
Connection refused : connect#src/mongo/shell/mongo.js:353:17
#(connect):2:6 2020-12-09T14:44:21.830+0000 F - [main]
exception: connect failed 2020-12-09T14:44:21.830+0000 E -
[main] exiting with code 1
I've already try the mongo string with some authentication as well
mongodb://admin:password#localhost:27017/mybase
Does anyone know what i'm doing wrong ?
You should use alias of the services to connect to the containers. In this case, you can access mongo by using this connection string mongodb://admin:password#give_sync_test:27017/mybase
Related
I used to run mongodb in docker with this config
~/project/.env file is
# Mongodb common config
CONFIG_MONGODB_PORT=27020
CONFIG_MONGODB_PREFIX=mongodb://
CONFIG_MONGODB_AUTH_ENABLED=true
CONFIG_MONGODB_USERNAME=alok
CONFIG_MONGODB_PASSWORD=alokm
CONFIG_MONGODB_AUTH_SOURCE=admin
~/project/docker-compose.yml file is
version: '3.4'
mongodb:
image: mongo:4.0.3
restart: unless-stopped
command: mongod --port ${CONFIG_MONGODB_PORT}
volumes:
- ${HOST_MONGODB_DATA}:/data/db
ports:
- 5010:${CONFIG_MONGODB_PORT}
expose:
- ${CONFIG_MONGODB_PORT}
environment:
MONGO_INITDB_ROOT_USERNAME: ${CONFIG_MONGODB_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${CONFIG_MONGODB_PASSWORD}
To run mongodb
sudo docker-compose up --build
To access mongodb from my laptop
mongo localhost:5010 --password alokm --username alok --authenticationDatabase admin
This all setup used to work fine without any extra setup.
But when I changed mongodb image from mongo:4.0.3 to mongo:4.4.4 and run freshly on another laptop I see
ubuntu#myhost:~/project$ mongo localhost:5010 --password alokm --username alok --authenticationDatabase admin
MongoDB shell version v3.6.8
connecting to: mongodb://localhost:5010/test
Implicit session: session { "id" : UUID("4282986e-eb0b-4a88-8d39-1295192a66e7") }
MongoDB server version: 4.4.4
WARNING: shell and server versions do not match
2021-04-16T01:41:18.225+0000 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1608:20
#(auth):6:1
#(auth):1:2
exception: login failed
Why my this setup is not working in case of image: mongo:4.4.4 like it was working in case of image: mongo:4.0.3?
I am trying to create test container with DockerComposeContainer.
DockerComposeContainer container =
new DockerComposeContainer(
new File("src/integrationTest/resources/mycompose-file.yml"))
.withLocalCompose(true)
.withExposedService("mongodb", 27017, Wait.forListeningPort());
container.start();
return container;
mycopose-file.yml has following:
version: '3'
services:
mongodb:
image: mongo:3.6
environment:
MONGO_INITDB_ROOT_USERNAME: uname
MONGO_INITDB_ROOT_PASSWORD: upass
My test dependency include: testcontainers-1.12.3.jar
I am getting error during application context startup:
java.lang.IllegalStateException: Failed to load ApplicationContext
with root cause:
Caused by: org.testcontainers.containers.ContainerLaunchException: Local Docker Compose exited abnormally with code 1 whilst running command: up -d
at org.testcontainers.containers.LocalDockerCompose.invoke(DockerComposeContainer.java:711)
....
In my case I got this error because network in docker had exhausted. Given docker has limited network that we can have. We have to do the cleanup, for that I ran command:
docker network prune
this fixed my problem
My docker container needs to connect to other system via http. so , I have export an REST_BASE_URL in environment variable and pass it on docker run using -e parameter . Unfortunately, I am ending up with below error . Can anybody help me out ?
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"-eREST_BASE_URL=https://some.server.url\": stat -eREST_BASE_URL=https://some.server.url: no such file or directory": unknown.
Solved it using docker-compose.
Below is the sample :
version: '3.1'
services:
receiving:
build: .
restart: always
environment:
- "REST_BASE_URL=http://some.server.url"
ports:
- 8080:8080
I'm trying to execute command after docker container start.
Here is my Dockerfile
FROM mysite:myport/mongo:3.6
EXPOSE 27017
ADD mongo.js /data/mongodb/scripts/
here is my docker-compose.yml
mongo:
image: test-mongo
container_name: test-mongo
hostname: mongo
expose:
- "27017"
ports:
- "27017:27017"
command: mongo --eval load("/data/mongodb/scripts/mongo.js")
docker-compose --no-ansi up returns me
Creating test-mongo ...
Creating test-mongo ... done
Attaching to test-mongo
test-mongo | MongoDB shell version v3.6.5
test-mongo | connecting to: mongodb://127.0.0.1:27017
test-mongo | 2018-06-09T15:46:50.229+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), rea
son: Connection refused
test-mongo | 2018-06-09T15:46:50.229+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
test-mongo | connect#src/mongo/shell/mongo.js:251:13
test-mongo | #(connect):1:6
test-mongo | exception: connect failed
test-mongo exited with code 1
I don't uderstand how can I load my file after mongodb is started
By passing a command in your docker-compose.yaml, you are overriding the normal behavior of the mongo image -- so the mongo server is never starting. The way you would typically handle this is to put your script into a second image, and then start a second container as part of your docker-compose.yaml that makes a network connection to the mongo server. Something like:
version: "3"
services:
mongo:
image: mongo
client:
image: my-mongo-client
restart: on-failure
command: mongo --host mongo --eval load("/data/mongodb/scripts/mongo.js")
The restart directive will cause the client container to restart
automatically on failure, which is necessary because it may try to
connect before the mongo server is available to handle connections.
If all you're trying to do is initialize a new database, such as creating a new schema and adding a user, you can create an initialization script and load it into the container. From the Docker Hub documentation:
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.
I am using docker-compose to launch an official mongodb database container.
Reading the scripts and some StackOverflow questions looks like MONGO_INITDB_ environment variables can be used to set default credentials.
After running the following docker-compose.yml:
version: '2'
services:
mongodb:
image: mongo:3.6
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=collection
ports:
- 27017:27017
And trying to connect with mongo cli or mongoose Node.js ODM I get authentication error:
➔ mongo -u user -p password --verbose localhost:27017/collection
MongoDB shell version: 2.6.10
connecting to: localhost:27017/collection
2018-04-10T12:29:46.386+0200 creating new connection to:localhost:27017
2018-04-10T12:29:46.386+0200 [ConnectBG] BackgroundJob starting: ConnectBG
2018-04-10T12:29:46.387+0200 connected to server localhost:27017 (127.0.0.1)
2018-04-10T12:29:46.387+0200 connected connection!
2018-04-10T12:29:46.391+0200 User Assertion: 18:{ ok: 0.0, errmsg: "auth failed", code: 18, codeName: "AuthenticationFailed" }
2018-04-10T12:29:46.392+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18, codeName: "AuthenticationFailed" } at src/mongo/shell/db.js:1287
2018-04-10T12:29:46.392+0200 User Assertion: 12514:login failed
2018-04-10T12:29:46.393+0200 freeing 1 uncollected N5mongo12DBClientBaseE objects
exception: login failed
Connecting to /admin collection as defined in the entrypoint script [1] gave the same result. I am unable to see what am I doing wrong.
EDIT: Derick, from irc.freenode.org #mongodb channel, suggested that it may be not picking the --auth flag. I tried to log in without credentials (mongo --verbose localhost:27017/admin) and it worked. I am still having the issue of establishing defaults credentials, so issue is not yet solved.
[1] https://github.com/docker-library/mongo/blob/b96fddd1e1a100c01f0ea6d28e1c7ccc750fd5c0/3.6/docker-entrypoint.sh#L292