can't connect to mongo docker within gitlab

Viewed 287

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 ?

1 Answers

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