0

I am trying to set up a circleCI test, I have created a database in circleCI and I have a docker container which needs to connect to the database, but it can't. Inside my docker container is a script which before it does anything it runs pg_isready, this cannot connect to the database. Here's my circle job creation

   postgres_tests:
    docker:
      - image: circleci/python:3.7
      - image: circleci/postgres:9.6.2-alpine
        environment:
           POSTGRES_USER: postgres
          POSTGRES_DB: my_test
    steps:
      - setup_remote_docker:
          docker_layer_caching: true
      - attach_workspace:
          at: /tmp/workspace
      - run:
          name: Install awscli docker-squash
          working_directory: /
          command: sudo pip3 install awscli docker-squash
      - run: eval `aws ecr get-login --no-include-email --region eu-west-1`
      - checkout
      - run: echo 'export PATH=/usr/lib/postgresql/9.6/bin/:$PATH' >> $BASH_ENV
      - run: sudo apt-get update && sudo apt-get install -y postgresql-client
      - run: psql -h localhost -U postgres --command "ALTER USER postgres WITH PASSWORD 'password';"
      - run:
          name: run_pg_tests
          working_directory: /tmp/workspace
          command: |
            /tmp/workspace/sql/t/run_tests.sh

The run_tests.sh is a script which pulls my docker image from the company repo and then does a docker run on that image.

I have read other people have issues where the database isn't ready so to test this I added pg_isready before the docker run

So my script looks like this

DB_HOST=`psql -X -A -h localhost -U postgres -p 5432 -t -c "select inet_server_addr()"`
DB_PORT=5432
DB_NAME=my_test
DB_USER=postgres
DB_PASSWORD=password


pg_isready -h "${DB_HOST}" -p "${DB_PORT}"

#restore database from supplied image
docker run \
    -e SAPIENTIA_DB_HOST=$DB_HOST \
    -e SAPIENTIA_DB_PORT=$DB_PORT \
    -e SAPIENTIA_DB_NAME=$DB_NAME \
    -e SAPIENTIA_DB_PASSWORD=$DB_PASSWORD \
    -e SAPIENTIA_DB_USER=$DB_USER \
    $EMPTY_DB_FULL_PATH \
    path_to_file/file

I have also tried setting the DB_HOST variable directly to 'localhost' the result is exactly the same

Here's what I get as a result:

127.0.0.1:5432 - accepting connections

127.0.0.1:5432 - no response

I have also tried re-running the test with ssh and connecting myself. Same result, I can connect to the database, but i I then run docker exec and try to connect from inside the docker container it can't connect.

I'm pretty stumped here, so any help would be useful.

DatabaseShouter
  • 814
  • 9
  • 11

1 Answers1

0

EDIT: I've found this documentation page about your issue:

It is not possible to start a service in remote docker and ping it directly from a primary container or to start a primary container that can ping a service in remote docker. To solve that, you’ll need to interact with a service from remote docker, as well as through the same container

That line is not 100% clear to me, but I understand that they tell us that we should run the containers we want to communicate from another container manually. Therefore:

- run:
    name: run_pg_tests
    working_directory: /tmp/workspace
    command: |
    docker run -d --name postgres --env POSTGRES_USER=postgres --env POSTGRES_DB=my_test circleci/postgres:9.6.2-alpine
    /tmp/workspace/sql/t/run_tests.sh

Since the postgres container is not accessible anymore through the local network, your up check could be docker exec postgres pg_isready

You can then set your DB_HOST to postgres in your run script.


Original answer:

I'm not well versed into CircleCI configuration, but my guess would be that your Docker container you run manually is not attached to the same network as the containers launched by CircleCI.

From what I see in the documentation, you can specify the hostname of the service container:

The name the container is reachable by. By default, container services are accessible through localhost

So maybe if you try something lile this:

- image: circleci/postgres:9.6.2-alpine
  name: postgres
  environment:
    POSTGRES_USER: postgres
    POSTGRES_DB: my_test

You can then set your DB_HOST to postgres in your run script.

frankie567
  • 1,703
  • 12
  • 20