Setting up Redis in CircleCI

Hi guys,

I’ve searched high and low for a quick simple solution to my problem but am struggling.

I have a simple script in my node js project which will spin up a redis docker container on port 6378 (not the default 6379). This is to help my integration tests avoid port collisions on my local environment.

When I run my tests on CircleCI, I get the following error:

Redis connection to localhost:6378 failed - connect ECONNREFUSED 127.0.0.1:6378

Here is my config.yml:

version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/node:13
    working_directory: ~/repo

    steps:
      - checkout
      - setup-remote-docker

      - run: echo "lerna installing"
      - run: sudo npm install -g lerna
      - run: sudo npm i
      - run: lerna bootstrap

      # build, lint, test!
      - run: npm run build
      - run: npm run lint
      - run: npm run test

workflows:
  version: 2
  build_deploy:
    jobs:
      - build

I have seen from my searching that we aren’t allowed permissions to change the ports and that docker-compose may be a good alternative, however I don’t know how to set this up. Any pointers?

Thank you.

Edit:
Here are the npm scripts that set up the container and run the tests:

"test:int": "npm run redis-test:start && jest --passWithNoTests --forceExit --runInBand --testRegex ispec\\.ts$ && npm run redis-test:stop",
"redis-test:start": "echo Starting up redis integration testing server && docker run --name redis-testing -d -p 6378:6379 redis",
"redis-test:stop": "echo Stopping redis integration testing server && docker rm $(docker stop $(docker ps -a -q --filter=\"name=redis-testing\"))"

Hi @vck3000! You can specify more than one Docker image to run during a job - typically this is used for databases, redis, or other services. You can check out some examples of this in use here: https://circleci.com/docs/2.0/databases/.

Adding answer here if anyone need more than 1 redis setup in circleCI with different port. Please find working config.yml of mine:

version: 2.1
orbs:
  node: circleci/node@4.1.0
executors:
  node-default-executor:
    docker:
      - image: cimg/node:12.20
      - image: circleci/postgres:9.6.2-alpine
      - image: circleci/redis:4.0.9-alpine
      - image: circleci/redis:4.0.9-alpine
        command: redis-server --port 6380
      - image: minio/minio:RELEASE.2021-08-05T22-01-19Z
        environment:
          MINIO_ACCESS_KEY: minioadmin
          MINIO_SECRET_KEY: minioadmin
        command: ["server", "/data"]
jobs:
  mocha-test:
    executor: node-default-executor
    steps:
      - checkout
      - node/install-packages
      - run:
          name: Install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.6.1
      - run:
          name: Waiting for Postgres to be ready
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Waiting for Redis primary to be ready
          command: dockerize -wait tcp://localhost:6379 -timeout 1m
      - run:
          name: Waiting for Redis secondary (US) to be ready
          command: dockerize -wait tcp://localhost:6380 -timeout 1m
      - run:
          name: Waiting for Minio to be ready
          command: dockerize -wait tcp://localhost:9000 -timeout 1m
      - run: REDIS_PORT_US=6380 npm run test
workflows:
  node-tests:
    jobs:
      - mocha-test