Can't get Docker containers linking/working

I’ve got two Docker containers which I want to start/link but I get connection refused for Postgres and Connection refused for trying to ping web server container on localhost.

Both images start/link fine and work as expected outside of CircleCI so my issue must be related to how they are to be set up within CircleCI.

version: 2
jobs:
  build:
    docker:
      - image: circleci/node:latest-browsers
    parallelism: 1
    working_directory: ~/myFolder
    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
      - run:
          name: Start application API
          command: |
            set -e
            docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
            docker pull delaneymethod/postgres:9.5
            docker pull delaneymethod/nginx-phpfpm:latest
            docker run --rm --name ci-postgres -d -p 5432:5432 -e POSTGRES_USER=e2e -e POSTGRES_DB=e2e -e POSTGRES_PASSWORD=e2e -e PGDATA=/var/lib/postgresql/data/pgdata delaneymethod/postgres:9.5
            docker run --rm --name ci-webserver -d -p 8000:80 --link postgres:postgres delaneymethod/nginx-phpfpm:latest
            docker ps -a
           curl --retry 3 http://localhost:8000

Within my Nginx-PHP container, the DB host is set to DB_HOST=postgres

Any ideas what I’m doing wrong/missing?

Yep! This is a common pitfall for folks new to CircleCI.

The Docker system here does not allow ports to be exposed in a primary container. I believe that if you were to have permission to do that, you would also be able to monkey around with the networking stack for all customers on the same build server (i.e. the Docker host) and that is obviously impermissible.

There is a variety of posts about this in the forum, including a duplicate from me when I bumped into it!

So, you need to find a way around that - there are a few:

  • Spin up servers in your docker config section. This runs them remotely where they are permitted to expose ports. Those exposed ports will be merged with localhost in the primary build container. Use this approach if you do not need access to the underlying file system of those containers.
  • Run your Docker containers through Compose, and then use the internal networking links between them, rather than publishing ports. The downside is that you will have to convert your build container tests to a separate Docker image, but the upside is that this is a very flexible approach.

You cannot use on-host volumes either, in either solution, so bear that in mind.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.