#docker clinent api实现docker exec
package main
import (
"log"
"os"
docker "github.com/fsouza/go-dockerclient"
)
func main() {
client, err := docker.NewClient("unix:///var/run/docker.sock")
if err != nil {
log.Fatal(err)
}
createOpts := docker.CreateExecOptions{}
createOpts.AttachStdin = true
createOpts.AttachStdout = true
createOpts.AttachStderr = true
createOpts.Tty = true
createOpts.Cmd = []string{"sh", "-c", "echo '\x21\x21' > /tmp/aaa2"}
createOpts.Container = "7da04dd8b7b8"
exec, err := client.CreateExec(createOpts)
if err != nil {
log.Fatalf("create exec error: %v\n", err)
}
// start exec
startOpts := docker.StartExecOptions{}
startOpts.Tty = true
startOpts.RawTerminal = true
startOpts.Detach = false
startOpts.ErrorStream = os.Stderr
startOpts.InputStream = os.Stdin
startOpts.OutputStream = os.Stdout
err = client.StartExec(exec.ID, startOpts)
if err != nil {
log.Fatalf("start exec error: %v\n", err)
}
}