supercharge / mongodb-github-action

Use MongoDB in GitHub Actions
MIT License
222 stars 46 forks source link

docker: Error response from daemon: Conflict. The container name "/mongodb" is already in use by container #24

Open akoskm opened 3 years ago

akoskm commented 3 years ago

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]
akoskm commented 3 years ago

I made the error from `MongoDB in GitHub Action disappear by cleaning up the docker images on my actions runner machine:

Starting as single-node replica set (in replica set [rs0])
Unable to find image 'mongo:4.2' locally
4.2: Pulling from library/mongo

I'm still getting the same error from the tests.

akoskm commented 3 years ago

If I mess up the connection string, for example:

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

I get a different error:

(node:427) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [Error: getaddrinfo ENOTFOUND mongodb
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
  name: 'MongoNetworkError'
}]
marcuspoehls commented 3 years ago

@akoskm Hey Akos, looks like you're connecting to MongoDB using a Node.js app. Is it the mongodb package or Mongoose or something else you're using?

Is your app using the provided MONGO_URI "as is" or do you replace/add some other values (Iike auth credentials)?

akoskm commented 3 years ago

Hey, thanks for the prompt response @marcuspoehls!

I'm using Mongoose to connect to MongoDB. MONGO_URI is used "as is".

This is how my setup was working with services:.

It's really similar to what you have in your test:

https://github.com/supercharge/mongodb-github-action/blob/56f4c41162527145c8f1b4c4504e0f307eb50029/test/replica-set.js#L12-L19

mine goes like this:

await mongoose.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

where MONGO_URI is coming from the workflow file.

marcuspoehls commented 3 years ago

Good you found the test using Mongoose. I remember running into issues with the database connection when using Mongoose and replica sets. You already copied the connection details from the test. That's good. Could you please test whether adding the replica set config to the Mongoose object helps? Maybe it gets overridden in the connection string. But I'm not sure.

akoskm commented 3 years ago

Tried this, actually, I wrapped my above connect code such as:

before(async function(){
    try {
      console.log(MONGO_URI);
      await mongoose.connect(MONGO_URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        serverSelectionTimeoutMS: 10000,
        replicaSet: 'rs0'
      });
      app.db = mongoose.connection;
      // please ignore these lines, I'm setting up some models like this because 5y ago it seemed like a good idea 😅
      require('../../schema/Finish')(app, mongoose);
      require('../../schema/Counter')(app, mongoose);
    } catch (err) {
      console.log(err)
    }
  });

and here's the error I'm getting:


MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:42069
    at NativeConnection.Connection.openUri (/__w/10log-base/10log-base/node_modules/mongoose/lib/connection.js:800:32)
    at /__w/10log-base/10log-base/node_modules/mongoose/lib/index.js:342:10
    at /__w/10log-base/10log-base/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/__w/10log-base/10log-base/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Mongoose.connect (/__w/10log-base/10log-base/node_modules/mongoose/lib/index.js:341:10)
    at Context.<anonymous> (/__w/10log-base/10log-base/test/unit/index.js:16:22)
    at callFn (/__w/10log-base/10log-base/node_modules/mocha/lib/runnable.js:358:21)
    at Hook.Runnable.run (/__w/10log-base/10log-base/node_modules/mocha/lib/runnable.js:346:5)
    at next (/__w/10log-base/10log-base/node_modules/mocha/lib/runner.js:454:10)
    at Immediate.<anonymous> (/__w/10log-base/10log-base/node_modules/mocha/lib/runner.js:516:5)
    at processImmediate (internal/timers.js:458:21) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) { 'localhost:42069' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}
marcuspoehls commented 3 years ago

@akoskm Looks like your connection string has the wrong port. Can you check whether the MONGO_URI is the one provided in your GitHub Action workflow? Because the error message says port 42069 and I guess you’re trying to connect on port 27017

akoskm commented 3 years ago

Oh sorry, in the meantime I updated my yaml config to experiment with different ports:

      - name: MongoDB in GitHub Actions
        uses: supercharge/mongodb-github-action@1.4.1
        with:
          mongodb-version: '4.2'
          mongodb-replica-set: rs0
          mongodb-port: 42069
akoskm commented 3 years ago

I found in the input of this action the following:

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("09535804-17aa-4146-b0d5-32f762bbc573") }

So I tried hardcoding the full URL I see here in my test (compressors cannot be disabled in Mongoose):

MONGO_URI = 'mongodb://127.0.0.1:42069/?compressors=snappy&gssapiServiceName=mongodb';

I got the following error:

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:42069
    at NativeConnection.Connection.openUri (/__w/10log-base/10log-base/node_modules/mongoose/lib/connection.js:800:32)
    at /__w/10log-base/10log-base/node_modules/mongoose/lib/index.js:342:10
    at /__w/10log-base/10log-base/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/__w/10log-base/10log-base/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Mongoose.connect (/__w/10log-base/10log-base/node_modules/mongoose/lib/index.js:341:10)
    at Context.<anonymous> (/__w/10log-base/10log-base/test/unit/index.js:16:22)
    at callFn (/__w/10log-base/10log-base/node_modules/mocha/lib/runnable.js:358:21)
    at Hook.Runnable.run (/__w/10log-base/10log-base/node_modules/mocha/lib/runnable.js:346:5)
    at next (/__w/10log-base/10log-base/node_modules/mocha/lib/runner.js:454:10)
    at Immediate.<anonymous> (/__w/10log-base/10log-base/node_modules/mocha/lib/runner.js:516:5)
    at processImmediate (internal/timers.js:458:21) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) { '127.0.0.1:42069' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}
marcuspoehls commented 3 years ago

Looks like the connection attempt from the logs goes to the default port 27017. Even with a custom port configured.

Just to make sure: you're still using the Mongoose options for URL parser and topology? I remember those being required for replica sets. Mongoose wouldn't connect properly without these

Akos, do you work on a public project where I can look at the logs of the GitHub Action?

akoskm commented 3 years ago

I just sent you a link to the build log/yaml file in DM on Twitter.

eBsowka commented 3 years ago

Hello Was the last issue resolved? What was the problem? I am currently struggling with the same problem. I start the MongoDB in git actions I manage to restore database but then when I try to connect to that restored database with nextflow pipeline it gives me: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

akoskm commented 3 years ago

@eBsowka I'm still using the docker-network branch:

uses: supercharge/mongodb-github-action@docker-network

@marcuspoehls do you remember why this wasn't merged into master? Is it the issue with the network not being cleaned up?

marcuspoehls commented 3 years ago

@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:

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).

vladkasianenko commented 6 months ago

Any updates here? image image

marcuspoehls commented 6 months ago

@vladkasianenko Hey Vlad, I didn’t look into this issue over the last year. I have to get back digging and reading on container actions. Maybe GitHub changed or improved the way how they work. If you have any knowledge about solving this issue, please share it. I appreciate any progress, hint, and lead 🙂

Zig1375 commented 5 months ago

same issue, but I have it in my own server with gitea and act_runner... My script cannot connect to mongo (from mongoose) and once I restart my action it stops because container exists...

hanyang1986 commented 1 month ago
  # Stop and remove all running Docker containers before the job
  - name: Stop and remove all running Docker containers
    run: |
      echo "Stopping all running Docker containers..."
      docker ps -q | xargs --no-run-if-empty docker stop
      echo "Removing all Docker containers..."
      docker ps -a -q | xargs --no-run-if-empty docker rm