Skip to content

Commit

Permalink
Adding --docekr_root to customize where Docker stores state.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarmol committed Aug 29, 2014
1 parent 4706361 commit b75dea4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
4 changes: 4 additions & 0 deletions container/docker/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (

var ArgDockerEndpoint = flag.String("docker", "unix:///var/run/docker.sock", "docker endpoint")

// Basepath to all container specific information that libcontainer stores.
var dockerRootDir = flag.String("docker_root", "/var/lib/docker", "Absolute path to the Docker state root directory (default: /var/lib/docker)")

type dockerFactory struct {
machineInfoFactory info.MachineInfoFactory

Expand All @@ -54,6 +57,7 @@ func (self *dockerFactory) NewContainerHandler(name string) (handler container.C
name,
self.machineInfoFactory,
self.useSystemd,
*dockerRootDir,
)
return
}
Expand Down
35 changes: 19 additions & 16 deletions container/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,35 @@ import (
"github.com/google/cadvisor/utils"
)

// Basepath to all container specific information that libcontainer stores.
const dockerRootDir = "/var/lib/docker/execdriver/native"
// Relative path from Docker root to the libcontainer per-container state.
const pathToLibcontainerState = "execdriver/native"

var fileNotFound = errors.New("file not found")

type dockerContainerHandler struct {
client *docker.Client
name string
parent string
id string
aliases []string
machineInfoFactory info.MachineInfoFactory
useSystemd bool
client *docker.Client
name string
parent string
id string
aliases []string
machineInfoFactory info.MachineInfoFactory
useSystemd bool
libcontainerStateDir string
}

func newDockerContainerHandler(
client *docker.Client,
name string,
machineInfoFactory info.MachineInfoFactory,
useSystemd bool,
dockerRootDir string,
) (container.ContainerHandler, error) {
handler := &dockerContainerHandler{
client: client,
name: name,
machineInfoFactory: machineInfoFactory,
useSystemd: useSystemd,
client: client,
name: name,
machineInfoFactory: machineInfoFactory,
useSystemd: useSystemd,
libcontainerStateDir: path.Join(dockerRootDir, pathToLibcontainerState),
}
if handler.isDockerRoot() {
return handler, nil
Expand Down Expand Up @@ -89,7 +92,7 @@ func (self *dockerContainerHandler) isDockerRoot() bool {

// TODO(vmarmol): Switch to getting this from libcontainer once we have a solid API.
func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontainer.Config, err error) {
configPath := path.Join(dockerRootDir, self.id, "container.json")
configPath := path.Join(self.libcontainerStateDir, self.id, "container.json")
if !utils.FileExists(configPath) {
// TODO(vishh): Return file name as well once we have a better error interface.
err = fileNotFound
Expand All @@ -116,11 +119,11 @@ func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontain
}

func (self *dockerContainerHandler) readLibcontainerState() (state *libcontainer.State, err error) {
statePath := path.Join(dockerRootDir, self.id, "state.json")
statePath := path.Join(self.libcontainerStateDir, self.id, "state.json")
if !utils.FileExists(statePath) {
// TODO(vmarmol): Remove this once we can depend on a newer Docker.
// Libcontainer changed how its state was stored, try the old way of a "pid" file
if utils.FileExists(path.Join(dockerRootDir, self.id, "pid")) {
if utils.FileExists(path.Join(self.libcontainerStateDir, self.id, "pid")) {
// We don't need the old state, return an empty state and we'll gracefully degrade.
state = new(libcontainer.State)
return
Expand Down

0 comments on commit b75dea4

Please sign in to comment.