1

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?

David Maze
  • 130,717
  • 29
  • 175
  • 215
xue xue
  • 11
  • 2
  • The Docker API doesn't have "run in the foreground" as an option, and so the various Docker SDKs frequently don't either. "Keep the container running" isn't an option _per se_, but if the container exits it means the process inside the container is exiting too. Have you looked at things like the `docker logs` of the container that gets started (could be from outside your application)? Have you verified the `image` runs independently with these options (`docker run -it image`)? – David Maze Sep 10 '20 at 11:17
  • @DavidMaze thanks your attention,my image's `docker-entrypoint.sh` just do `echo`. I change `nginx:latest` image, container keep running. When i add `while [1]` in my `docker-entrypoint.sh`, container keep running too, but `execStartCmd` is no effect. As if i should write a event handling service. I am confused. – xue xue Sep 10 '20 at 11:57
  • @xuexue were you able to solve this problem? – Blitz Nov 24 '21 at 18:38

0 Answers0