testcontainers / testcontainers-go

Testcontainers for Go is a Go package that makes it simple to create and clean up container-based dependencies for automated integration/smoke tests. The clean, easy-to-use API enables developers to programmatically define containers that should be run as part of a test and clean up those resources when the test is done.
https://golang.testcontainers.org
MIT License
3.3k stars 450 forks source link

[Bug]: Unable to share host port in Mac #2570

Open JulienBreux opened 3 weeks ago

JulienBreux commented 3 weeks ago

Testcontainers version

0.31.0

Using the latest Testcontainers version?

Yes

Host OS

macOS Sonoma 14.5

Host arch

ARM

Go version

1.22.4

Docker version

Client:
 Cloud integration: v1.0.35+desktop.13
 Version:           26.1.1
 API version:       1.45
 Go version:        go1.21.9
 Git commit:        4cf5afa
 Built:             Tue Apr 30 11:44:56 2024
 OS/Arch:           darwin/arm64
 Context:           desktop-linux

Server: Docker Desktop 4.30.0 (149282)
 Engine:
  Version:          26.1.1
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.9
  Git commit:       ac2de55
  Built:            Tue Apr 30 11:48:04 2024
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.31
  GitCommit:        e377cd56a71523140ca6ae87e30244719194a521
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Docker info

Client:
 Version:    26.1.1
 Context:    desktop-linux
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.14.0-desktop.1
    Path:     /Users/julienbreux/.docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.27.0-desktop.2
    Path:     /Users/julienbreux/.docker/cli-plugins/docker-compose
  debug: Get a shell into any image or container (Docker Inc.)
    Version:  0.0.29
    Path:     /Users/julienbreux/.docker/cli-plugins/docker-debug
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.2
    Path:     /Users/julienbreux/.docker/cli-plugins/docker-dev
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.23
    Path:     /Users/julienbreux/.docker/cli-plugins/docker-extension
  feedback: Provide feedback, right in your terminal! (Docker Inc.)
    Version:  v1.0.4
    Path:     /Users/julienbreux/.docker/cli-plugins/docker-feedback
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v1.1.0
    Path:     /Users/julienbreux/.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/julienbreux/.docker/cli-plugins/docker-sbom
  scout: Docker Scout (Docker Inc.)
    Version:  v1.8.0
    Path:     /Users/julienbreux/.docker/cli-plugins/docker-scout

Server:
 Containers: 2
  Running: 0
  Paused: 0
  Stopped: 2
 Images: 17
 Server Version: 26.1.1
 Storage Driver: overlayfs
  driver-type: io.containerd.snapshotter.v1
 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: e377cd56a71523140ca6ae87e30244719194a521
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  seccomp
   Profile: unconfined
  cgroupns
 Kernel Version: 6.6.26-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 12
 Total Memory: 7.657GiB
 Name: docker-desktop
 ID: ffbd4f59-685e-499b-8eeb-7368da5e4e4c
 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/julienbreux/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

What happened?

Just run /usr/local/go/bin/go test -timeout 60s -run ^TestExposeHostPorts$ github.com/testcontainers/testcontainers-go -v

Relevant log output

Running tool: /usr/local/go/bin/go test -timeout 60s -run ^TestExposeHostPorts$ github.com/testcontainers/testcontainers-go -v

=== RUN   TestExposeHostPorts
=== RUN   TestExposeHostPorts/single_port
2024/06/10 09:57:58 🐳 Creating container for image testcontainers/ryuk:0.7.0
2024/06/10 09:57:58 βœ… Container created: 2ce517a76312
2024/06/10 09:57:58 🐳 Starting container: 2ce517a76312
2024/06/10 09:57:58 βœ… Container started: 2ce517a76312
2024/06/10 09:57:58 🚧 Waiting for container id 2ce517a76312 image: testcontainers/ryuk:0.7.0. Waiting for: &{Port:8080/tcp timeout:<nil> PollInterval:100ms}
2024/06/10 09:57:58 πŸ”” Container is ready: 2ce517a76312
2024/06/10 09:57:58 github.com/testcontainers/testcontainers-go - Connected to docker:
  Server Version: 26.0.0
  API Version: 1.44
  Operating System: Docker Desktop
  Total Memory: 7840 MB
  Resolved Docker Host: tcp://127.0.0.1:62541
  Resolved Docker Socket Path: /var/run/docker.sock
  Test SessionID: bc811786169a853888375cda60a113c0b5ae6614281106eca5926af257abd460
  Test ProcessID: 7df84bcc-cc01-43b6-924f-3cdd8b246503
