Skip to content

Commit

Permalink
feature: exposed rootfs path of the container through inspect command
Browse files Browse the repository at this point in the history
Signed-off-by: fengzixu <hnustphoenix@gmail.com>
  • Loading branch information
fengzixu authored and HusterWan committed Jan 29, 2019
1 parent 9df7eab commit 9f28670
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
11 changes: 11 additions & 0 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
"github.com/sirupsen/logrus"
)

const (
unknowHostRootPath = "<unknown>"
)

func (s *Server) createContainer(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
label := util_metrics.ActionCreateLabel
defer func(start time.Time) {
Expand Down Expand Up @@ -84,6 +88,12 @@ func (s *Server) getContainer(ctx context.Context, rw http.ResponseWriter, req *
mounts = append(mounts, *mp)
}

hostRootPath := unknowHostRootPath
mergedDir, ok := c.Snapshotter.Data["MergedDir"]
if ok {
hostRootPath = mergedDir
}

container := types.ContainerJSON{
ID: c.ID,
Name: c.Name,
Expand All @@ -105,6 +115,7 @@ func (s *Server) getContainer(ctx context.Context, rw http.ResponseWriter, req *
Args: c.Args,
ResolvConfPath: c.ResolvConfPath,
HostnamePath: c.HostnamePath,
HostRootPath: hostRootPath,
HostsPath: c.HostsPath,
Driver: c.Driver,
MountLabel: c.MountLabel,
Expand Down
4 changes: 3 additions & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3484,7 +3484,9 @@ definitions:
NetworkSettings:
description: "NetworkSettings exposes the network settings in the API."
$ref: "#/definitions/NetworkSettings"

HostRootPath:
description: "The rootfs path of the container on the host."
type: "string"
ContainerState:
type: "object"
required: [StartedAt, FinishedAt, Pid, ExitCode, Error, OOMKilled, Dead, Paused, Restarting, Running, Status]
Expand Down
3 changes: 3 additions & 0 deletions apis/types/container_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions test/cli_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,25 @@ func (suite *PouchInspectSuite) TestContainerInspectPorts(c *check.C) {
data, _ := json.Marshal(containers[0].NetworkSettings.Ports)
c.Assert(string(data), check.Equals, "{\"80/tcp\":[{\"HostIp\":\"0.0.0.0\",\"HostPort\":\"8080\"}]}")
}

func (suite *PouchInspectSuite) TestContainerInspectHostRootPath(c *check.C) {
name := "TestContainerInspectHostRootPath"
res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

hostRootPathOutput := command.PouchRun("inspect", "-f", "{{.HostRootPath}}", name).Stdout()
containerOutput := command.PouchRun("inspect", name).Stdout()

containers := make([]types.ContainerJSON, 1)
err := json.Unmarshal([]byte(containerOutput), &containers)
if err != nil || len(containers) == 0 {
c.Fatal("fail to format container json")
}

if containers[0].GraphDriver == nil {
c.Fatal("cannot to find any info of GraphDriver")
}

c.Assert(strings.TrimSpace(hostRootPathOutput), check.Equals, containers[0].GraphDriver.Data["MergedDir"])
}

0 comments on commit 9f28670

Please sign in to comment.