1

I am using docker-java to spawn new containers. I want to remove the containers after they are finished. Is there a way to achieve this with docker-java?

So I basically want something like

docker run --rm my-docker

with docker-java.

1
  • I am aware of the remove container function but I would prefer the to have this done automatically after the container stopped. Commented Jan 11, 2020 at 10:50

1 Answer 1

5

In the Docker HTTP API, the docker run --rm option translates to an AutoRemove option inside a HostConfig object. The Java API mirrors this object layout. The docker-java wiki doesn't have any good examples of using that object, but it's in the Java API too.

import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.model.HostConfig;

HostConfig hostConfig = HostConfig
  .newHostConfig()
  .withAutoRemove(true);             // Set the "remove" flag

CreateContainerResponse container = dockerClient
  .createContainerCommand("busybox")
  .withHostConfig(hostConfig)        // Add in the HostConfig object
  .exec();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.