mirror of https://github.com/docker/docs.git
Merge pull request #1159 from unclejack/add_container_id_file_to_run
*Client: Add support for container ID files (a la pidfile)
This commit is contained in:
commit
e59dd2c62c
17
commands.go
17
commands.go
|
@ -1311,6 +1311,18 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
||||||
return nil
|
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
|
//create the container
|
||||||
body, statusCode, err := cli.call("POST", "/containers/create", config)
|
body, statusCode, err := cli.call("POST", "/containers/create", config)
|
||||||
//if image not found try to pull it
|
//if image not found try to pull it
|
||||||
|
@ -1341,6 +1353,11 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
||||||
for _, warning := range runResult.Warnings {
|
for _, warning := range runResult.Warnings {
|
||||||
fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
|
fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
|
||||||
}
|
}
|
||||||
|
if len(hostConfig.ContainerIDFile) > 0 {
|
||||||
|
if _, err = containerIDFile.WriteString(runResult.ID); err != nil {
|
||||||
|
return fmt.Errorf("failed to write the container ID to the file: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//start the container
|
//start the container
|
||||||
if _, _, err = cli.call("POST", "/containers/"+runResult.ID+"/start", hostConfig); err != nil {
|
if _, _, err = cli.call("POST", "/containers/"+runResult.ID+"/start", hostConfig); err != nil {
|
||||||
|
|
|
@ -81,6 +81,7 @@ type Config struct {
|
||||||
|
|
||||||
type HostConfig struct {
|
type HostConfig struct {
|
||||||
Binds []string
|
Binds []string
|
||||||
|
ContainerIDFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BindMap struct {
|
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")
|
flStdin := cmd.Bool("i", false, "Keep stdin open even if not attached")
|
||||||
flTty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
flTty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
||||||
flMemory := cmd.Int64("m", 0, "Memory limit (in bytes)")
|
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 {
|
if capabilities != nil && *flMemory > 0 && !capabilities.MemoryLimit {
|
||||||
//fmt.Fprintf(stdout, "WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
|
//fmt.Fprintf(stdout, "WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
|
||||||
|
@ -191,6 +193,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig,
|
||||||
}
|
}
|
||||||
hostConfig := &HostConfig{
|
hostConfig := &HostConfig{
|
||||||
Binds: binds,
|
Binds: binds,
|
||||||
|
ContainerIDFile: *flContainerIDFile,
|
||||||
}
|
}
|
||||||
|
|
||||||
if capabilities != nil && *flMemory > 0 && !capabilities.SwapLimit {
|
if capabilities != nil && *flMemory > 0 && !capabilities.SwapLimit {
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
-a=map[]: Attach to stdin, stdout or stderr.
|
-a=map[]: Attach to stdin, stdout or stderr.
|
||||||
-c=0: CPU shares (relative weight)
|
-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
|
-d=false: Detached mode: leave the container running in the background
|
||||||
-e=[]: Set environment variables
|
-e=[]: Set environment variables
|
||||||
-h="": Container host name
|
-h="": Container host name
|
||||||
|
@ -26,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.
|
-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.
|
-volumes-from="": Mount all volumes from the given container.
|
||||||
-entrypoint="": Overwrite the default entrypoint set by the image.
|
-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.
|
||||||
|
|
Loading…
Reference in New Issue