Created
March 1, 2019 07:19
-
-
Save skanehira/3b2fe63194d29db91d4cb5f2b5bdbbfe to your computer and use it in GitHub Desktop.
Use docker sdk to tail container logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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