how to start docker container with detach for docker-java?

312 Views Asked by At

I am trying to run a docker image using the following code. But the container is exiting immediately after starting. I don't find -d params at creating container. how to do?

HostConfig hostConfig = new HostConfig();
DefaultDockerClientConfig.Builder config = DefaultDockerClientConfig.createDefaultConfigBuilder();
DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();
CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(image);
createContainerCmd.withTty(true)
.withAttachStdout(true)
.withStdinOpen(true)
.withAttachStdout(true)
.withAttachStderr(true)
.withAttachStdin(true)
.withName(containerName)
.withHostConfig(hostConfig);
String containerId = createContainerCmd.exec().getId();
dockerClient.startContainerCmd(containerId).exec();

Could anybody help me to understand the how to keep the container up and running?

0

There are 0 best solutions below