From 3498daf9b5fe147e8151e44e931d9236e45aeea0 Mon Sep 17 00:00:00 2001 From: unclejack Date: Mon, 8 Jul 2013 11:01:16 +0300 Subject: [PATCH 1/4] add support for container ID files (a la pidfile) Upstream-commit: 64e74cefb746caa7f2a581149bbd523dd1ac9215 Component: engine --- components/engine/commands.go | 14 ++++++++++++++ components/engine/container.go | 7 +++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/components/engine/commands.go b/components/engine/commands.go index 936b23fea2..42cccf6b96 100644 --- a/components/engine/commands.go +++ b/components/engine/commands.go @@ -1341,6 +1341,20 @@ func (cli *DockerCli) CmdRun(args ...string) error { for _, warning := range runResult.Warnings { fmt.Fprintf(cli.err, "WARNING: %s\n", warning) } + if len(hostConfig.ContainerIDFile) > 0 { + if _, err := ioutil.ReadFile(hostConfig.ContainerIDFile); err == nil { + return fmt.Errorf("cid file found, make sure the other container isn't running or delete %s", hostConfig.ContainerIDFile) + } + file, err := os.Create(hostConfig.ContainerIDFile) + if err != nil { + return fmt.Errorf("failed to create the container ID file: %s", err) + } + + defer file.Close() + if _, err = file.WriteString(runResult.ID); err != nil { + return fmt.Errorf("failed to write the container ID to the file: %s", err) + } + } //start the container if _, _, err = cli.call("POST", "/containers/"+runResult.ID+"/start", hostConfig); err != nil { diff --git a/components/engine/container.go b/components/engine/container.go index 95c5ba0f72..4443ad52a3 100644 --- a/components/engine/container.go +++ b/components/engine/container.go @@ -80,7 +80,8 @@ type Config struct { } type HostConfig struct { - Binds []string + Binds []string + ContainerIDFile string } type BindMap struct { @@ -103,6 +104,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, flStdin := cmd.Bool("i", false, "Keep stdin open even if not attached") flTty := cmd.Bool("t", false, "Allocate a pseudo-tty") flMemory := cmd.Int64("m", 0, "Memory limit (in bytes)") + flContainerIDFile := cmd.String("cidfile", "", "Write the container ID to the file") if capabilities != nil && *flMemory > 0 && !capabilities.MemoryLimit { //fmt.Fprintf(stdout, "WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n") @@ -190,7 +192,8 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, Entrypoint: entrypoint, } hostConfig := &HostConfig{ - Binds: binds, + Binds: binds, + ContainerIDFile: *flContainerIDFile, } if capabilities != nil && *flMemory > 0 && !capabilities.SwapLimit { From ce2e737919772137afa268bd944e55c176f28ccc Mon Sep 17 00:00:00 2001 From: unclejack Date: Thu, 11 Jul 2013 23:38:43 +0300 Subject: [PATCH 2/4] docs - add cidfile flag to run docs Upstream-commit: 221ee504aa06d06eb868898cca2fcc020a861e84 Component: engine --- components/engine/docs/sources/commandline/command/run.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/components/engine/docs/sources/commandline/command/run.rst b/components/engine/docs/sources/commandline/command/run.rst index 1a14a9b616..bfd35738fa 100644 --- a/components/engine/docs/sources/commandline/command/run.rst +++ b/components/engine/docs/sources/commandline/command/run.rst @@ -14,6 +14,7 @@ -a=map[]: Attach to stdin, stdout or stderr. -c=0: CPU shares (relative weight) + -cidfile="": Write the container ID to the file -d=false: Detached mode: leave the container running in the background -e=[]: Set environment variables -h="": Container host name From 59b7a438c9d2201429d643a94740cba1fbd34748 Mon Sep 17 00:00:00 2001 From: unclejack Date: Fri, 12 Jul 2013 00:50:03 +0300 Subject: [PATCH 3/4] docs - add example for cidfile Upstream-commit: 2a3b91e3b66c48c6a26dbd673957a46c1afacbbe Component: engine --- .../engine/docs/sources/commandline/command/run.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/components/engine/docs/sources/commandline/command/run.rst b/components/engine/docs/sources/commandline/command/run.rst index bfd35738fa..19efd85821 100644 --- a/components/engine/docs/sources/commandline/command/run.rst +++ b/components/engine/docs/sources/commandline/command/run.rst @@ -27,3 +27,13 @@ -v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. If "host-dir" is missing, then docker creates a new volume. -volumes-from="": Mount all volumes from the given container. -entrypoint="": Overwrite the default entrypoint set by the image. + + +Examples +-------- + +.. code-block:: bash + + docker run -cidfile /tmp/docker_test.cid ubuntu echo "test" + +| This will create a container and print "test" to the console. The cidfile flag makes docker attempt to create a new file and write the container ID to it. If the file exists already, docker will return an error. Docker will close this file when docker run exits. From 1b1ac70ce1e733a2c7a93c8933f0fded1a69af5d Mon Sep 17 00:00:00 2001 From: unclejack Date: Fri, 12 Jul 2013 01:11:44 +0300 Subject: [PATCH 4/4] create the cidfile before creating the container This change makes docker attempt to create the container ID file and open it before attempting to create the container. This avoids leaving a stale container behind if docker has failed to create and open the container ID file. The container ID is written to the file after the container is created. Upstream-commit: 25be79208a1473a65be883989ae49b7c71081a83 Component: engine --- components/engine/commands.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/components/engine/commands.go b/components/engine/commands.go index 42cccf6b96..7f8f9eec0b 100644 --- a/components/engine/commands.go +++ b/components/engine/commands.go @@ -1311,6 +1311,18 @@ func (cli *DockerCli) CmdRun(args ...string) error { return nil } + var containerIDFile *os.File + if len(hostConfig.ContainerIDFile) > 0 { + if _, err := ioutil.ReadFile(hostConfig.ContainerIDFile); err == nil { + return fmt.Errorf("cid file found, make sure the other container isn't running or delete %s", hostConfig.ContainerIDFile) + } + containerIDFile, err = os.Create(hostConfig.ContainerIDFile) + if err != nil { + return fmt.Errorf("failed to create the container ID file: %s", err) + } + defer containerIDFile.Close() + } + //create the container body, statusCode, err := cli.call("POST", "/containers/create", config) //if image not found try to pull it @@ -1342,16 +1354,7 @@ func (cli *DockerCli) CmdRun(args ...string) error { fmt.Fprintf(cli.err, "WARNING: %s\n", warning) } if len(hostConfig.ContainerIDFile) > 0 { - if _, err := ioutil.ReadFile(hostConfig.ContainerIDFile); err == nil { - return fmt.Errorf("cid file found, make sure the other container isn't running or delete %s", hostConfig.ContainerIDFile) - } - file, err := os.Create(hostConfig.ContainerIDFile) - if err != nil { - return fmt.Errorf("failed to create the container ID file: %s", err) - } - - defer file.Close() - if _, err = file.WriteString(runResult.ID); err != nil { + if _, err = containerIDFile.WriteString(runResult.ID); err != nil { return fmt.Errorf("failed to write the container ID to the file: %s", err) } }