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]: Unable to share host port in Mac #2570

Open
JulienBreux opened this issue Jun 10, 2024 · 10 comments
Open

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

JulienBreux opened this issue Jun 10, 2024 · 10 comments
Labels
bug An issue with the library

Comments

@JulienBreux
Copy link
Contributor

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 JulienBreux added the bug An issue with the library label Jun 10, 2024
@JulienBreux
Copy link
Contributor Author

JulienBreux commented Jun 10, 2024

cc @lbroudoux ❤️

@mdelapenya
Copy link
Member

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

I have expericed something similar when I was testing host ports. I think the problem is hardcoded localhost in

pw := NewPortForwarder(fmt.Sprintf("localhost:%s", sshdC.port), sshdC.sshConfig, port, port)

I was able to fix my problem by changing localhost to sshdC.Host(ctx) like in wait.ForListeningPort

ipAddress, err := target.Host(ctx)

And I think that the ssh container should idealy set WaitingFor: wait.ForListeningPort(sshPort) in its setup

ContainerRequest: ContainerRequest{
Image: sshdImage,
HostAccessPorts: []int{}, // empty list because it does not need any port
ExposedPorts: []string{sshPort},
Env: map[string]string{"PASSWORD": sshPassword},
},

@mdelapenya
Copy link
Member

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

For me a simple:

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

Docker Desktop

@roastiek
Copy link

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.

@lbroudoux
Copy link

lbroudoux commented Jul 23, 2024

I'm still facing the same issue with v0.32.0... Any hints on what could go wrong?

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

=== RUN   TestExposeHostPorts
=== RUN   TestExposeHostPorts/single_port
2024/07/23 14:07:15 🐳 Creating container for image testcontainers/ryuk:0.8.1
2024/07/23 14:07:16 ✅ Container created: 4c138984352b
2024/07/23 14:07:16 🐳 Starting container: 4c138984352b
2024/07/23 14:07:16 ✅ Container started: 4c138984352b
2024/07/23 14:07:16 ⏳ Waiting for container id 4c138984352b image: testcontainers/ryuk:0.8.1. Waiting for: &{Port:8080/tcp timeout:<nil> PollInterval:100ms}
2024/07/23 14:07:16 🔔 Container is ready: 4c138984352b
2024/07/23 14:07:16 🐳 Creating container for image testcontainers/sshd:1.2.0
2024/07/23 14:07:16 ✅ Container created: a0870f18640a
2024/07/23 14:07:16 🐳 Starting container: a0870f18640a
2024/07/23 14:07:16 ✅ Container started: a0870f18640a
2024/07/23 14:07:16 🔔 Container is ready: a0870f18640a
2024/07/23 14:07:16 🐳 Creating container for image alpine:3.17
2024/07/23 14:07:16 ✅ Container created: 4ae584788dd7
2024/07/23 14:07:16 🐳 Starting container: 4ae584788dd7
2024/07/23 14:07:16 ✅ Container started: 4ae584788dd7
2024/07/23 14:07:16 🔔 Container is ready: 4ae584788dd7
2024/07/23 14:07:16 🐳 Terminating container: 4ae584788dd7
2024/07/23 14:07:16 🐳 Terminating container: a0870f18640a
2024/07/23 14:07:16 🚫 Container terminated: a0870f18640a
2024/07/23 14:07:17 🚫 Container terminated: 4ae584788dd7
=== RUN   TestExposeHostPorts/single_port_using_a_network
    port_forwarding_test.go:112: failed to expose host ports: failed to get the network: failed to list networks: Cannot connect to the Docker daemon at tcp://127.0.0.1:51636. Is the docker daemon running?: failed to create container
=== RUN   TestExposeHostPorts/multiple_ports
2024/07/23 14:07:17 🐳 Creating container for image testcontainers/sshd:1.2.0
2024/07/23 14:07:17 ✅ Container created: 07934e50c602
2024/07/23 14:07:17 🐳 Starting container: 07934e50c602
2024/07/23 14:07:17 ✅ Container started: 07934e50c602
2024/07/23 14:07:17 🔔 Container is ready: 07934e50c602
2024/07/23 14:07:17 🐳 Creating container for image alpine:3.17
2024/07/23 14:07:17 ✅ Container created: 9d3b2ef3fd10
2024/07/23 14:07:17 🐳 Starting container: 9d3b2ef3fd10
2024/07/23 14:07:17 ✅ Container started: 9d3b2ef3fd10
2024/07/23 14:07:17 🔔 Container is ready: 9d3b2ef3fd10
2024/07/23 14:07:17 🐳 Terminating container: 9d3b2ef3fd10
2024/07/23 14:07:17 🐳 Terminating container: 07934e50c602
2024/07/23 14:07:17 🚫 Container terminated: 07934e50c602
2024/07/23 14:07:17 🚫 Container terminated: 9d3b2ef3fd10
=== RUN   TestExposeHostPorts/single_port_with_cancellation
2024/07/23 14:07:17 🐳 Creating container for image testcontainers/sshd:1.2.0
2024/07/23 14:07:17 ✅ Container created: b4dbdc85c12f
2024/07/23 14:07:17 🐳 Starting container: b4dbdc85c12f
2024/07/23 14:07:17 ✅ Container started: b4dbdc85c12f
2024/07/23 14:07:17 🔔 Container is ready: b4dbdc85c12f
2024/07/23 14:07:17 🐳 Creating container for image alpine:3.17
2024/07/23 14:07:17 ✅ Container created: 5f6d4afe4753
2024/07/23 14:07:17 🐳 Starting container: 5f6d4afe4753
2024/07/23 14:07:18 ✅ Container started: 5f6d4afe4753
2024/07/23 14:07:18 🔔 Container is ready: 5f6d4afe4753
2024/07/23 14:07:29 🐳 Terminating container: 5f6d4afe4753
2024/07/23 14:07:29 🐳 Terminating container: b4dbdc85c12f
2024/07/23 14:07:29 🚫 Container terminated: b4dbdc85c12f
2024/07/23 14:07:29 🚫 Container terminated: 5f6d4afe4753
--- FAIL: TestExposeHostPorts (13.48s)
    --- PASS: TestExposeHostPorts/single_port (1.12s)
    --- FAIL: TestExposeHostPorts/single_port_using_a_network (0.09s)
    --- PASS: TestExposeHostPorts/multiple_ports (0.67s)
    --- PASS: TestExposeHostPorts/single_port_with_cancellation (11.60s)