2024/06/10 09:57:58 Failed to get image auth for https://index.docker.io/v1/. Setting empty credentials for the image: alpine:3.17. Error is:credentials not found in native keychain
    /Users/julienbreux/Projects/testcontainers-go/port_forwarding_test.go:108: failed to expose host ports: failed to create the SSH server: Cannot connect to the Docker daemon at tcp://127.0.0.1:62541. Is the docker daemon running?: failed to create container: failed to create container
=== RUN   TestExposeHostPorts/single_port_using_a_network
    /Users/julienbreux/Projects/testcontainers-go/port_forwarding_test.go:87: Cannot connect to the Docker daemon at tcp://127.0.0.1:62541. Is the docker daemon running?: failed to create network
=== RUN   TestExposeHostPorts/multiple_ports
    /Users/julienbreux/Projects/testcontainers-go/port_forwarding_test.go:108: Cannot connect to the Docker daemon at tcp://127.0.0.1:62541. Is the docker daemon running?: failed to create container
=== RUN   TestExposeHostPorts/single_port_with_cancellation
    /Users/julienbreux/Projects/testcontainers-go/port_forwarding_test.go:108: Cannot connect to the Docker daemon at tcp://127.0.0.1:62541. Is the docker daemon running?: failed to create container
--- FAIL: TestExposeHostPorts (2.09s)
    --- FAIL: TestExposeHostPorts/single_port (2.08s)
    --- FAIL: TestExposeHostPorts/single_port_using_a_network (0.00s)
    --- FAIL: TestExposeHostPorts/multiple_ports (0.00s)
    --- FAIL: TestExposeHostPorts/single_port_with_cancellation (0.00s)
FAIL
FAIL    github.com/testcontainers/testcontainers-go 2.290s

Additional information

No response

JulienBreux commented 3 weeks ago

cc @lbroudoux ❀️

mdelapenya commented 3 weeks ago

@JulienBreux I see connections to the docker daemon to a TCP address:

Cannot connect to the Docker daemon at tcp://127.0.0.1:62541

Could it be possible that you switched from Testcontainers Cloud, which writes to the testcontainers properties file a TCP remote docker host, to plain Docker Desktop (or alternative) without using the Testcontainers Desktop app (which uses the tcd Docker context to manage the connections)?

roastiek commented 6 days ago

I have expericed something similar when I was testing host ports. I think the problem is hardcoded localhost in https://github.com/testcontainers/testcontainers-go/blob/bca5a282a15f345b8db7c91179dcb38f74cc7a2d/port_forwarding.go#L230

I was able to fix my problem by changing localhost to sshdC.Host(ctx) like in wait.ForListeningPort https://github.com/testcontainers/testcontainers-go/blob/bca5a282a15f345b8db7c91179dcb38f74cc7a2d/wait/host_port.go#L83

And I think that the ssh container should idealy set WaitingFor: wait.ForListeningPort(sshPort) in its setup https://github.com/testcontainers/testcontainers-go/blob/bca5a282a15f345b8db7c91179dcb38f74cc7a2d/port_forwarding.go#L153-L158

mdelapenya commented 6 days ago

It's weird, as I'm on a Mac too and I do not see this error. Could you share more about your scenario if possible?

JulienBreux commented 6 days ago

For me a simple:

/usr/local/go/bin/go test -timeout 60s -run ^TestExposeHostPorts$ github.com/testcontainers/testcontainers-go -v

Docker Desktop

roastiek commented 5 days ago

I think that interface/ip address which used for exposed ports (in this case ssh port) is configurable in docker and is not guaranteed that it will be localhost on every system.