@@ -30,10 +30,17 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c
3030 return fmt .Errorf ("You must choose at least one stream" )
3131 }
3232
33- cLog , err := daemon .getLogger (container )
33+ cLog , cLogCreated , err := daemon .getLogger (container )
3434 if err != nil {
3535 return err
3636 }
37+ if cLogCreated {
38+ defer func () {
39+ if err = cLog .Close (); err != nil {
40+ logrus .Errorf ("Error closing logger: %v" , err )
41+ }
42+ }()
43+ }
3744 logReader , ok := cLog .(logger.LogReader )
3845 if ! ok {
3946 return logger .ErrReadLogsNotSupported
@@ -64,14 +71,6 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c
6471 // Close logWatcher on exit
6572 defer func () {
6673 logs .Close ()
67- if cLog != container .LogDriver {
68- // Since the logger isn't cached in the container, which
69- // occurs if it is running, it must get explicitly closed
70- // here to avoid leaking it and any file handles it has.
71- if err := cLog .Close (); err != nil {
72- logrus .Errorf ("Error closing logger: %v" , err )
73- }
74- }
7574 }()
7675
7776 wf := ioutils .NewWriteFlusher (config .OutStream )
@@ -117,11 +116,17 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c
117116 }
118117}
119118
120- func (daemon * Daemon ) getLogger (container * container.Container ) (logger.Logger , error ) {
121- if container .LogDriver != nil && container .IsRunning () {
122- return container .LogDriver , nil
119+ func (daemon * Daemon ) getLogger (container * container.Container ) (l logger.Logger , created bool , err error ) {
120+ container .Lock ()
121+ if container .State .Running {
122+ l = container .LogDriver
123+ }
124+ container .Unlock ()
125+ if l == nil {
126+ created = true
127+ l , err = container .StartLogger ()
123128 }
124- return container . StartLogger ()
129+ return
125130}
126131
127132// mergeLogConfig merges the daemon log config to the container's log config if the container's log driver is not specified.
0 commit comments