FAIL
FAIL	github.com/testcontainers/testcontainers-go	13.993s
FAIL

$ uname -a

Darwin MacBook-Pro-de-Laurent-2.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct  9 21:28:45 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6020 arm64

cc @JulienBreux @mdelapenya

@roastiek
Copy link

I have found out that our setup runs in containers which are accesing host docker daemon using mounted socket (something like -v /var/run/docker.sock:/var/run/docker.sock). However they are not using host network and cannot access loopback interface of the host.
Container.Host(...) method handle this situation and returns ip address of container's network gateway (172.17.0.1 for example) instead of localhost, which works.

@mdelapenya
Copy link
Member

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.

I think #2403 tries to address that.

@lbroudoux
Copy link

I think this is now OK with a fresh git clone of the repo! I just had to change the go.mod to reference go 1.22.0 instead of go 1.22 (was failing on Mac).

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

go: downloading github.com/docker/docker v27.1.1+incompatible
=== RUN   TestExposeHostPorts
=== RUN   TestExposeHostPorts/single_port
2024/09/04 14:30:26 github.com/testcontainers/testcontainers-go - Connected to docker: 
  Server Version: 24.0.2
  API Version: 1.43
  Operating System: Docker Desktop
  Total Memory: 11962 MB
  Testcontainers for Go Version: v0.34.0
  Resolved Docker Host: unix:///var/run/docker.sock
  Resolved Docker Socket Path: /var/run/docker.sock
  Test SessionID: 3c51c5001b926adaaced72a4a825b61c3ebffe7f1b3ad54807d578681ea4072d
  Test ProcessID: 08686ed6-b490-4bc1-9362-863b409b46d6
