1

I want to connect my golang code (without a container) to the monogdb container, when it is local it works.

when I push it to gitlab.ci using container, connection refused

previously I used to use testing in dockerfile, but I don't use that.

the code is like this

image: docker:latest
services:
  - docker:dind

stages:
  - test

variables:
  REPO_NAME: $REPO_URL
  DOCKER_HOST: tcp://docker:2375
  DOCKER_DRIVER: overlay2


test:
  stage: test
  before_script:
    - apk add go make bash docker-compose
    # - make service-up-test
  script:
    - make mongodb-test-up
    - go clean -testcache && go test -v ./app/test

and golang test :

package codetify

import (
    "context"
    "log"
    "testing"

    "github.com/stretchr/testify/assert"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/mongo/readpref"
)

var credential = options.Credential{
    Username: "usernametest",
    Password: "passwordtest",
}
var addr = "mongodb://0.0.0.0:27018"

func InitMongoDB() *mongo.Database {
    clientOpts := options.Client().ApplyURI(addr).SetAuth(credential)
    clientOpts.SetDirect(true)
    client, err := mongo.Connect(context.TODO(), clientOpts)
    if err != nil {
        log.Println("client", client)
        return nil
    }
    return client.Database("databasetest")
}

func TestPingMongoDBServer(t *testing.T) {
    clientOpts := options.Client().ApplyURI(addr).SetAuth(credential)
    clientOpts.SetDirect(true)
    client, err := mongo.Connect(context.TODO(), clientOpts)
    assert.Equal(t, err, nil, "Shoudl be not error")
    err = client.Ping(context.Background(), readpref.Primary())
    assert.Equal(t, err, nil, "Shoudl be not error")
}
  • Hello Fajrul, could you share some more details like where mongodb is running and the full error message? The connection error is happening in that test code you shared right? – William Hammond Oct 25 '20 at 12:30
  • 1
    this is reality: `*errors.errorString(&errors.errorString{s:"server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: 0.0.0.0:27018, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : dial tcp 0.0.0.0:27018: connect: connection refused }, ] }"})`, not expected, but I think, this problem not from golang or docker, this is problem in gitlab.ci, I want to connect to mongoDB container from my code in giltlab.ci runner – Fajrul Aulia Oct 25 '20 at 14:28
  • I think https://stackoverflow.com/questions/45316098/connecting-to-docker-in-docker-from-a-gitlab-ci-runner might be of help – William Hammond Oct 25 '20 at 14:45

1 Answers1

0

[Next]

this is a docker-compose.yml for mongodb, for testing, i use another port

version: '3'
services: 
    database:
        image:  'mongo:latest'
        container_name: '${APP_NAME}-mongodb-test'
        environment: 
            MONGO_INITDB_ROOT_USERNAME: usernametest
            MONGO_INITDB_ROOT_PASSWORD: passwordtest
            MONGO_INITDB_DATABASE: databasetest
        command: mongod
        ports:
            - '27018:27017'
        restart: always
        volumes: 
            - ./resources/mongo-initdb.js:/docker-entrypoint-initdb.d/mongo-initdb.js
        networks: 
            - codetify-net-test
networks: 
    codetify-net-test