Port conflicts with service containers on Docker executor

With many service images you use on CircleCI, it is not possible to change the ports for the image during the job run time.

CircleCI binds container ports to localhost automatically, and this can be an issue when multiple service containers require the same port, such as two postgres databases. Consider the example below: 

version: 2.1
jobs:
  build:
    # The primary container is an instance of the first image listed.
    docker:
      - image: circleci/node:4.8.2-jessie
      - image: postgres:11-alpine-ram
    - image: postgres:11-alpine-ram

Both Postgres service images will attempt to bind to localhost:5432 and cause a conflict.

You can prevent this by providing a name to your image which will be used as a hostname to bind the image ports to.

  docker:
    - image: circleci/node:4.8.2-jessie
    - image: postgres:11-alpine-ram
    - image: postgres:11-alpine-ram
    name: postgres2

Providing the name parameter in the example below will result in the following:

  • The first Postgres container can be accessed from localhost:5432
  • The second Postgres container to be accessed from postgres2:5432

 

Further reading:
https://circleci.com/docs/2.0/configuration-reference/#docker

Was this article helpful?
2 out of 7 found this helpful

Comments

0 comments

Article is closed for comments.