diff --git a/daemon/exec.go b/daemon/exec.go index ecdbc58d85..616093c67d 100644 --- a/daemon/exec.go +++ b/daemon/exec.go @@ -35,7 +35,7 @@ type execConfig struct { type execStore struct { s map[string]*execConfig - sync.Mutex + sync.RWMutex } func newExecStore() *execStore { @@ -49,9 +49,9 @@ func (e *execStore) Add(id string, execConfig *execConfig) { } func (e *execStore) Get(id string) *execConfig { - e.Lock() + e.RLock() res := e.s[id] - e.Unlock() + e.RUnlock() return res } @@ -61,6 +61,16 @@ func (e *execStore) Delete(id string) { e.Unlock() } +func (e *execStore) List() []string { + var IDs []string + e.RLock() + for id := range e.s { + IDs = append(IDs, id) + } + e.RUnlock() + return IDs +} + func (execConfig *execConfig) Resize(h, w int) error { return execConfig.ProcessConfig.Terminal.Resize(h, w) } @@ -249,6 +259,10 @@ func (d *Daemon) Exec(c *Container, execConfig *execConfig, pipes *execdriver.Pi return exitStatus, err } +func (container *Container) GetExecIDs() []string { + return container.execCommands.List() +} + func (container *Container) Exec(execConfig *execConfig) error { container.Lock() defer container.Unlock() diff --git a/daemon/inspect.go b/daemon/inspect.go index 2bf1773d3a..37d00573bc 100644 --- a/daemon/inspect.go +++ b/daemon/inspect.go @@ -50,6 +50,8 @@ func (daemon *Daemon) ContainerInspect(job *engine.Job) engine.Status { out.SetJson("VolumesRW", container.VolumesRW) out.SetJson("AppArmorProfile", container.AppArmorProfile) + out.SetList("ExecIDs", container.GetExecIDs()) + if children, err := daemon.Children(container.Name); err == nil { for linkAlias, child := range children { container.hostConfig.Links = append(container.hostConfig.Links, fmt.Sprintf("%s:%s", child.Name, linkAlias)) diff --git a/docs/sources/reference/api/docker_remote_api.md b/docs/sources/reference/api/docker_remote_api.md index e44576cea0..fa210d4583 100644 --- a/docs/sources/reference/api/docker_remote_api.md +++ b/docs/sources/reference/api/docker_remote_api.md @@ -51,6 +51,11 @@ You can still call an old version of the API using **New!** Docker client now hints potential proxies about connection hijacking using HTTP Upgrade headers. +`GET /containers/(id)/json` + +**New!** +This endpoint now returns the list current execs associated with the container (`ExecIDs`). + ## v1.16 ### Full Documentation diff --git a/docs/sources/reference/api/docker_remote_api_v1.17.md b/docs/sources/reference/api/docker_remote_api_v1.17.md index 1ffb1cbf95..7c81a7acb5 100644 --- a/docs/sources/reference/api/docker_remote_api_v1.17.md +++ b/docs/sources/reference/api/docker_remote_api_v1.17.md @@ -310,6 +310,9 @@ Return low-level information on the container `id` "SysInitPath": "/home/kitty/go/src/github.com/docker/docker/bin/docker", "ResolvConfPath": "/etc/resolv.conf", "Volumes": {}, + "ExecIDs": [ + "15f211491dced6a353a2e0f37fe3f3692ee2370a4782418e9bf7052865c10fde" + ], "HostConfig": { "Binds": null, "ContainerIDFile": "", diff --git a/integration-cli/docker_cli_inspect_test.go b/integration-cli/docker_cli_inspect_test.go index cf42217ac8..ee69a89a43 100644 --- a/integration-cli/docker_cli_inspect_test.go +++ b/integration-cli/docker_cli_inspect_test.go @@ -21,3 +21,38 @@ func TestInspectImage(t *testing.T) { logDone("inspect - inspect an image") } + +func TestInspectExecID(t *testing.T) { + defer deleteAllContainers() + + out, exitCode, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox", "top")) + if exitCode != 0 || err != nil { + t.Fatalf("failed to run container: %s, %v", out, err) + } + id := strings.TrimSuffix(out, "\n") + + out, err = inspectField(id, "ExecIDs") + if err != nil { + t.Fatalf("failed to inspect container: %s, %v", out, err) + } + if out != "" { + t.Fatalf("ExecIDs should be empty, got: %s", out) + } + + exitCode, err = runCommand(exec.Command(dockerBinary, "exec", "-d", id, "ls", "/")) + if exitCode != 0 || err != nil { + t.Fatalf("failed to exec in container: %s, %v", out, err) + } + + out, err = inspectField(id, "ExecIDs") + if err != nil { + t.Fatalf("failed to inspect container: %s, %v", out, err) + } + + out = strings.TrimSuffix(out, "\n") + if out == "[]" || out == "" { + t.Fatalf("ExecIDs should not be empty, got: %s", out) + } + + logDone("inspect - inspect a container with ExecIDs") +}