3

In our J2EE project, we're doing some video conversion by calling sjourdan/ffmpeg docker from our java code, relying on docker-java.

For now, it looks something like this (quite simplified for brevity):

CreateContainerCmd createCommand = dockerClient.createContainerCmd("sjourdan/ffmpeg")
    .withVolumes(aVolume)
    .withBinds(aBind)
    .withCmd("a lot of options about the conversion itself, codec, ratio, …");
CreateContainerResponse container = createCommand.exec();

dockerClient.startContainerCmd(container.getId()).exec();

// vanilla implementation of ResultCallback
MyResultCallback callback = new MyResultCallback();
dockerClient.waitContainerCmd(container.getId()).exec(callback);

Fact is, of course, the thread keeps going after that without waiting the callback to be called back.

How should we do to force the thread to wait until the callback is called, informing us the docker command is fully ended?


On another note, if someone knows how to add a --rm parameter to the played docker run command using docker-java, I'm interested.

Xavier Portebois
  • 3,354
  • 6
  • 33
  • 53

2 Answers2

3

Well, if anyone needs a solution, here what we implemented at the end:

CountDownLatch latch = new CountDownLatch(1);
MyResultCallback callback = new MyResultCallback(latch);
dockerClient.waitContainerCmd(container.getId()).exec(callback);
return latch.await(5, TimeUnits.MINUTES);

and the MyResultCallback.onComplete looks like

public void onComplete()
{
    latch.countDown();
}

Seems a nice enough way to handle the waiting from the main thread.

Xavier Portebois
  • 3,354
  • 6
  • 33
  • 53
1

There is a built-in solution today:

var callback = new WaitContainerResultCallback();
docker.waitContainerCmd(container.getId()).exec(callback);
callback.awaitStarted();

The thread will be blocked until the contain is started.

Samuel Zhang
  • 1,290
  • 8
  • 14