Creating a Mongo replicaset in a CircleCI job

Viewed 610

I am trying to run integration tests in CircleCI on code that makes use of MongoDB Transactions. I am currently getting the following error:

(IllegalOperation) Transaction numbers are only allowed on a replica set member or mongos

Is there a way to initiate a replicaset before running the tests?

I have tried using docker exec to run commands on the container without any success.

Has anyone managed to do this before?

Currently my circleci config file looks like this:

 integration:
    docker:
      - image: circleci/golang:1.13
      - image: circleci/mongo:3
    steps:
      - attach_workspace:
          at: .

      - run: go test ./... -mod vendor -v -race -tags integration
1 Answers

first off, you need to initialize mongod as a replica set. The docker: section takes a command tag that can be run at start up

- image: circleci/mongo:4.0
  command: [--replSet,rs0]

now the mongo container will automatically start up as a replSet. However you aren't out of the weeds yet. You still need to run rs.Initiate(), and that must be run outside of the mongo container (since it is running the mongod server).

Whenever you use run it relates to the first container, which in this case is the golang container. You need to install the mongo-cli in that container and then run rs.initiate()

In my case, I was running an Alpine docker container and resolved it with this code

  - run:
    name: "Initialize Replica Set"
    command: |
      echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/main' >> /etc/apk/repositories
      echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/community' >> /etc/apk/repositories
      apk update
      apk add mongodb=3.4.4-r0
      mongo mongodb://localhost:27017 --eval "rs.initiate()"

However I think the circle:golang containers are running off of Debian. According to this blog it looks like you can run

  - run:
    name: "Initialize Replica Set"
    command: |
      echo "deb http://repo.mongodb.org/apt/debian "$(lsb_release -sc)"/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb.list
      sudo apt-get update
      sudo apt-get install mongodb-org
      mongo mongodb://localhost:27017 --eval "rs.initiate()"
Related