1

I run tests in a docker container. As a part of this process, I need to spin up another docker container. I use testscontainers-go to do it. The docker container starts just fine but I can't talk to it.

     req := testcontainers.ContainerRequest{
        Image:        "my-image",
        ExposedPorts: []string{"9000:9000"},        
        SkipReaper: true,
        WaitingFor: wait.
            ForLog("Server started").
            WithPollInterval(time.Second * 10).
            WithStartupTimeout(time.Minute * 10),
        RegistryCred: getRegistryCred(),
        Hostname:     "server",
    }

From the test I try to access the new container using http://server:9000 but it is not there. Is there any way to assign a hostname to the child container?

Artur Shamsutdinov
  • 3,127
  • 3
  • 21
  • 39
  • try mount inside "/var/run/docker.sock" file to outside docker container – 袁文涛 Jun 23 '22 at 02:02
  • @袁文涛 sorry, can you please elaborate on that? I use `/var/run/docker.sock:/var/run/docker.sock` for the outer container to have access to Docker. Do I need the same volume for the internal container as well? – Artur Shamsutdinov Jun 23 '22 at 02:26
  • Simplest option generally is to run both containers on a [user-defined bridge network](https://docs.docker.com/network/bridge/) then they can just use DNS to find each other. However from your question it looks like you are starting a container from within another container and this may complicate things (please provide a bit more info re how you are accomplishing this). – Brits Jun 23 '22 at 02:47
  • @Brits yes, you are right. This is the current situation. I start one container, run tests in it and then, start another container from the test. – Artur Shamsutdinov Jun 23 '22 at 02:49
  • Well if they are on the same network you can use [`Endpoint`](https://pkg.go.dev/github.com/testcontainers/testcontainers-go#DockerContainer.Endpoint) but it's probably easier to use [`ContainerRequsets.Network`](https://pkg.go.dev/github.com/testcontainers/testcontainers-go#ContainerRequest) to select the network your other container and then use DNS. Of course you could always access it [via the host](https://stackoverflow.com/a/24326540/11810946). If you show how you connect to the docker daemon that will make it easier to understand what will work. – Brits Jun 23 '22 at 03:55

0 Answers0