4

I am trying to use the docker remote API from java using docker-java and I'm struggling to find a way to see when the execution of my container has finished:

 CreateContainerResponse container = dockerClient.createContainerCmd("me/vw:v0.1")
                .withVolumes(workingDIR)
                .withBinds(new Bind("/localDrive", workingDIR))
                .withCmd("vw", "/rcv1/rcv1.train.vw.gz", "--cache_file", "/rcv1/cache_train", "-f", "/rcv1/r_temp")
                .withLogConfig(logconf)
                .exec();
        dockerClient.startContainerCmd((container.getId())).exec();

        Thread.sleep(10000); //OBVIOUSLY BROKEN HACK IS BROKEN
        System.out.println("PRINTING LOGS");
        System.out.println(containerLog(container.getId(), dockerClient));

What is the normal way to check if container is finished? Do I have to write something that polls the docker server, or do is there some convenient blocking function/invocation? docker-java has really bad documentation and most of what I've gotten to work is from looking at the test cases.

Cheers,

Nick

XapaJIaMnu
  • 1,408
  • 3
  • 12
  • 28
  • What exactly are you trying to do? Why do you want your container to finish? – Matthew Aug 23 '16 at 08:02
  • I'm trying to package self contained applications inside a docker image, so that they can be called as if they were local unix programs, but from java. Does this make sense? – XapaJIaMnu Aug 23 '16 at 09:40
  • Hmm. I think you need to have your docker client library wait for completiton. [This example](https://github.com/docker-java/docker-java/blob/master/src/test/java/com/github/dockerjava/core/command/AttachContainerCmdImplTest.java) calls `awaitCompletion` which looks promising. – Matthew Aug 23 '16 at 09:50
  • Thanks, that looks like what I am looking for. `docker-java` doesn't have the greatest documentation so it will have some time to understand what happens... – XapaJIaMnu Aug 23 '16 at 09:53

3 Answers3

6
dockerClient.startContainerCmd(createContainerResponse.getId()).exec();
WaitContainerResultCallback resultCallback = new WaitContainerResultCallback();
dockerClient.waitContainerCmd(createContainerResponse.getId()).exec(resultCallback);
resultCallback.awaitCompletion();
dockerClient.removeContainerCmd(createContainerResponse.getId()).exec();
Dmytro Titov
  • 2,802
  • 6
  • 38
  • 60
  • How to "`awaitCompletion`" in `removeContainerCmd` method? I'm facing this issue: I want to remove a container and wait be removed before starts a new one. Do you know how to do that? – Odravison May 03 '19 at 14:12
  • Why is it different from another commands? Just await for completion, as in the other cases. – Dmytro Titov May 04 '19 at 07:24
0

If you have access to the docker socket you can use the remote API. This is generally only used for admin since it requires full privileges.

Matthew
  • 10,361
  • 5
  • 42
  • 54
  • What exactly in the remote API gives me a blocking read? I am the admin and I have access to the docker socket. – XapaJIaMnu Aug 23 '16 at 07:59
  • I'm not an expert, but I doubt it has a blocking functionality. You'll have to write that. Though there's probably a python wrapper somewhere. – Matthew Aug 23 '16 at 08:01
  • Yeah I was asking if there's something available so that I don't have to write it myself. – XapaJIaMnu Aug 23 '16 at 09:40
0

Consider monitoring Docker system events, more here https://docs.docker.com/engine/api/v1.29/#operation/SystemEvents

You will need to create a separate thread and subscribe to the lifecycle of your newly created container. You will get a notification upon your container exit. Notify your main thread and you'll be all right. Laborious.

bioffe
  • 6,283
  • 3
  • 50
  • 65