Test containers: ignore parent `EXPOSE` instruction from Dockerfile Test containers: ignore parent `EXPOSE` instruction from Dockerfile docker docker

Test containers: ignore parent `EXPOSE` instruction from Dockerfile


Can I somehow force Test containers to ignore EXPOSE instruction?

I don't know if there is a simple configuration option for this, but a workaround solution I found is to use an advanced feature of the docker-java create container command customization. I'm providing an example in Java, translate it to Scala yourself, please. Apply it as the last command before returning a container object from your function:

container.withCreateContainerCmdModifier(    cmd -> cmd.getHostConfig().withPublishAllPorts(false));

The main point here is the usage of .withPublishAllPorts(false). From my understanding, this is the same as --publish-all (or -P) arguments of the docker run command. Testcontainers library sets this value to true by default. This modification overrides it to false.

With this configuration no ports are published at all for your example, not the 5 fixed as expected:

CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS          PORTS                                                        NAMES2ee4fb91b97c   couchbase:community-5.0.1   "/entrypoint.sh couc…"   33 seconds ago   Up 32 seconds   8091-8094/tcp, 11207/tcp, 11210-11211/tcp, 18091-18094/tcp   trusting_keldysh

This is because in the answer you provided, the author created a special custom docker image of couchbase, which "understands" environment variables like COUCHBASE_RANDOM_PORT_8091. Your code uses the standard couchbase image couchbase:community-5.0.1, which basically just ignores these environment variables. So in order to run counchbase on not standard internal ports, you need to build a custom image with the "magic" configure-node.sh script, which tunes couchbase config using values provided in environment variables.

I hope it helps anyhow :)