Merge pull request #129 from docker/errors
Handle start errors sync with runc
This commit is contained in:
commit
41d041270b
3 changed files with 47 additions and 5 deletions
|
@ -12,15 +12,12 @@ import (
|
|||
"github.com/docker/docker/pkg/term"
|
||||
)
|
||||
|
||||
var runtimeLog string
|
||||
|
||||
func setupLogger() {
|
||||
f, err := os.OpenFile("/tmp/shim.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logrus.SetOutput(f)
|
||||
runtimeLog = "/tmp/runtime.log"
|
||||
}
|
||||
|
||||
// containerd-shim is a small shim that sits in front of a runtime implementation
|
||||
|
|
|
@ -84,7 +84,11 @@ func (p *process) start() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args := []string{"--log", runtimeLog}
|
||||
logPath := filepath.Join(cwd, "log.json")
|
||||
args := []string{
|
||||
"--log", logPath,
|
||||
"--log-format", "json",
|
||||
}
|
||||
if p.state.Exec {
|
||||
args = append(args, "exec",
|
||||
"--process", filepath.Join(cwd, "process.json"),
|
||||
|
@ -140,7 +144,6 @@ func (p *process) start() error {
|
|||
}
|
||||
p.containerPid = pid
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (p *process) pid() int {
|
||||
|
|
|
@ -2,7 +2,9 @@ package runtime
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
@ -308,6 +310,20 @@ func waitForStart(p *process, cmd *exec.Cmd) error {
|
|||
return err
|
||||
}
|
||||
if !alive {
|
||||
// runc could have failed to run the container so lets get the error
|
||||
// out of the logs
|
||||
messages, err := readLogMessages(filepath.Join(p.root, "log.json"))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return ErrContainerNotStarted
|
||||
}
|
||||
return err
|
||||
}
|
||||
for _, m := range messages {
|
||||
if m.Level == "error" {
|
||||
return errors.New(m.Msg)
|
||||
}
|
||||
}
|
||||
return ErrContainerNotStarted
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
@ -362,3 +378,29 @@ func (o *oom) Close() error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type message struct {
|
||||
Level string `json:"level"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func readLogMessages(path string) ([]message, error) {
|
||||
var out []message
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
dec := json.NewDecoder(f)
|
||||
for {
|
||||
var m message
|
||||
if err := dec.Decode(&m); err != nil {
|
||||
if err == io.EOF {
|
||||
return out, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue