testcontainers/testcontainers-node

Cannot define container hostname

Closed this issue · 5 comments

When I run docker image with -h --hostname I can define container hostname. In Testcontainers NodeJS I can not define host (but in go I can)

Why is this needed? Just curious.

By default, Docker assigns a random hostname to a container if you don't specify one explicitly. This hostname is usually derived from the container ID.

I wonder if this interacts badly with the resolveHost logic in the Localstack module, though I'm probably confusing internal and external host names.

Why is this needed?

  1. YDB daemon cannot start when localhost is specified in the configs, and the node (in this case, the container) has a different hostname.

  2. If you use a host network, then the server that is bound to container hostname cannot respond to requests from localhost, it will expect container hostname. (ex. Golang: http.ListenAndServe("localhost:8080", nil))

I'm not sure about this one. There's a lot of logic which ensures that the container hostname is derived correctly. You can get the hostname via container.getHost(), and you should override your configs which are hardcoding localhost with its value. If tomorrow you run your test in a container, or in some other CI environment, the host may no longer be localhost anymore and your tests will break.

You can get the hostname via container.getHost()

I don't need it, I need the container's hostname to match the config. And the only way to record always single execution in different environments is to run the container via docker -h/--hostname localhost.

I can only pass configs as mount and only before launching my ydb daemon, and what is in the config should match reality (hostname -h).

I can only pass configs as mount and only before launching my ydb daemon

This is more a limitation of the setup. It should be possible to do whatever you like before your tests start, and inject whatever is needed into the environment or wherever else YBD reads from. Telling the container its host is something it isn't is just going to cause more problems down the line.

I'll close this issue as it doesn't seem to be something to recommend and it's the first I've heard of it being an issue. Will reopen in future if it's necessary.

In Testcontainers NodeJS I can not define host (but in go I can)

FYI TC-go forwards you the entire Docker API, so you can do whatever you want. TC-node does not follow this practice. If absolutely necessary, TC-node gives you access to a container runtime getContainerRuntime() where you can interact with images/containers/networks however you want, not following the GenericContainer APIs.