简体   繁体   中英

docker-java run container with -rm flag

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM