3

In our J2EE project, we want to call sjourdan/ffmpeg docker through the docker-java library.

A typical call for ffmpeg conversion will be something like:

docker run --rm -v /e/data:/tmp/workdir sjourdan/ffmpeg -i /tmp/workdir/test.mov -f mp4 -vcodec libx264 -acodec aac /tmp/workdir/test.mp4

We managed all of that with a DockerClient.createContainerCmd() and the right .with() methods, except for the --rm argument.

Is there a way to add it through docker-java?

Xavier Portebois
  • 3,354
  • 6
  • 33
  • 53

3 Answers3

2

According to this other StackOverflow question/answer, --rm is not handled by the Docker API, so we got no luck with docker-java either.

So, in the end we carefully remove the container after the execution, something like:

dockerClient.startContainerCmd(container.getId()).exec();
// do some stuff
dockerClient.removeContainerCmd(container.getId()).withForce(true).exec();
Xavier Portebois
  • 3,354
  • 6
  • 33
  • 53
2

You can send "AutoRemove" value to docker API:

String containerId = dockerClient.createContainerCmd(image).
    withHostConfig(new HostConfig() {
        @JsonProperty("AutoRemove")
        public boolean autoRemove = true;
    }).exec().getId();
dockerClient.startContainerCmd(containerId).exec();

That's it

Docker API info https://docs.docker.com/engine/api/v1.37/#operation/ContainerCreate

"AutoRemove" was accepted, at least, starting at v1.25

Ricard Nàcher Roig
  • 1,271
  • 12
  • 14
1

For the current version, which is 3.2.8, you can set the autoremove option with:

dockerClient.createContainerCmd(image).withHostConfig(new HostConfig().withAutoRemove(true)).exec()
Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42