# How to build new Docker image and access running dependencies?

**URL:** <https://discuss.circleci.com/t/how-to-build-new-docker-image-and-access-running-dependencies/28681>\
**Category:** Build Environment\
**Tags:** docker\
**Created:** [February 28, 2019, 10:42am UTC](https://discuss.circleci.com/t/how-to-build-new-docker-image-and-access-running-dependencies/28681 "2019-02-28T10:42:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![celsodantas](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/celsodantas/32/21548_2.png) [@celsodantas](https://discuss.circleci.com/u/celsodantas)\
**Post date:** [February 28, 2019, 10:42am UTC](https://discuss.circleci.com/t/how-to-build-new-docker-image-and-access-running-dependencies/28681/1 "2019-02-28T10:42:49Z")

</div>

I’m building a Docker image using `setup_remote_docker` but on the same run I have some dependencies that my image will need to access when I run that image.

This is what I have:

```auto
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/ruby:2.5.1-node-browsers
        environment: # environment variables for primary container
          RAILS_ENV: test
      - image: redis:5.0.3-alpine
      - image: mdillon/postgis:11

    working_directory: ~/repo

    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
          
      - run: docker build -t myapp .

      # Database setup
      - run: docker run myapp bin/rake db:setup
      - run:
          name: run tests
          command: docker run myapp rspec

```

What I’m doing is, build my image and then run DB setup and my tests. The issue I’m having is that the command `docker run myapp bin/rake db:setup` doesn’t have access to the Postgresql network so that fails with:

```auto
psql: could not connect to server: Connection refused
	Is the server running on host "0.0.0.0" and accepting
	TCP/IP connections on port 5432?

```

From what I understand, the `setup_remote_docker` causes my Docker to run in a segregated environment and because of that I can’t get it to access my other dependencies.

Thoughts about how to access them from my running Docker container?

---

<div class="post-metadata">

**Author:** ![drazisil](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/drazisil/32/24029_2.png) [@drazisil](https://discuss.circleci.com/u/drazisil)\
**Post date:** [February 28, 2019, 1:53pm UTC](https://discuss.circleci.com/t/how-to-build-new-docker-image-and-access-running-dependencies/28681/2 "2019-02-28T13:53:57Z")

</div>

I believe you will need to use the machine executor for this, instead of the Docker one. [https://circleci.com/docs/2.0/executor-types/#using-machine](https://circleci.com/docs/2.0/executor-types/#using-machine) Can you give that a try and see if it works?

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/halfer/32/9083_2.png) [@halfer](https://discuss.circleci.com/u/halfer)\
**Post date:** [February 28, 2019, 6:41pm UTC](https://discuss.circleci.com/t/how-to-build-new-docker-image-and-access-running-dependencies/28681/3 "2019-02-28T18:41:11Z")

</div>

> [@drazisil](#):
>
> I believe you will need to use the machine executor for this

I’d recommend using Docker Compose for the three images (Redis, Postgis, myapp). When this spins up, they will all appear on the same TCP network. I’ve done this with CircleCI (in a DC app with around 10 images/containers) - works a treat.

If the OP wishes to try that, I’d suggest dropping `circleci/ruby:2.5.1-node-browsers` and going to `docker/compose`, since most of the current build image features are not being used.

Machine would work, but one loses the benefits of Docker.

---

<div class="post-metadata">

**Author:** ![celsodantas](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/celsodantas/32/21548_2.png) [@celsodantas](https://discuss.circleci.com/u/celsodantas)\
**Post date:** [March 7, 2019, 10:21am UTC](https://discuss.circleci.com/t/how-to-build-new-docker-image-and-access-running-dependencies/28681/4 "2019-03-07T10:21:52Z")

</div>

Thanks for all the replies.

Using `setup_remote_docker` with Docker Compose worked. It’s just a pain to debug as I can’t execute any Docker command with I’m in a SSH session.

SSHing into the remote Docker machine doesn’t work that well (I’m using `ssh remote-docker` from the primary machine) as I don’t have access to the `docker-compose.ci.yml` file, so debugging is difficult as the network created by the Docker Compose between all the dependencies are isolated.
