0

I know I can create container with infinity loop:

val containerConfig = ContainerConfig.builder()
    .image(imageName)
    .cmd("sh", "-c", "while :; do sleep 1; done")
    .build()
val container = docker.createContainer(containerConfig)
docker.startContainer(container.id())

And then execute a command in this container using docker.execCreate and docker.execStart. Getting stdout and stderr in this case isn't a problem.

But I wonder if I can make things more simple. I want to execute command like this:

val containerConfig = ContainerConfig.builder()
    .image(imageName)
    .cmd(command)
    .build()
val container = docker.createContainer(containerConfig)
docker.startContainer(container.id())

and get access to stdout and stderr. Is it possible?

Okumo
  • 169
  • 1
  • 12

1 Answers1

0

Okay, there is a way to do that:

val containerConfig = ContainerConfig.builder()
    .image(imageName)
    .cmd(command)
    .build()
val container = docker.createContainer(containerConfig)
docker.startContainer(container.id())
docker.waitContainer(container.id())
val logs = docker.logs(container.id(), stdout(), stderr())
val output = logs.readFully()
Okumo
  • 169
  • 1
  • 12