ory/hydra connection refused Testcontainers for Go

214 Views Asked by At

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

There are 1 best solutions below

0
On

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"),