Testcontainers-java: Allow command line arguments for

Created on 16 Oct 2018  路  13Comments  路  Source: testcontainers/testcontainers-java

We are currently trying to integrate OpenLDAP into our test suite using the osixia-image (https://github.com/osixia/docker-openldap).
We ran into a problem while creating the container.

There docs show how to get a container going when one needs to load a custom LDIF.

docker run \
  --volume ./bootstrap.ldif:/container/service/slapd/assets/config/bootstrap/ldif/50-bootstrap.ldif \
  osixia/openldap:1.2.2 --copy-service

The tricky part is the --copy-service at the end.
It doesn't look as if there is a way to specify arguments for the command this way in testcontainers.

Is there a reason for this or am I missing something (I hope I didn脰t overlook anything in the docs).

And as a sidenote:
Testcontainers is amazing, we were able to increase the number of intergration tests tremendously thanks to your project!

typquestion

Most helpful comment

Aaaargh, ok.
Thanks for clearing that one up ...
Sorry for the confusion and thanks for teaching me the correct wording in Docker!

Works as expected.

All 13 comments

Hi @codepitbull!

Thanks for the sidenote! 馃憤 鈽猴笍

You can do it with .withCmd("--copy-service") :)

@codepitbull Do you think we are lacking docs in this regard?

I am sorry, I didn't realize withCmd would extend the existing command. I was under the impression it would replace the command running in the container.
I just figured it out when playing around with it and checking how the resulting command in docker looked like.
That wrong impression came from teh docs here:
https://github.com/testcontainers/testcontainers-java/blob/master/docs/usage/options.md

By default the container will execute whatever command is specified in the image's Dockerfile. To override this, and specify a different command, use withCommand:
This sentence is rather misleading.

So my original problem is solved :)

Fun fact we thought the same when reading the documentation a few weeks and wanted to raise an issue but then looked at the source. So yes the documentation is very misleading

@codepitbull @atomfrede
Just to be clear, is there a confusion between CMDand ENTRYPOINT?
withCommand should indeed override the specified CMD in the Dockerfile, if I've read the code correctly (haven't tried it out).

And thanks for the sidenote again @codepitbull 鉂わ笍

Just to make it clear:
withCommand doesn't work as you describe it.
My code does a simple .withCommand("--option") and the --optiongets added to the command, it is not overridden as the documentation suggests.

Which command you are talking about? What is the assumed default CMD of the image to which you are appending your command?

In your initial example, your only command is --copy-service, so you are basically setting it this way, which seems to be expected?

The default command of the container is /container/tool/run.

I use the following combination to run the container:

new GenericContainer("osixia/openldap:1.2.1")
              .withExposedPorts(389, 636)
              .withEnv(environment)
              .withCommand("--copy-service")
              .withClasspathResourceMapping(ldif, "/container/service/slapd/assets/config/bootstrap/ldif/custom", BindMode.READ_ONLY);

According to the description I'd expect the container to fail, as I override the command to --copy-service.
Instead I see that the command is expanded to /container/tool/run --copy-service.
That is exactly what I wanted but I didn脰t realize from the description that this would be the outcome :)

You are talking about the Entrypoint here, see https://github.com/osixia/docker-light-baseimage/blob/stable/image/Dockerfile.

When the container is launched, it executes entrypoint + command (this is Docker in general and has nothing to do with Testcontainers).

So I'd say, works as expected 馃槄

Aaaargh, ok.
Thanks for clearing that one up ...
Sorry for the confusion and thanks for teaching me the correct wording in Docker!

Works as expected.

I'd like to add that I was also confused by the withCommand usage as well.

My scenario is that I wanted to emulate the Docker run command:

docker run \
  -p 8002:9080 \
  rodolpheche/wiremock:2.6.0 \
  --port 9080

Looking at the Dockerfile, I assumed that withCommand would override this line of the Dockerfile:

CMD java $JAVA_OPTS -cp /var/wiremock/lib/*:/var/wiremock/extensions/* com.github.tomakehurst.wiremock.standalone.WireMockServerRunner

which would be very clunky to override/maintain.

I think the subtle difference I missed was the CMD directive of the Dockerfile, and the COMMAND argument for docker run:

$ docker run --help

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

In my case:

  • OPTIONS="-p 8002:9080"
  • IMAGE="rodolpheche/wiremock:2.6.0"
  • COMMAND="--port 9080

So user error in the end, but still confusing due to subtle differences. More of a Docker problem than a Testcontainers problem though

the docs could point out to not confuse the two things 馃槅

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dabraham02124 picture dabraham02124  路  3Comments

denis-zhdanov picture denis-zhdanov  路  3Comments

itudoben picture itudoben  路  3Comments

oneiros-de picture oneiros-de  路  3Comments

naderghanbari picture naderghanbari  路  3Comments