Skip to content

Instantly share code, notes, and snippets.

@skanehira
Created March 1, 2019 07:19
Show Gist options
  • Save skanehira/3b2fe63194d29db91d4cb5f2b5bdbbfe to your computer and use it in GitHub Desktop.
Save skanehira/3b2fe63194d29db91d4cb5f2b5bdbbfe to your computer and use it in GitHub Desktop.
Use docker sdk to tail container logs
package main
import (
"context"
"log"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
)
func run() int {
c, err := client.NewClientWithOpts(client.WithVersion("1.39"))
if err != nil {
log.Println(err)
return 1
}
r, err := c.ContainerLogs(context.Background(), "657814ba450f", types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
if err != nil {
log.Println(err)
return 1
}
_, err = stdcopy.StdCopy(os.Stdout, os.Stderr, r)
if err != nil {
log.Println(err)
return 1
}
return 0
}
func main() {
run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment