mongodb-github-action: docker: Error response from daemon: Conflict. The container name "/mongodb" is already in use by container

I’m trying to replace my old mongodb services setup with mongodb-github-action because it gives me an easy way to spin up a replica set.

Here’s my old setup:


    services:
      mongodb:
        image: mongo:4.2
        ports:
          - 27017:27017

I removed this and put the supercharge action in my build steps:

name: Build
# This workflow is triggered on pushes to the repository.
on: [push]

jobs:
  build:
    runs-on: self-hosted
    container: 10log/10log-base
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          persist-credentials: false

      - name: Use Node.js
        uses: actions/setup-node@v1
        with:
          node-version: '14'

      - name: MongoDB in GitHub Actions
        uses: supercharge/mongodb-github-action@1.4.1
        with:
          mongodb-version: '4.2'
          mongodb-replica-set: rs0


      - name: Install
        run: npm install

      - name: Bundle
        run: npm run build
        env:
          PAT_TOKEN: ${{ secrets.PAT_AKOSKM }}

      - name: Run unit tests
        env:
          MONGO_URI: mongodb://localhost:27017/test?replicaSet=rs0
        run: |
          ./version.sh
          npm run test-unit

      - name: Run client tests
        run: npm run test-client

      - name: Run integration tests
        env:
          MONGOLAB_URI: mongodb://mongodb/test?replicaSet=rs0
          MONGO_URI: mongodb://mongodb/test?replicaSet=rs0
        run: npm run test-integration

My problem is that the first test in the “Run unit tests” build step fails to connect to the mongodb instance:

(node:427) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
  name: 'MongoNetworkError'
}]

but I think this is caused by an uncaught error that happens much earlier, in MongoDB in GithHub Actions:

Starting as single-node replica set (in replica set [rs0])
docker: Error response from daemon: Conflict. The container name "/mongodb" is already in use by container "ba21c3f0db068d6a3978d2217b8c012a181c7c748ade61c4d3e56cc1dfaff392". You have to remove (or rename) that container to be able to reuse that name.

For reference, here’s how the MongoDB in GithHub Actions command ends:

Initiating replica set [rs0]
MongoDB shell version v4.2.14
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f3bb2a76-c258-4466-b7e2-8dc67b73a12d") }
MongoDB server version: 4.2.14
{
	"operationTime" : Timestamp(1622264561, 1),
	"ok" : 0,
	"errmsg" : "already initialized",
	"code" : 23,
	"codeName" : "AlreadyInitialized",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1622264561, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
Check! Initiated replica set [rs0]

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 23 (15 by maintainers)

Commits related to this issue

Most upvoted comments

@laurogripa Hey Lauro, looks like CI/CD deletes the containers after processing the pipeline. Locally, act won’t clean up and keep the containers. When running the pipeline a second time, the containers already exist. The container recreation then fails with this issue.

One thing coming to my mind: a workaround here could be the explicit acceptance of such cases. For example, we’re extending this MongoDB GitHub Action to contact the Docker daemon and check if the container name that should be used is already present. If a container with the same name already exists, we could accept that and be done. This could be an opt-in setting that is helpful during local development.

Would you be interested in contributing this functionality?

@akoskm @eBsowka Yeah, I remember why I never merged the docker-network branch: the GitHub Actions runner does not properly clean up the network used to connect all containers.

There are two ways of networking when using docker actions on a self-hosted runners:

  • you run your tests directly on the runner
    • you can connect to dependencies (like MongoDB) using localhost as the host address
  • you run your tests in another docker container
    • the runner creates a dedicated Docker network for all containers configured in the action’s workflow file
    • the runner then connects all containers to this dedicated network
    • your test can then use localhost to connect to dependencies
    • third-party actions (like this MongoDB container action) must also connect to the dedicated network
    • yet, there’s no step to tell the runner to disconnect third-party container actions to disconnect from the network or clean themselfs up when tearing down

While writing this, I think there’s a solution we can build into this MongoDB action: an input that disconnects the container from the network ensuring that your self-hosted runner can clean up correctly (remove container, remove the network).