dockerspotify-docker-client

Spotify docker client: How to get stdout and stderr of started container?


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?


Solution

  • 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()