Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
akoskm opened this issue May 29, 2021 · 17 comments · Fixed by #25
Assignees

Comments

@akoskm
Copy link
Contributor

akoskm commented May 29, 2021

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
Copy link
Contributor Author

akoskm commented May 29, 2021

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
Copy link
Contributor Author

akoskm commented May 29, 2021

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 marcuspoehls self-assigned this May 29, 2021
@marcuspoehls
Copy link
Member

@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
Copy link
Contributor Author

akoskm commented May 29, 2021

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:

before(async () => {
await Mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true,
replicaSet: replicaSetName,
serverSelectionTimeoutMS: 1500
})
})

mine goes like this:

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

where MONGO_URI is coming from the workflow file.

@marcuspoehls
Copy link
Member

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
Copy link
Contributor Author

akoskm commented May 29, 2021

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
Copy link
Member

@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
Copy link
Contributor Author

akoskm commented May 29, 2021

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
Copy link
Contributor Author

akoskm commented May 30, 2021

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
Copy link
Member

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
Copy link
Contributor Author

akoskm commented May 31, 2021

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

akoskm added a commit to akoskm/mongodb-github-action that referenced this issue Jun 5, 2021
@marcuspoehls marcuspoehls reopened this Jun 7, 2021
@eBsowka
Copy link

eBsowka commented Jul 16, 2021

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
Copy link
Contributor Author

akoskm commented Jul 17, 2021

@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
Copy link
Member

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

@vladkasianenko
Copy link

Any updates here?
image
image

@marcuspoehls
Copy link
Member

@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
Copy link

Zig1375 commented Mar 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants