Search code examples
docker-composecirclecidocker-networkcircleci-2.0docker-in-docker

Networking using docker-compose in docker executor in circleci


this is a circleci question I guess.

I am quite happy with circleci but now I ran into a problem and I don't know what I'm doing wrong. Maybe this is something very easy, but I don't see the it.

In short

I can't make containers talk to each other on circleci.

Problem

Basically what I wanted to do is start a server container and a client container, and then let them talk to each other. I created a minimal example here: https://github.com/mRcSchwering/circleci-integration-test

The README.md basically explains the desired outcome. I have a .circleci/config.yml like this:

version: 2
jobs:
  build:
    docker:
      - image: docker:18.03.0-ce-git
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Install docker-compose
          command: |
            apk --update add py2-pip
            /usr/bin/pip2 install docker-compose
            docker-compose --version
      - run:
          name: Start Container
          command: |
            docker-compose up -d
            docker-compose ps
      - run:
          name: Let client talk to server
          command: |
            docker-compose run client psql -h server -p 5432 -U postgres -c "\l"

In a docker container, docker-compose is installed, which is then used to start a server and a client (postgres here). In the last step I am telling the client to query the server. However, it cannot find the server:

#!/bin/sh -eo pipefail
docker-compose run client psql -h server -p 5432 -U postgres -c "\l"
Starting project_server_1 ... 

^@^@psql: could not connect to server: Connection refused
    Is the server running on host "server" (172.18.0.2) and accepting
    TCP/IP connections on port 5432?
Exited with code 2

Files

The docker-compose.yml looks like this

version: '2'

services:
  server:
    image: postgres:9.5.12-alpine
    networks:
      - internal
    expose:
      - '5432'
  client:
    build:
      context: .
    networks:
      - internal
    depends_on:
        - server

networks:
  internal:
    driver: bridge

where the client is built from a dockerfile like this

FROM alpine:3.7
RUN apk --no-cache add postgresql-client && rm -rf /var/cache/apk/*

Note

If I repeat everything on my Linux (also with docker-in-docker) it works. But I guess some things work completely different on circleci. I found some people mentioning that on circleci networking and bind mounts can be tricky but I didn't find anything that can help me. There is this doc but I thought I am doing this already. Then there is this project where someone seems to do the same thing on circleci successfully. But I cannot figure out what's different there...

Anyway I would really appreciate your help. So far I have given up on this.

Best Marc


Solution

  • Ok, in the meanwhile I (no actually it was halfer from the circleci forum) noticed that docker-compose run client psql -h server -p 5432 -U postgres -c "\l" was run before the server was up and running. A simple sleep 5 after docker-compose up -d fixes the problem.