UPDATED with repository:
I have made a simple repository to test connecting to mongo service in GitLab CI.
The repository address: https://gitlab.com/arashchm/mongodb
The issue is that main container cannot access the container that has mongodb.
Here is the .gitlab-ci.yml
contents:
test-mongo:
services:
- name: mongo:latest
alias: mongo
variables:
MONGO_INITDB_DATABASE: custom_db
MONGO_INITDB_ROOT_USERNAME: custom_user
MONGO_INITDB_ROOT_PASSWORD: custom_pass
image: node:18.19-bullseye
script:
- npm install
- node ./test-db.js
The same setup works locally with docker-compose.yml
which contains:
services:
mongo:
image: mongo:latest
container_name: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: custom_user
MONGO_INITDB_ROOT_PASSWORD: custom_pass
MONGO_INITDB_DATABASE: custom_db
ports:
- "27017:27017"
app:
image: node:18.19-bullseye
container_name: app
volumes:
- .:/usr/src/app
working_dir: /usr/src/app
command: bash -c "npm install && node test-db.js"
depends_on:
- mongo
and this is the contents of test-db.js
file:
import {MongoClient} from 'mongodb';
const uri = 'mongodb://custom_user:custom_pass@mongo:27017/custom_db?authSource=admin';
async function run() {
const client = new MongoClient(uri);
try {
await client.connect();
console.log("Connected successfully to MongoDB");
// Your database interaction logic here
} catch (err) {
console.error("Failed to connect to MongoDB:", err);
process.exit(1);
}
}
run()
In the CI logs, I can see that the service container has started and there is a 30 sec time to make sure it's up and running:
It seems that there is a problem with connecting to the IPv6:
MongoServerSelectionError: connect ECONNREFUSED fd76:5338:4685:1:0:242:ac11:3:27017
at Topology.selectServer (/builds/arashchm/mongodb/node_modules/mongodb/lib/sdam/topology.js:303:38)
at async Topology._connect (/builds/arashchm/mongodb/node_modules/mongodb/lib/sdam/topology.js:196:28)
at async Topology.connect (/builds/arashchm/mongodb/node_modules/mongodb/lib/sdam/topology.js:158:13)
at async topologyConnect (/builds/arashchm/mongodb/node_modules/mongodb/lib/mongo_client.js:204:17)
at async MongoClient._connect (/builds/arashchm/mongodb/node_modules/mongodb/lib/mongo_client.js:217:13)
at async MongoClient.connect (/builds/arashchm/mongodb/node_modules/mongodb/lib/mongo_client.js:142:13)
at async run (file:///builds/arashchm/mongodb/test-db.js:8:9) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'mongo:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {},
[cause]: MongoNetworkError: connect ECONNREFUSED fd76:5338:4685:1:0:242:ac11:3:27017
at connectionFailureError (/builds/arashchm/mongodb/node_modules/mongodb/lib/cmap/connect.js:353:20)
at Socket.<anonymous> (/builds/arashchm/mongodb/node_modules/mongodb/lib/cmap/connect.js:268:44)
at Object.onceWrapper (node:events:632:26)
at Socket.emit (node:events:517:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
[Symbol(errorLabels)]: Set(1) { 'ResetPool' },
[cause]: Error: connect ECONNREFUSED fd76:5338:4685:1:0:242:ac11:3:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: 'fd76:5338:4685:1:0:242:ac11:3',
port: 27017
}
}
}
This line in the MongoDB docs helped:
mongod disables IPv6 support by default.
So I made this change:
test-mongo:
services:
- name: mongo:latest
alias: mongo
command:
- mongod
- --ipv6
variables:
MONGO_INITDB_DATABASE: custom_db
MONGO_INITDB_ROOT_USERNAME: custom_user
MONGO_INITDB_ROOT_PASSWORD: custom_pass
image: node:18.19-bullseye
script:
- npm install
- node ./test-db.js
Now the connection works as it should.