Hi,
I am starting a Docker-Compose with (in Kotlin):
@ClassRule
@JvmField
val env: DockerComposeContainer =
DockerComposeContainer(composeId, File("../docker-compose.yml"))
.withLocalCompose(true)
this works just fine. However, I want to add a Chrome Docker image to control with TestContainer, which should be on the same network of Docker Compose. In docker-compose.yml I specify the name of the default network manually (to make sure the name is deterministic with no random strings added to it), ie:
networks:
default:
name: foo-network
I then try to start Chrome with:
@Rule
@JvmField
val browser: BrowserWebDriverContainer = BrowserWebDriverContainer()
.withDesiredCapabilities(DesiredCapabilities.chrome())
.withNetworkMode("foo-network")
However, that does not work. When I use docker inspect on the Chrome image, the setting foo-network is completely ignored, and I get a new network with a random name, eg:
"Networks": {
"7af40cfa-f262-463c-accb-1707bdd8f2d9": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"tc-qEF38kvs",
"a8bd32b1f2f3"
],
"NetworkID": "96313abc29bedbb992f754ba6684a3c8847bcd7f8bf9af547ce6fb38b0152a91",
"EndpointID": "6851d5ba06dda964677f57263a03806c47aea7d3a021fb2e71b3f14fe895664f",
"Gateway": "192.168.48.1",
"IPAddress": "192.168.48.2",
"IPPrefixLen": 20,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:c0:a8:30:02",
"DriverOpts": null
}
}
Is there a better way to connect BrowserWebDriverContainer to an existing Docker Compose network? And in any case, if how I use withNetworkMode is wrong, it would be good if TestContainer threw an exception with some info.
TestContainer version: 1.9.1
Docker version: 18.06.1
OS: Mac 10.12.6
Hi @arcuri82,
Your usage of withNetworkMode is indeed slightly wrong. You would need to provide the id of the network, not the name, then it will work.
You should be able to retrieve the network id by using listNetworksCmd() from DockerClient.
I however see the use case of setting the network by name, would be quite convenient for the compose use case.
many thanks for the very quick response! ;)
btw, the JavaDocs of withNetworkMode states:
Parameters:
networkMode - network mode, e.g. including 'host', 'bridge', 'none' or the name of an existing named network.
so, if it requires the id and not name, I guess what stated in JavaDoc is wrong? And throwing an IllegalArgumentException (or IllegalStateException when Container is started later) would be good if the id does not exist
I checked the Docker CLI docs directly and found this part:
--network="bridge" : Connect a container to a network
'bridge': create a network stack on the default Docker bridge
'none': no networking
'container:<name|id>': reuse another container's network stack
'host': use the Docker host network stack
'<network-name>|<network-id>': connect to a user-defined network
We are just delegating to docker-java's withNetworkMode, so either there is a bug in docker-java, or there is something else wrong with your example.
Can you please check the name of the created network in your docker-compose example?
Maybe the network becomes the name $composeProjectName_foo-network.
Hi,
to double-check if it was an OS issue or local misconfiguration, I ran the same example on another machine in Windows 10, and getting exactly the same problem.
By using docker inspect, I do verify that all the images running in Docker-Compose have the right network name, exactly as specified in docker-compose.yml. On the other hand, the image of Selenium+Chrome is having a random name.
If it helps, the example I am working on is in an open-source project , and the test can be found here. Note: besides JDK 8, it requires NPM and NodeJS. But these latter can be skipped by removing the frontend module.
Thanks a lot for your example, I'll give it a look. Maybe there is some bug in docker-java regarding network name.
It doesn't look like a bug in docker-java. Seems that this is caused by
https://github.com/testcontainers/testcontainers-java/blob/94c903d755c89340aecb7eeb8f77834c6c88e095/modules/selenium/src/main/java/org/testcontainers/containers/BrowserWebDriverContainer.java#L143-L145
and
https://github.com/testcontainers/testcontainers-java/blob/43a76abb5d27f322a6a7a30ffc148beb31cd5736/core/src/main/java/org/testcontainers/containers/GenericContainer.java#L687-L692
GenericContainer will ignore the networkMode when network is set. BrowserWebDriverContainer seems to set network by default (unless recordingMode is set to SKIP).