Docker Image build through CI unable to connect to mongodb defined as service
Summary
Docker image build through CI cannot contact mongodb even tough I am able to ping the servicename
Steps to reproduce
Create a NodeJS application docker image that calls a mongodb connection ("mongodb://mongo/spa-wandeling";). Build and run the image with CI
Expected behavior
Application connects to the mongodb instance and runs
Actual behavior
Application dies because it fails the connect.
Relevant logs and/or screenshots
.gitlab-ci.yaml file
image: docker:latest
services:
- mongo
before_script:
- docker info
build:
stage: build
script:
- docker build -t wandeling .
- ping mongo -c 50
- docker run -e "configfile=configuration-staging" wandeling
Output of the Ci-build
Running with gitlab-ci-multi-runner 1.9.2 (ade6572)
Using Docker executor with image docker:latest ...
Pulling docker image mongo:latest ...
Starting service mongo:latest ...
Waiting for services to be up and running...
Pulling docker image docker:latest ...
Running on runner-78157924-project-9-concurrent-0 via 7dab12ad32e3...
Fetching changes...
HEAD is now at 46599f6 Ping Mongo before starting the container
From http://gitlab.****.com/****/wandeling-evaluatie
46599f6..95bb049 master -> origin/master
Checking out 95bb049c as master...
$ docker info
$ docker build -t wandeling .
Sending build context to Docker daemon 469 kB
Step 1 : FROM node:4.1.2-onbuild
Step 7 : ENTRYPOINT npm start
Successfully built b0be36433a53
$ ping mongo -c 50
PING mongo (172.17.0.4): 56 data bytes
64 bytes from 172.17.0.4: seq=0 ttl=64 time=0.439 ms
64 bytes from 172.17.0.4: seq=1 ttl=64 time=0.149 ms
...
64 bytes from 172.17.0.4: seq=48 ttl=64 time=0.267 ms
64 bytes from 172.17.0.4: seq=49 ttl=64 time=0.133 ms
--- mongo ping statistics ---
50 packets transmitted, 50 packets received, 0% packet loss
round-trip min/avg/max = 0.110/0.208/0.607 ms
$ docker run -e "configfile=configuration-staging" wandeling
npm info it worked if it ends with ok
npm info using npm@2.14.4
npm info using node@v4.1.2
npm info prestart WandelingEvaluatie@0.0.1
npm info start WandelingEvaluatie@0.0.1
> WandelingEvaluatie@0.0.1 start /usr/src/app
> node ./bin/www
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
error during startup:TypeError: path must be a string
Falling back to http
/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/server.js:265
process.nextTick(function() { throw err; })
^
MongoError: failed to connect to server [mongo:27017] on first connect
at null.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:325:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:173:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at connectErrorNT (net.js:993:8)
at doNTCallback2 (node.js:430:9)
at process._tickCallback (node.js:344:17)
npm info WandelingEvaluatie@0.0.1 Failed to exec start script
npm ERR! Linux 3.16.0-4-amd64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
ERROR: Build failed: exit code 1
I can exec into the container and verify that mongo is running by running the following commands in the the container
me@dockerhost:~$ docker exec -it e0d2604c9f9d /bin/bash
root@e0d2604c9f9d:/# mongo
MongoDB shell version v3.4.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.1
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-01-11T14:45:30.544+0000 I STORAGE [initandlisten]
2017-01-11T14:45:30.544+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-01-11T14:45:30.544+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-01-11T14:45:30.710+0000 I CONTROL [initandlisten]
2017-01-11T14:45:30.710+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-01-11T14:45:30.710+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-01-11T14:45:30.710+0000 I CONTROL [initandlisten]
2017-01-11T14:45:30.711+0000 I CONTROL [initandlisten]
2017-01-11T14:45:30.711+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-01-11T14:45:30.711+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-01-11T14:45:30.712+0000 I CONTROL [initandlisten]
> show dbs
admin 0.000GB
local 0.000GB
> use spa-wandeling
switched to db spa-wandeling
What am I missing here to get my connection up to mongo?
I tried connecting to mongodb
as described in gitlab-ce#13262 but this yields the same result
MongoError: failed to connect to server [mongo:27017] on first connect
Edited by Jason Yavorska