mirror of https://github.com/docker/docs.git
Container logs are persisted on disk
This commit is contained in:
parent
007c57deba
commit
2df0bc6bc0
55
container.go
55
container.go
|
@ -1,7 +1,6 @@
|
||||||
package docker
|
package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/kr/pty"
|
"github.com/kr/pty"
|
||||||
|
@ -12,7 +11,6 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -48,8 +46,8 @@ type Container struct {
|
||||||
stdin io.ReadCloser
|
stdin io.ReadCloser
|
||||||
stdinPipe io.WriteCloser
|
stdinPipe io.WriteCloser
|
||||||
|
|
||||||
stdoutLog *bytes.Buffer
|
stdoutLog *os.File
|
||||||
stderrLog *bytes.Buffer
|
stderrLog *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -84,8 +82,20 @@ func createContainer(id string, root string, command string, args []string, laye
|
||||||
lxcConfigPath: path.Join(root, "config.lxc"),
|
lxcConfigPath: path.Join(root, "config.lxc"),
|
||||||
stdout: newWriteBroadcaster(),
|
stdout: newWriteBroadcaster(),
|
||||||
stderr: newWriteBroadcaster(),
|
stderr: newWriteBroadcaster(),
|
||||||
stdoutLog: new(bytes.Buffer),
|
}
|
||||||
stderrLog: new(bytes.Buffer),
|
if err := os.Mkdir(root, 0700); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Setup logging of stdout and stderr to disk
|
||||||
|
if stdoutLog, err := os.OpenFile(path.Join(container.Root, id+"-stdout.log"), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
container.stdoutLog = stdoutLog
|
||||||
|
}
|
||||||
|
if stderrLog, err := os.OpenFile(path.Join(container.Root, id+"-stderr.log"), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
container.stderrLog = stderrLog
|
||||||
}
|
}
|
||||||
if container.Config.OpenStdin {
|
if container.Config.OpenStdin {
|
||||||
container.stdin, container.stdinPipe = io.Pipe()
|
container.stdin, container.stdinPipe = io.Pipe()
|
||||||
|
@ -95,9 +105,6 @@ func createContainer(id string, root string, command string, args []string, laye
|
||||||
container.stdout.AddWriter(NopWriteCloser(container.stdoutLog))
|
container.stdout.AddWriter(NopWriteCloser(container.stdoutLog))
|
||||||
container.stderr.AddWriter(NopWriteCloser(container.stderrLog))
|
container.stderr.AddWriter(NopWriteCloser(container.stderrLog))
|
||||||
|
|
||||||
if err := os.Mkdir(root, 0700); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := container.Filesystem.createMountPoints(); err != nil {
|
if err := container.Filesystem.createMountPoints(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -115,15 +122,29 @@ func loadContainer(containerPath string, netManager *NetworkManager) (*Container
|
||||||
container := &Container{
|
container := &Container{
|
||||||
stdout: newWriteBroadcaster(),
|
stdout: newWriteBroadcaster(),
|
||||||
stderr: newWriteBroadcaster(),
|
stderr: newWriteBroadcaster(),
|
||||||
stdoutLog: new(bytes.Buffer),
|
|
||||||
stderrLog: new(bytes.Buffer),
|
|
||||||
lxcConfigPath: path.Join(containerPath, "config.lxc"),
|
lxcConfigPath: path.Join(containerPath, "config.lxc"),
|
||||||
networkManager: netManager,
|
networkManager: netManager,
|
||||||
NetworkSettings: &NetworkSettings{},
|
NetworkSettings: &NetworkSettings{},
|
||||||
}
|
}
|
||||||
|
// Load container settings
|
||||||
if err := json.Unmarshal(data, container); err != nil {
|
if err := json.Unmarshal(data, container); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Setup logging of stdout and stderr to disk
|
||||||
|
if stdoutLog, err := os.OpenFile(path.Join(container.Root, container.Id+"-stdout.log"), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
container.stdoutLog = stdoutLog
|
||||||
|
}
|
||||||
|
if stderrLog, err := os.OpenFile(path.Join(container.Root, container.Id+"-stderr.log"), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
container.stderrLog = stderrLog
|
||||||
|
}
|
||||||
|
container.stdout.AddWriter(NopWriteCloser(container.stdoutLog))
|
||||||
|
container.stderr.AddWriter(NopWriteCloser(container.stderrLog))
|
||||||
|
|
||||||
|
// Create mountpoints
|
||||||
if err := container.Filesystem.createMountPoints(); err != nil {
|
if err := container.Filesystem.createMountPoints(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -357,7 +378,11 @@ func (container *Container) StdoutPipe() (io.ReadCloser, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) StdoutLog() io.Reader {
|
func (container *Container) StdoutLog() io.Reader {
|
||||||
return strings.NewReader(container.stdoutLog.String())
|
r, err := os.Open(container.stdoutLog.Name())
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) StderrPipe() (io.ReadCloser, error) {
|
func (container *Container) StderrPipe() (io.ReadCloser, error) {
|
||||||
|
@ -367,7 +392,11 @@ func (container *Container) StderrPipe() (io.ReadCloser, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) StderrLog() io.Reader {
|
func (container *Container) StderrLog() io.Reader {
|
||||||
return strings.NewReader(container.stderrLog.String())
|
r, err := os.Open(container.stderrLog.Name())
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) allocateNetwork() error {
|
func (container *Container) allocateNetwork() error {
|
||||||
|
|
Loading…
Reference in New Issue