# Access to MongoDB service from docker container

**URL:** <https://travis-ci.community/t/access-to-mongodb-service-from-docker-container/10665>\
**Category:** Travis CI Discussions & Feedback\
**Tags:** build-env, travis-build\
**Created:** [November 23, 2020, 7:42pm UTC](https://travis-ci.community/t/access-to-mongodb-service-from-docker-container/10665 "2020-11-23T19:42:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremycod](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/jeremycod/32/5604_2.png) [@jeremycod](https://travis-ci.community/u/jeremycod)\
**Post date:** [November 23, 2020, 7:42pm UTC](https://travis-ci.community/t/access-to-mongodb-service-from-docker-container/10665/1 "2020-11-23T19:42:16Z")

</div>

I’m trying to setup mongo db service on Travis to be used for my integration tests running in the docker container. My travis configuration that initializes mongodb is:

```auto
env:
  global:
    - MONGODB_URI: mongodb://fillrx_test:test@127.0.0.1:27017/fillrx_test_db
services:
  - docker
  - mongodb

before_install:
  - sleep 15
  - mongo fillrx_test_db --eval 'db.createUser({user:"fillrx_test", pwd:"test",roles:["readWrite"]});'
  - docker build -t jeremycod/api-test -f ./server/Dockerfile.dev ./server

script:
  - docker run --env MONGODB_URI -e CI=true jeremycod/api-test npm test

after_success:
  - docker build -t jeremycod/fillrx-client ./client
  - docker build -t jeremycod/fillrx-nginx ./nginx
  - docker build -t jeremycod/fillrx-api ./server
  # Log in to the docker CLI
  - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_ID" --password-stdin

  # Take those images and push them to docker hub
  - docker push jeremycod/fillrx-client
  - docker push jeremycod/fillrx-nginx
  - docker push jeremycod/fillrx-api

deploy:
  provider: elasticbeanstalk
  region: 'ca-central-1'
  app: 'Fillrx'
  env: 'Fillrx-env'
  bucket_name: 'mys3bucketname'
  bucket_path: 'assets'
  on:
    branch: master
  access_key_id: $AWS_ACCESS_KEY
  secret_access_key: $AWS_SECRET_KEY

```

However, this fails on Travis with the connection refused error:

```auto
1) Create address endpoint /api/v1/address/user/userId

       "before all" hook: connectToTestDB for "Create address with correct input":

       MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017

       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {

            name: 'MongoNetworkError'

       }]

```

Obviously, MongoDB service is not available from the docker container on this address, but I can’t find anywhere in documentation how to correctly setup this.

Anyone can help with this?

Thanks

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [November 23, 2020, 9:24pm UTC](https://travis-ci.community/t/access-to-mongodb-service-from-docker-container/10665/3 "2020-11-23T21:24:48Z")

</div>

For starters, I don’t see you starting the MongoDB service anywhere.

Can’t say anything else for sure without seeing the entire build.

Googling “access host from docker container” finds

[https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container](https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container) (though it’s rather old and probably outdated) and

> **[Networking using the host network](https://docs.docker.com/network/network-tutorial-host/)**
>
> Tutorials for networking using the host network, disabling network isolation

---

<div class="post-metadata">

**Author:** ![jeremycod](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/jeremycod/32/5604_2.png) [@jeremycod](https://travis-ci.community/u/jeremycod)\
**Post date:** [November 24, 2020, 12:18am UTC](https://travis-ci.community/t/access-to-mongodb-service-from-docker-container/10665/4 "2020-11-24T00:18:13Z")

</div>

Thanks. Solution suggested in this article works.

```auto
script:
   - docker run --network host -e CI=true mydocker/api-test npm test

```
