Skip to content

Commit fb6ee86

Browse files
committed
save start error into State.Error
when a container failed to start, saves the error message into State.Error so that it can be retrieved when calling `docker inspect` instead of having to look at the log Docker-DCO-1.1-Signed-off-by: Daniel, Dao Quang Minh <[email protected]> (github: dqminh)
1 parent 5343ede commit fb6ee86

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

daemon/container.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ func (container *Container) Start() (err error) {
297297
// setup has been cleaned up properly
298298
defer func() {
299299
if err != nil {
300+
container.setError(err)
301+
container.toDisk()
300302
container.cleanup()
301303
}
302304
}()

daemon/state.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type State struct {
1515
Restarting bool
1616
Pid int
1717
ExitCode int
18+
Error string // contains last known error when starting the container
1819
StartedAt time.Time
1920
FinishedAt time.Time
2021
waitChan chan struct{}
@@ -137,6 +138,7 @@ func (s *State) SetRunning(pid int) {
137138
}
138139

139140
func (s *State) setRunning(pid int) {
141+
s.Error = ""
140142
s.Running = true
141143
s.Paused = false
142144
s.Restarting = false
@@ -179,6 +181,13 @@ func (s *State) SetRestarting(exitCode int) {
179181
s.Unlock()
180182
}
181183

184+
// setError sets the container's error state. This is useful when we want to
185+
// know the error that occurred when container transits to another state
186+
// when inspecting it
187+
func (s *State) setError(err error) {
188+
s.Error = err.Error()
189+
}
190+
182191
func (s *State) IsRestarting() bool {
183192
s.Lock()
184193
res := s.Restarting

integration-cli/docker_cli_start_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,44 @@ func TestStartAttachCorrectExitCode(t *testing.T) {
6868

6969
logDone("start - correct exit code returned with -a")
7070
}
71+
72+
func TestStartRecordError(t *testing.T) {
73+
defer deleteAllContainers()
74+
75+
// when container runs successfully, we should not have state.Error
76+
cmd(t, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
77+
stateErr, err := inspectField("test", "State.Error")
78+
if err != nil {
79+
t.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
80+
}
81+
if stateErr != "" {
82+
t.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
83+
}
84+
85+
// Expect this to fail and records error because of ports conflict
86+
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top"))
87+
if err == nil {
88+
t.Fatalf("Expected error but got none, output %q", out)
89+
}
90+
stateErr, err = inspectField("test2", "State.Error")
91+
if err != nil {
92+
t.Fatalf("Failed to inspect %q state's error, got error %q", "test2", err)
93+
}
94+
expected := "port is already allocated"
95+
if stateErr == "" || !strings.Contains(stateErr, expected) {
96+
t.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
97+
}
98+
99+
// Expect the conflict to be resolved when we stop the initial container
100+
cmd(t, "stop", "test")
101+
cmd(t, "start", "test2")
102+
stateErr, err = inspectField("test2", "State.Error")
103+
if err != nil {
104+
t.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
105+
}
106+
if stateErr != "" {
107+
t.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
108+
}
109+
110+
logDone("start - set state error when start fails")
111+
}

0 commit comments

Comments
 (0)