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

[Bug]: MongoDB WithReplicaSet fails to find server #2672

Closed
Chris-Sheridan opened this issue Jul 28, 2024 · 9 comments
Closed

[Bug]: MongoDB WithReplicaSet fails to find server #2672

Chris-Sheridan opened this issue Jul 28, 2024 · 9 comments
Labels
bug An issue with the library

Comments

@Chris-Sheridan
Copy link

Testcontainers version

0.32.0

Using the latest Testcontainers version?

Yes

Host OS

MacOS

Host arch

ARM

Go version

1.22.2

Docker version

Client:
 Cloud integration: v1.0.35+desktop.13
 Version:           26.0.0
 API version:       1.45
 Go version:        go1.21.8
 Git commit:        2ae903e
 Built:             Wed Mar 20 15:14:46 2024
 OS/Arch:           darwin/arm64
 Context:           desktop-linux

Server: Docker Desktop 4.29.0 (145265)
 Engine:
  Version:          26.0.0
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.8
  Git commit:       8b79278
  Built:            Wed Mar 20 15:18:02 2024
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Docker info

Client:
 Version:    26.0.0
 Context:    desktop-linux
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.13.1-desktop.1
    Path:     /Users/chriss/.docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.26.1-desktop.1
    Path:     /Users/chriss/.docker/cli-plugins/docker-compose
  debug: Get a shell into any image or container. (Docker Inc.)
    Version:  0.0.27
    Path:     /Users/chriss/.docker/cli-plugins/docker-debug
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.2
    Path:     /Users/chriss/.docker/cli-plugins/docker-dev
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.23
    Path:     /Users/chriss/.docker/cli-plugins/docker-extension
  feedback: Provide feedback, right in your terminal! (Docker Inc.)
    Version:  v1.0.4
    Path:     /Users/chriss/.docker/cli-plugins/docker-feedback
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v1.1.0
    Path:     /Users/chriss/.docker/cli-plugins/docker-init
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     /Users/chriss/.docker/cli-plugins/docker-sbom
  scout: Docker Scout (Docker Inc.)
    Version:  v1.6.3
    Path:     /Users/chriss/.docker/cli-plugins/docker-scout

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 20
 Server Version: 26.0.0
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  seccomp
   Profile: unconfined
  cgroupns
 Kernel Version: 6.6.22-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 12
 Total Memory: 7.657GiB
 Name: docker-desktop
 ID: 7b791e9f-a4a6-4582-94dd-1d6571714207
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Labels:
  com.docker.desktop.address=unix:///Users/chriss/Library/Containers/com.docker.docker/Data/docker-cli.sock
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: daemon is not using the default seccomp profile

What happened?

MongoDB testcontainer works brilliantly, but I have multi-collection ACID transactions in my code. When I use the WithReplicaSet, the Go mongo client is unable to find the servers (via ping, calls to collections). The test container provides a connection string (e.g. mongodb://localhost:50144), but it's unreachable from the Go mongodb driver.

I assume this is something related to the Docker network internals, but I am stuck at the moment and appreciate anyone's input.

Test container setup:
mongo, err := mongodb.Run(ctx, "mongo:latest", mongodb.WithReplicaSet("rs0"), )

Client call:
` if client, err = mongo.Connect(ctx, opts); err != nil {
return repo, errors.Join(err, errors.New("unable to connect to MongoDB: "+url))
}

err = client.Ping(ctx, nil)`

Relevant log output

No response

Additional information

No response

@Chris-Sheridan Chris-Sheridan added the bug An issue with the library label Jul 28, 2024
@stevenh
Copy link
Collaborator

stevenh commented Aug 8, 2024

This should be fixed by #2699

@mdelapenya
Copy link
Member

Ey @Chris-Sheridan can you check with latest release from yesterday, v0.33.0?

If fixed, let's close this one. Thanks!

@Chris-Sheridan
Copy link
Author

Chris-Sheridan commented Aug 24, 2024

I really appreciate everyone's help here. Unfortunately @mdelapenya, it's still not working. I've tried several connection strings, but constantly get an error message 'unable to connect' when trying to ping the cluster. Do you have an example of a connection string that would work? I assume the replicaSet needs to be a part of it.

I can dig in more later when I have some time.

Also, if I apply WithUsername or WithPassword along with WithReplica, there's an error trying to spin up the container saying the mapped port wasn't created within 5 seconds.

@mdelapenya
Copy link
Member

Mmm here you can find an example for the credentials: https://pkg.go.dev/github.com/testcontainers/testcontainers-go/modules/mongodb#example-Run-WithCredentials, and some tests using the replica set option: https://github.com/testcontainers/testcontainers-go/blob/main/modules/mongodb/mongodb_test.go#L59

We probably need a testable example for the replicaset though.

@dimboknv
Copy link

try to add "/?replicaSet=&directConnection=true" to connection string

@Chris-Sheridan
Copy link
Author

Thanks @dimboknv! I knew there was a trick to that connection string. It works now, but I found another bug. When you use WithUsername and WithPassword along with the WithReplicaSet options, you get an error. I will open a separate ticket for that and dig in.

@mdelapenya
Copy link
Member

Hey @Chris-Sheridan given you were able to build the right connection string, and you opened a new ticket for your new findings, I'm closing this one.

Please reopen if you consider it's not done 🙏

@mdelapenya mdelapenya closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2024
@datshiro
Copy link

I don't know whether this is another bug or I'm using the wrong way. I run WithReplicaSet option only and got failure when connecting use connection string.

	mongodbContainer, err := mongodb.Run(s.ctx, "mongo:6", mongodb.WithReplicaSet("rs0"))
	s.NoError(err)
	s.mongodbContainer = mongodbContainer

	uri, err := mongodbContainer.ConnectionString(s.ctx)
	s.NoError((err))

	cfg := mongox.MongoDbConfig{
		Url:          uri,
		DatabaseName: "test-db",
	}
	// Init MongoDB database
	mongoDb := mongox.NewMongoDb(s.ctx, cfg)

However, thank to @dimboknv it worked when I add options to the connection string. Shouldn't ConnectionString method return include replicaSet=&directConnection=true ?

@mdelapenya
Copy link
Member

@datshiro what version are you using? #2699 will be released in the next release. You could give a try of the fix using HEAD main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue with the library
Projects
None yet
Development

No branches or pull requests

5 participants