ory/hydra连接被拒绝 Testcontainers for Go

huangapple go评论60阅读模式
英文:

ory/hydra connection refused Testcontainers for Go

问题

我在另一个Docker容器中连接到Hydra时遇到了问题。我总是得到以下错误信息:

> Post "http://test-hydra:4445/oauth2/introspect": dial tcp 172.19.0.3:4445: connect: connection refused

而且Hydra容器中没有日志。

我还尝试了使用HTTPS,但是得到了相同的错误。

但是我可以通过映射的端口从容器外部访问Hydra。

我还检查了网络docker network inspect test-network,容器在同一个网络中,IP地址也是正确的。

可能出了什么问题?

以下是启动Hydra容器的代码:

func (c ServiceConfig) StartHydraContainer(ctx context.Context) (url string) {

	container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
		ContainerRequest: tc.ContainerRequest{
			Image:    "oryd/hydra:v1.10.6",
			Networks: []string{"test-network"},
			NetworkAliases: map[string][]string{
				networkName: {"test-hydra"},
			},
			Env:          c.env(),
			ExposedPorts: []string{"4444/tcp", "4445/tcp"},
			Cmd:          []string{"serve", "all"},
			WaitingFor:   wait.ForLog("Thank you for using ORY Hydra v1.10.6!"),
		},
		Started: true,
	})

	if err != nil {
		log.Fatal("Hydra start error:", err)
	}

	return fmt.Sprintf("https://test-hydra:4445")
}

这是服务容器的代码:

func (t ServiceConfig) StartContainer(ctx context.Context) string {
	dir, _ := os.Getwd()

	_, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
		ContainerRequest: tc.ContainerRequest{
			FromDockerfile: tc.FromDockerfile{Context: filepath.Join(dir, "Service")},
			Networks:       []string{"test-network"},
			NetworkAliases: map[string][]string{networkName: {"test-service"}},
			Env:            t.env(),
			ExposedPorts:   []string{"9000/tcp"},
			WaitingFor:     wait.ForExposedPort(),
		},
		Started: true,
	})
	if err != nil {
		log.Fatal(err)
	}

	return fmt.Sprintf("test-service:9000")
}

func (t ServiceConfig) env() map[string]string {
	return map[string]string{"OIDC_PROVIDER_ADDRESS": "https://test-hydra:4445"}
}
英文:

I have problem connecting to Hydra from a other docker container. I always get

> Post "http://test-hydra:4445/oauth2/introspect": dial tcp 172.19.0.3:4445: connect: connection refused

And there is no log in Hydra container.

Also I tried https and getting same error.

But I was able to reach Hydra outside of container using mapped port.

I also have checked the network docker network inspect test-network and containers are in the same network and IP is correct.

What could be wrong?

func (c ServiceConfig) StartHydraContainer(ctx context.Context) (url string) {

	container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
		ContainerRequest: tc.ContainerRequest{
			Image:    "oryd/hydra:v1.10.6",
			Networks: []string{"test-network"},
			NetworkAliases: map[string][]string{
				networkName: {"test-hydra"},
			},
			Env:          c.env(),
			ExposedPorts: []string{"4444/tcp", "4445/tcp"},
			Cmd:          []string{"serve", "all"},
			WaitingFor:   wait.ForLog("Thank you for using ORY Hydra v1.10.6!"),
		},
		Started: true,
	})

	if err != nil {
		log.Fatal("Hydra start error:", err)
	}

	return fmt.Sprintf("https://test-hydra:4445")
}

This is the service container:

func (t ServiceConfig) StartContainer(ctx context.Context) string {
	dir, _ := os.Getwd()

	_, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
		ContainerRequest: tc.ContainerRequest{
			FromDockerfile: tc.FromDockerfile{Context: filepath.Join(dir, "Service")},
			Networks:       []string{"test-network"},
			NetworkAliases: map[string][]string{networkName: {"test-service"}},
			Env:            t.env(),
			ExposedPorts:   []string{"9000/tcp"},
			WaitingFor:     wait.ForExposedPort(),
		},
		Started: true,
	})
	if err != nil {
		log.Fatal(err)
	}

	return fmt.Sprintf("test-service:9000")
}

func (t ServiceConfig) env() map[string]string {
	return map[string]string{"OIDC_PROVIDER_ADDRESS": "https://test-hydra:4445"}
} 

答案1

得分: 1

问题出在等待策略上:当请求发送时,WaitingFor中的hydra还没有准备好。

这是新的策略:
WaitingFor: wait.ForHTTP("/clients").WithPort("4445/tcp"),

英文:

The problem was in waiting strategy: WaitingFor the hydra was not ready when the request was sent.

This is the new strategy:
WaitingFor: wait.ForHTTP("/clients").WithPort("4445/tcp"),

huangapple
  • 本文由 发表于 2023年4月19日 10:27:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76050334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定