How to build new Docker image and access running dependencies?

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:

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:

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?

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 Can you give that a try and see if it works?

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.

1 Like

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.