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: kafka - doesn't work when using different DOCKER_HOST unless ADV_HOST is overloaded #1033

Open
karelbilek opened this issue Apr 23, 2024 · 4 comments

Comments

@karelbilek
Copy link
Contributor

Describe the bug
If you are using different DOCKER_HOST than localhost, kafka gnomock doesn't work.

To Reproduce

Expected behavior
Everything works

System (please complete the following information):

  • Gitlab CI, in my case

Additional context
The whole issue is caused by ADV_HOST being hard-coded to 127.0.0.1 in the preset.

The solution was, for me, write this at the start of the test:

	dockerIP := ""
	if dh := os.Getenv("DOCKER_HOST"); dh != "" {
		u, err := url.Parse(dh)
		if err == nil {
			if host := u.Hostname(); host != "" {
				ips, err := net.LookupIP(host)
				if err != nil {
					t.Fatalf("Could not get IPs: %v\n", err)
				}
				dockerIP = ips[0].String()
			}
		}
	}

And later:

	opts := []gnomock.Option{
		gnomock.WithDebugMode(), gnomock.WithLogWriter(os.Stdout),
		gnomock.WithContainerName("kafka"),
	}
	if dockerIP != "" {
		opts = append(opts, gnomock.WithEnv("ADV_HOST="+dockerIP))
	}
	container, err := gnomock.Start(
		p,
		opts...,
	)

This should be either set automatically (I am not sure why is the localhost IP hardcoded, as gnomock does know that I am using DOCKER_HOST) or, at least, documented

@karelbilek
Copy link
Contributor Author

The hardcoded localhost is here

gnomock.WithEnv("ADV_HOST=127.0.0.1"),

@karelbilek
Copy link
Contributor Author

Note, you cannot use the DOCKER_HOST directly, you need to first resolve it if it's not an IP address, otherwise I get error replication-factor is invalid (not sure what that means)

@orlangure
Copy link
Owner

Thanks for reporting this @karelbilek.
Do you think the code you suggested can be merged into the Kafka preset, instead of being used externally in the test?

@Ocramius
Copy link

Ocramius commented Feb 13, 2025

Heyo, I just ran into this same issue as well.

The problem is that DOCKER_HOST is obviously not 127.0.0.1 for me, and in my case, I'm running the command from a docker-compose environment where networks: are defined too, further restricting interactions.

My setup roughly:

services:
  - docker:latest-dind # no, I don't actually use `latest` :-P 

integration-test:
  script:
    - docker compose run --rm my-service ./run-tests.sh

My docker-compose.yml is roughly:

services:
  my-service:
    image: some-image
    networks:
      - my-network

networks:
  my-network:
    driver: bridge

In this setup, I'm having a hard time figuring how the docker:dind DOCKER_HOST should be passed down to docker-compose.yml and further down to my-service.

In fact, assuming I can resolve DOCKER_HOST to an IP upfront: isn't that IP inaccessible from within my-service, given the different networks?

EDIT: closely related: https://github.com/testcontainers/testcontainers-go/blob/f09b3af2cb985a17bd2b2eaaa5d384882ded8e28/docs/system_requirements/ci/gitlab_ci.md

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

No branches or pull requests

3 participants