2024/09/04 14:30:27 Failed to get image auth for https://index.docker.io/v1/. Setting empty credentials for the image: testcontainers/ryuk:0.9.0. Error is: credentials not found in native keychain
2024/09/04 14:30:29 🐳 Creating container for image testcontainers/ryuk:0.9.0
2024/09/04 14:30:30 ✅ Container created: ab05786adcf0
2024/09/04 14:30:30 🐳 Starting container: ab05786adcf0
2024/09/04 14:30:30 ✅ Container started: ab05786adcf0
2024/09/04 14:30:30 ⏳ Waiting for container id ab05786adcf0 image: testcontainers/ryuk:0.9.0. Waiting for: &{Port:8080/tcp timeout:<nil> PollInterval:100ms skipInternalCheck:false}
2024/09/04 14:30:30 🔔 Container is ready: ab05786adcf0
2024/09/04 14:30:30 🐳 Creating container for image testcontainers/sshd:1.2.0
2024/09/04 14:30:30 ✅ Container created: 7ea81f499603
2024/09/04 14:30:30 🐳 Starting container: 7ea81f499603
2024/09/04 14:30:30 ✅ Container started: 7ea81f499603
2024/09/04 14:30:31 ⏳ Waiting for container id 7ea81f499603 image: testcontainers/sshd:1.2.0. Waiting for: &{Port:22/tcp timeout:<nil> PollInterval:100ms skipInternalCheck:false}
2024/09/04 14:30:31 🔔 Container is ready: 7ea81f499603
2024/09/04 14:30:31 🐳 Creating container for image alpine:3.17
2024/09/04 14:30:31 ✅ Container created: 1282a054ff3e
2024/09/04 14:30:31 🐳 Starting container: 1282a054ff3e
2024/09/04 14:30:31 ✅ Container started: 1282a054ff3e
2024/09/04 14:30:31 🔔 Container is ready: 1282a054ff3e
2024/09/04 14:30:31 🐳 Terminating container: 1282a054ff3e
2024/09/04 14:30:31 🐳 Terminating container: 7ea81f499603
2024/09/04 14:30:31 🚫 Container terminated: 7ea81f499603
2024/09/04 14:30:31 🚫 Container terminated: 1282a054ff3e
=== RUN   TestExposeHostPorts/single_port_using_a_network
2024/09/04 14:30:31 🐳 Creating container for image testcontainers/sshd:1.2.0
2024/09/04 14:30:31 ✅ Container created: 739aac57801c
2024/09/04 14:30:31 🐳 Starting container: 739aac57801c
2024/09/04 14:30:31 ✅ Container started: 739aac57801c
2024/09/04 14:30:31 ⏳ Waiting for container id 739aac57801c image: testcontainers/sshd:1.2.0. Waiting for: &{Port:22/tcp timeout:<nil> PollInterval:100ms skipInternalCheck:false}
2024/09/04 14:30:32 🔔 Container is ready: 739aac57801c
2024/09/04 14:30:32 🐳 Creating container for image alpine:3.17
2024/09/04 14:30:32 ✅ Container created: 833b847b07d3
2024/09/04 14:30:32 🐳 Starting container: 833b847b07d3
2024/09/04 14:30:32 ✅ Container started: 833b847b07d3
2024/09/04 14:30:32 🔔 Container is ready: 833b847b07d3
2024/09/04 14:30:32 🐳 Terminating container: 833b847b07d3
2024/09/04 14:30:32 🐳 Terminating container: 739aac57801c
2024/09/04 14:30:32 🚫 Container terminated: 739aac57801c
2024/09/04 14:30:32 🚫 Container terminated: 833b847b07d3
=== RUN   TestExposeHostPorts/multiple_ports
2024/09/04 14:30:32 🐳 Creating container for image testcontainers/sshd:1.2.0
2024/09/04 14:30:32 ✅ Container created: 20c901dd98b7
2024/09/04 14:30:32 🐳 Starting container: 20c901dd98b7
2024/09/04 14:30:32 ✅ Container started: 20c901dd98b7
2024/09/04 14:30:32 ⏳ Waiting for container id 20c901dd98b7 image: testcontainers/sshd:1.2.0. Waiting for: &{Port:22/tcp timeout:<nil> PollInterval:100ms skipInternalCheck:false}
2024/09/04 14:30:32 🔔 Container is ready: 20c901dd98b7
2024/09/04 14:30:32 🐳 Creating container for image alpine:3.17
2024/09/04 14:30:33 ✅ Container created: a6d77527d929
2024/09/04 14:30:33 🐳 Starting container: a6d77527d929
2024/09/04 14:30:33 ✅ Container started: a6d77527d929
2024/09/04 14:30:33 🔔 Container is ready: a6d77527d929
2024/09/04 14:30:33 🐳 Terminating container: a6d77527d929
2024/09/04 14:30:33 🐳 Terminating container: 20c901dd98b7
2024/09/04 14:30:33 🚫 Container terminated: 20c901dd98b7
2024/09/04 14:30:33 🚫 Container terminated: a6d77527d929
=== RUN   TestExposeHostPorts/single_port_with_cancellation
2024/09/04 14:30:33 🐳 Creating container for image testcontainers/sshd:1.2.0
2024/09/04 14:30:33 ✅ Container created: 5db600520bf0
2024/09/04 14:30:33 🐳 Starting container: 5db600520bf0
2024/09/04 14:30:33 ✅ Container started: 5db600520bf0
2024/09/04 14:30:33 ⏳ Waiting for container id 5db600520bf0 image: testcontainers/sshd:1.2.0. Waiting for: &{Port:22/tcp timeout:<nil> PollInterval:100ms skipInternalCheck:false}
2024/09/04 14:30:33 🔔 Container is ready: 5db600520bf0
2024/09/04 14:30:33 🐳 Creating container for image alpine:3.17
2024/09/04 14:30:33 ✅ Container created: 5d2afc6f6fa5
2024/09/04 14:30:33 🐳 Starting container: 5d2afc6f6fa5
2024/09/04 14:30:34 ✅ Container started: 5d2afc6f6fa5
2024/09/04 14:30:34 🔔 Container is ready: 5d2afc6f6fa5
2024/09/04 14:30:45 🐳 Terminating container: 5d2afc6f6fa5
2024/09/04 14:30:45 🐳 Terminating container: 5db600520bf0
2024/09/04 14:30:45 🚫 Container terminated: 5db600520bf0
2024/09/04 14:30:45 🚫 Container terminated: 5d2afc6f6fa5
--- PASS: TestExposeHostPorts (18.87s)
    --- PASS: TestExposeHostPorts/single_port (5.12s)
    --- PASS: TestExposeHostPorts/single_port_using_a_network (0.98s)
    --- PASS: TestExposeHostPorts/multiple_ports (0.87s)
    --- PASS: TestExposeHostPorts/single_port_with_cancellation (11.89s)
PASS
ok  	github.com/testcontainers/testcontainers-go	19.328s

@mdelapenya Could you confirm this is expected and maybe a collateral effect of #2403?

@JulienBreux Could you confirm this is fixed and that we can proceed on microcks/microcks-testcontainers-go#12?

Thanks guys!

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

4 participants