Created
November 12, 2015 17:42
-
-
Save pwaller/b21e452e0d9ed38d8441 to your computer and use it in GitHub Desktop.
Docker attach bug.
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
func DockerRun(c *docker.Client, imageName string) (io.ReadCloser, error) { | |
log.Printf("Create container...") | |
cont, err := c.CreateContainer(docker.CreateContainerOptions{ | |
Config: &docker.Config{ | |
Hostname: "generateruntimecontext", | |
AttachStdout: true, | |
AttachStderr: true, | |
Image: imageName, | |
Labels: map[string]string{ | |
"orchestrator": "hanoverd", | |
"purpose": "Generate build context for runtime container", | |
}, | |
}, | |
}) | |
if err != nil { | |
log.Printf("Create container... failed: %v", err) | |
return nil, err | |
} | |
log.Printf("Start build context emitter") | |
err = c.StartContainer(cont.ID, &docker.HostConfig{}) | |
if err != nil { | |
log.Printf("Start container... failed: %v", err) | |
return nil, err | |
} | |
r, w := io.Pipe() | |
go func() { | |
log.Printf("AttachToContainer") | |
err := c.AttachToContainer(docker.AttachToContainerOptions{ | |
Container: cont.ID, | |
OutputStream: &WriteMonitor{w}, | |
ErrorStream: os.Stderr, | |
Logs: true, | |
Stdout: true, | |
Stderr: true, | |
Stream: true, | |
}) | |
log.Printf("~AttachToContainer") | |
// io.Pipe hardwired to never return error here. | |
_ = w.CloseWithError(err) | |
}() | |
removeContainer := func() { | |
err := c.RemoveContainer(docker.RemoveContainerOptions{ | |
ID: cont.ID, | |
RemoveVolumes: true, | |
Force: true, | |
}) | |
if err != nil { | |
log.Printf("Error removing intermediate container: %v", err) | |
} | |
} | |
log.Printf("DockerRun succeeded") | |
return struct { | |
io.Reader | |
io.Closer | |
}{ | |
Reader: r, | |
Closer: CloseFunc(func() error { | |
defer removeContainer() | |
log.Printf("Wait") | |
status, err := c.WaitContainer(cont.ID) | |
if err != nil { | |
return err | |
} | |
if status != 0 { | |
return fmt.Errorf("non-zero exit status: %v", err) | |
} | |
return nil | |
}), | |
}, err | |
} | |
type WriteMonitor struct{ io.Writer } | |
func (w *WriteMonitor) Write(bs []byte) (int, error) { | |
n, err := w.Writer.Write(bs) | |
log.Printf("Write() (%v, %v)", n, err) | |
return n, err | |
} | |
type CloseFunc func() error | |
func (fn CloseFunc) Close() error { return fn() } |
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
Nov 12 17:11:33 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5901]: 2015/11/12 17:11:33 Create container... | |
Nov 12 17:11:33 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5901]: 2015/11/12 17:11:33 Start build context emitter | |
Nov 12 17:11:33 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5901]: 2015/11/12 17:11:33 DockerRun succeeded | |
Nov 12 17:11:33 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5901]: 2015/11/12 17:11:33 Building... | |
Nov 12 17:11:33 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5901]: 2015/11/12 17:11:33 AttachToContainer | |
Nov 12 17:11:33 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5901]: 2015/11/12 17:11:33 Write() (1033, <nil>) | |
Nov 12 17:11:33 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5901]: 2015/11/12 17:11:33 Write() (1028, <nil>) | |
[many of these with different lengths] | |
Nov 12 17:11:33 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5901]: 2015/11/12 17:11:33 Write() (124, <nil>) | |
Nov 12 17:11:33 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5901]: 2015/11/12 17:11:33 Write() (55, <nil>) | |
[program hangs] |
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
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Create container... | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Start build context emitter | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 DockerRun succeeded | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Building... | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 AttachToContainer | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Write() (32768, <nil>) | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Write() (32768, <nil>) | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Write() (32768, <nil>) | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Write() (32768, <nil>) | |
[snip many of these] | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Write() (32768, <nil>) | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Write() (32768, <nil>) | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Write() (32768, <nil>) | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 Write() (6144, <nil>) | |
Nov 12 17:11:58 ip-10-1-170-104.eu-west-1.compute.internal hanoverd[5973]: 2015/11/12 17:11:58 ~AttachToContainer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment