Circle CI - Can't connect to Redis or memcached using Docker Compose, but I can do so on my local machine

Viewed 566

I'm developing a Node.js program that connects to both Redis and memcached. I am testing my Node.js program with Jest, and before running the test I run docker-compose up. My Node.js program connects to the Docker Redis and memcached Docker containers fine, and my tests pass fine on my local machine.

Tests passing on local machine with Docker containers running

However, I want the tests to run on Circle CI so that every time I git push, the CI environment will verify the program is buildable and that tests are passing.

When I try to do the same on Circle CI, it seems that the Docker containers spin up fine, however the tests aren't able to connect to the Redis or memcached servers in the containers, despite it working fine on my local PC.

My config.yml for Circle CI:

version: 2
jobs:
  build:
    docker:
      - image: circleci/node
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Install Docker Compose
          command: |
            curl -L https://github.com/docker/compose/releases/download/1.28.2/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
            chmod +x ~/docker-compose
            sudo mv ~/docker-compose /usr/local/bin/docker-compose
      - run:
          name: Start Container
          command: |
            docker-compose up -d
            docker-compose ps
      - restore_cache:
          key: npm-cache-v1-{{ checksum "package-lock.json" }}
      - run:
          name: Install Dependencies
          command: npm ci
      - save_cache:
          key: npm-cache-v1-{{ checksum "package-lock.json" }}
          paths:
            - /home/circleci/.npm
      - run:
          name: Ensure Test Parity
          command: |
            chmod +x ./validateTestCases.sh
            ./validateTestCases.sh
      - run:
          name: Run Tests
          command: npm test

My docker-compose.yml:

services:
    redis:
        image: redis
        container_name: redis-container
        ports:
            - 6379:6379
    memcached:
        image: memcached
        container_name: memcached-container
        ports:
            - 11211:11211

My build failing test log in Circle CI:

#!/bin/bash -eo pipefail
npm test

> easy-cache@1.0.0 test
> jest

 FAIL  memcached/memcached.test.js
  ● Test suite failed to run

    Error: connect ECONNREFUSED 127.0.0.1:11211



 FAIL  redis/redis.test.js
  ● Test suite failed to run

    Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.

      at mapper (node_modules/jest-jasmine2/build/queueRunner.js:27:45)

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        36.183 s
Ran all test suites.
npm ERR! code 1
npm ERR! path /home/circleci/project
npm ERR! command failed
npm ERR! command sh -c jest

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/circleci/.npm/_logs/2021-02-05T20_29_26_896Z-debug.log


Exited with code exit status 1
CircleCI received exit code 1

Link to my current source code

I am not sure what to try next. I have tried moving the npm test block right after docker-compose up -d but that had no effect.

1 Answers

It turns out that Docker Compose is not required for what I'm trying to do. Instead, you can include multiple Docker images in Circle CI.

Here's my updated Circle CI yaml file, where my tests run successfully (connection to Redis and memcached works like on my local PC using Docker Compose):

version: 2
jobs:
  build:
    docker:
      - image: circleci/node
      - image: redis
      - image: memcached
    steps:
      - checkout
      # - setup_remote_docker
      # - run:
      #     name: Install Docker Compose
      #     command: |
      #       curl -L https://github.com/docker/compose/releases/download/1.28.2/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
      #       chmod +x ~/docker-compose
      #       sudo mv ~/docker-compose /usr/local/bin/docker-compose
      # - run:
      #     name: Start Container
      #     command: |
      #       docker-compose up -d
      #       docker-compose ps
      - restore_cache:
          key: npm-cache-v1-{{ checksum "package-lock.json" }}
      - run:
          name: Install Dependencies
          command: npm ci
      - save_cache:
          key: npm-cache-v1-{{ checksum "package-lock.json" }}
          paths:
            - /home/circleci/.npm
      - run:
          name: Ensure Test Parity
          command: |
            chmod +x ./validateTestCases.sh
            ./validateTestCases.sh
      - run:
          name: Run Tests
          command: npm test

Related