Flo
Flo

Reputation: 3067

Gitlab CI Service Access

I want to test my nodejs app with mongo db access in gitlab ci. therefore I setup a mongo service and try to connect to it. Docs says, it should be reachable by hostname. but it seems, that my app doesn't find the mongo service. Is there a possibillity to explicit map ports? As the Dockerimage exposes 27017, but I don't know what gitlab does under the hood.

.gitlab-ci.yml

test:
  stage: test
  image: docker:latest
  services:
    - docker:dind
    - mongo:latest
  variables:
    MONGO_INITDB_ROOT_USERNAME: test
    MONGO_INITDB_ROOT_PASSWORD: test
  before_script:
    - docker build -t app -f Dockerfile.test .
  script:
    - docker run app  npm run test

Error

MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongo mongo:27017]
    at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Upvotes: 3

Views: 671

Answers (1)

somar
somar

Reputation: 36

You can configure an alias for the hostname of your mongo service:

services:
  - name: docker:dind
  - name: mongo:latest
    alias: mongo

Otherwise you can forward your mongo port:

  before_script:
    - apt install -y socat 
    - socat tcp-listen:27017,fork tcp:mongo:27017 &
    - docker build -t app -f Dockerfile.test .

Upvotes: 1

Related Questions