docker-java 运行 带有 -rm 标志的容器

docker-java run container with -rm flag

我正在使用 docker-java 生成新容器。 我想在完成后删除容器。 有没有办法用docker-java来实现这个?

所以我基本上想要

docker run --rm my-docker

和docker-java.

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();