From 9f286709444ecb5bb6566a0ea6462c8eb1324b98 Mon Sep 17 00:00:00 2001 From: fengzixu Date: Tue, 29 Jan 2019 01:09:23 +0800 Subject: [PATCH] feature: exposed rootfs path of the container through inspect command Signed-off-by: fengzixu --- apis/server/container_bridge.go | 11 +++++++++++ apis/swagger.yml | 4 +++- apis/types/container_json.go | 3 +++ test/cli_inspect_test.go | 22 ++++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/apis/server/container_bridge.go b/apis/server/container_bridge.go index cc36f23ea..ab55ac0bb 100644 --- a/apis/server/container_bridge.go +++ b/apis/server/container_bridge.go @@ -25,6 +25,10 @@ import ( "github.com/sirupsen/logrus" ) +const ( + unknowHostRootPath = "" +) + func (s *Server) createContainer(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { label := util_metrics.ActionCreateLabel defer func(start time.Time) { @@ -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, @@ -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, diff --git a/apis/swagger.yml b/apis/swagger.yml index 4f6eeae51..8139f2d15 100644 --- a/apis/swagger.yml +++ b/apis/swagger.yml @@ -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] diff --git a/apis/types/container_json.go b/apis/types/container_json.go index b8a5871d9..55c175b69 100644 --- a/apis/types/container_json.go +++ b/apis/types/container_json.go @@ -43,6 +43,9 @@ type ContainerJSON struct { // host config HostConfig *HostConfig `json:"HostConfig,omitempty"` + // The rootfs path of the container on the host. + HostRootPath string `json:"HostRootPath,omitempty"` + // the path of container's hostname file on host. HostnamePath string `json:"HostnamePath,omitempty"` diff --git a/test/cli_inspect_test.go b/test/cli_inspect_test.go index 12c495a8e..f48bf2fd1 100644 --- a/test/cli_inspect_test.go +++ b/test/cli_inspect_test.go @@ -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"]) +}