@@ -111,7 +111,7 @@ func (s *ConcreteService) Start(networkName, sharedDir string) (err error) {
111111 s .usedNetworkName = networkName
112112
113113 // Wait until the container has been started.
114- if err = s .WaitStarted (); err != nil {
114+ if err = s .WaitForRunning (); err != nil {
115115 return err
116116 }
117117
@@ -123,10 +123,10 @@ func (s *ConcreteService) Start(networkName, sharedDir string) (err error) {
123123 out , err = RunCommandAndGetOutput ("docker" , "port" , s .containerName (), strconv .Itoa (containerPort ))
124124 if err != nil {
125125 // Catch init errors.
126- if werr := s .WaitStarted (); werr != nil {
126+ if werr := s .WaitForRunning (); werr != nil {
127127 return errors .Wrapf (werr , "failed to get mapping for port as container %s exited: %v" , s .containerName (), err )
128128 }
129- return errors .Wrapf (err , "unable to get mapping for port %d; service: %s" , containerPort , s .name )
129+ return errors .Wrapf (err , "unable to get mapping for port %d; service: %s; output: %q " , containerPort , s .name , out )
130130 }
131131
132132 stdout := strings .TrimSpace (string (out ))
@@ -243,22 +243,36 @@ func (s *ConcreteService) containerName() string {
243243 return containerName (s .usedNetworkName , s .name )
244244}
245245
246- func (s * ConcreteService ) WaitStarted () (err error ) {
246+ func (s * ConcreteService ) WaitForRunning () (err error ) {
247247 if ! s .isExpectedRunning () {
248248 return fmt .Errorf ("service %s is stopped" , s .Name ())
249249 }
250250
251251 for s .retryBackoff .Reset (); s .retryBackoff .Ongoing (); {
252252 // Enforce a timeout on the command execution because we've seen some flaky tests
253253 // stuck here.
254- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
255- defer cancel ()
256254
257- err = exec .CommandContext (ctx , "docker" , "inspect" , s .containerName ()).Run ()
258- if err == nil {
259- return nil
255+ var out []byte
256+ out , err = RunCommandWithTimeoutAndGetOutput (5 * time .Second , "docker" , "inspect" , "--format={{json .State.Running}}" , s .containerName ())
257+ if err != nil {
258+ s .retryBackoff .Wait ()
259+ continue
260260 }
261- s .retryBackoff .Wait ()
261+
262+ if out == nil {
263+ err = fmt .Errorf ("nil output" )
264+ s .retryBackoff .Wait ()
265+ continue
266+ }
267+
268+ str := strings .TrimSpace (string (out ))
269+ if str != "true" {
270+ err = fmt .Errorf ("unexpected output: %q" , str )
271+ s .retryBackoff .Wait ()
272+ continue
273+ }
274+
275+ return nil
262276 }
263277
264278 return fmt .Errorf ("docker container %s failed to start: %v" , s .name , err )
0 commit comments