2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2015-05-27 16:15:14 -04:00
|
|
|
"runtime"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/docker/docker/pkg/promise"
|
|
|
|
"github.com/docker/docker/pkg/signal"
|
|
|
|
"github.com/docker/docker/runconfig"
|
2015-05-06 18:39:29 -04:00
|
|
|
"github.com/docker/libnetwork/resolvconf/dns"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (cid *cidFile) Close() error {
|
|
|
|
cid.file.Close()
|
|
|
|
|
|
|
|
if !cid.written {
|
|
|
|
if err := os.Remove(cid.path); err != nil {
|
|
|
|
return fmt.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cid *cidFile) Write(id string) error {
|
|
|
|
if _, err := cid.file.Write([]byte(id)); err != nil {
|
|
|
|
return fmt.Errorf("Failed to write the container ID to the file: %s", err)
|
|
|
|
}
|
|
|
|
cid.written = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdRun runs a command in a new container.
|
|
|
|
//
|
|
|
|
// Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdRun(args ...string) error {
|
2015-05-05 00:18:28 -04:00
|
|
|
cmd := Cli.Subcmd("run", []string{"IMAGE [COMMAND] [ARG...]"}, "Run a command in a new container", true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
// These are flags not stored in Config/HostConfig
|
|
|
|
var (
|
2015-04-20 20:13:48 -04:00
|
|
|
flAutoRemove = cmd.Bool([]string{"-rm"}, false, "Automatically remove the container when it exits")
|
2015-03-24 23:57:23 -04:00
|
|
|
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Run container in background and print container ID")
|
2015-04-20 20:13:48 -04:00
|
|
|
flSigProxy = cmd.Bool([]string{"-sig-proxy"}, true, "Proxy received signals to the process")
|
|
|
|
flName = cmd.String([]string{"-name"}, "", "Assign a name to the container")
|
2015-03-24 23:57:23 -04:00
|
|
|
flAttach *opts.ListOpts
|
|
|
|
|
|
|
|
ErrConflictAttachDetach = fmt.Errorf("Conflicting options: -a and -d")
|
|
|
|
ErrConflictRestartPolicyAndAutoRemove = fmt.Errorf("Conflicting options: --restart and --rm")
|
|
|
|
ErrConflictDetachAutoRemove = fmt.Errorf("Conflicting options: --rm and -d")
|
|
|
|
)
|
|
|
|
|
|
|
|
config, hostConfig, cmd, err := runconfig.Parse(cmd, args)
|
|
|
|
// just in case the Parse does not exit
|
|
|
|
if err != nil {
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ReportError(err.Error(), true)
|
2015-03-31 03:11:03 -04:00
|
|
|
os.Exit(1)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(hostConfig.Dns) > 0 {
|
|
|
|
// check the DNS settings passed via --dns against
|
|
|
|
// localhost regexp to warn if they are trying to
|
|
|
|
// set a DNS to a localhost address
|
|
|
|
for _, dnsIP := range hostConfig.Dns {
|
2015-05-11 18:16:23 -04:00
|
|
|
if dns.IsLocalhost(dnsIP) {
|
2015-03-24 23:57:23 -04:00
|
|
|
fmt.Fprintf(cli.err, "WARNING: Localhost DNS setting (--dns=%s) may fail in containers.\n", dnsIP)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.Image == "" {
|
|
|
|
cmd.Usage()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !*flDetach {
|
|
|
|
if err := cli.CheckTtyInput(config.AttachStdin, config.Tty); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if fl := cmd.Lookup("-attach"); fl != nil {
|
|
|
|
flAttach = fl.Value.(*opts.ListOpts)
|
|
|
|
if flAttach.Len() != 0 {
|
|
|
|
return ErrConflictAttachDetach
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *flAutoRemove {
|
|
|
|
return ErrConflictDetachAutoRemove
|
|
|
|
}
|
|
|
|
|
|
|
|
config.AttachStdin = false
|
|
|
|
config.AttachStdout = false
|
|
|
|
config.AttachStderr = false
|
|
|
|
config.StdinOnce = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable flSigProxy when in TTY mode
|
|
|
|
sigProxy := *flSigProxy
|
|
|
|
if config.Tty {
|
|
|
|
sigProxy = false
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:15:14 -04:00
|
|
|
// Telling the Windows daemon the initial size of the tty during start makes
|
|
|
|
// a far better user experience rather than relying on subsequent resizes
|
|
|
|
// to cause things to catch up.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
hostConfig.ConsoleSize[0], hostConfig.ConsoleSize[1] = cli.getTtySize()
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
createResponse, err := cli.createContainer(config, hostConfig, hostConfig.ContainerIDFile, *flName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sigProxy {
|
|
|
|
sigc := cli.forwardAllSignals(createResponse.ID)
|
|
|
|
defer signal.StopCatch(sigc)
|
|
|
|
}
|
|
|
|
var (
|
2015-03-25 22:31:29 -04:00
|
|
|
waitDisplayID chan struct{}
|
2015-03-24 23:57:23 -04:00
|
|
|
errCh chan error
|
|
|
|
)
|
|
|
|
if !config.AttachStdout && !config.AttachStderr {
|
|
|
|
// Make this asynchronous to allow the client to write to stdin before having to read the ID
|
2015-03-25 22:31:29 -04:00
|
|
|
waitDisplayID = make(chan struct{})
|
2015-03-24 23:57:23 -04:00
|
|
|
go func() {
|
2015-03-25 22:31:29 -04:00
|
|
|
defer close(waitDisplayID)
|
2015-03-24 23:57:23 -04:00
|
|
|
fmt.Fprintf(cli.out, "%s\n", createResponse.ID)
|
|
|
|
}()
|
|
|
|
}
|
2015-05-16 07:34:09 -04:00
|
|
|
if *flAutoRemove && (hostConfig.RestartPolicy.IsAlways() || hostConfig.RestartPolicy.IsOnFailure()) {
|
2015-03-24 23:57:23 -04:00
|
|
|
return ErrConflictRestartPolicyAndAutoRemove
|
|
|
|
}
|
|
|
|
// We need to instantiate the chan because the select needs it. It can
|
|
|
|
// be closed but can't be uninitialized.
|
|
|
|
hijacked := make(chan io.Closer)
|
|
|
|
// Block the return until the chan gets closed
|
|
|
|
defer func() {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("End of CmdRun(), Waiting for hijack to finish.")
|
2015-03-24 23:57:23 -04:00
|
|
|
if _, ok := <-hijacked; ok {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintln(cli.err, "Hijack did not finish (chan still open)")
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
if config.AttachStdin || config.AttachStdout || config.AttachStderr {
|
|
|
|
var (
|
|
|
|
out, stderr io.Writer
|
|
|
|
in io.ReadCloser
|
|
|
|
v = url.Values{}
|
|
|
|
)
|
|
|
|
v.Set("stream", "1")
|
|
|
|
if config.AttachStdin {
|
|
|
|
v.Set("stdin", "1")
|
|
|
|
in = cli.in
|
|
|
|
}
|
|
|
|
if config.AttachStdout {
|
|
|
|
v.Set("stdout", "1")
|
|
|
|
out = cli.out
|
|
|
|
}
|
|
|
|
if config.AttachStderr {
|
|
|
|
v.Set("stderr", "1")
|
|
|
|
if config.Tty {
|
|
|
|
stderr = cli.out
|
|
|
|
} else {
|
|
|
|
stderr = cli.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errCh = promise.Go(func() error {
|
|
|
|
return cli.hijack("POST", "/containers/"+createResponse.ID+"/attach?"+v.Encode(), config.Tty, in, out, stderr, hijacked, nil)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
close(hijacked)
|
|
|
|
}
|
|
|
|
// Acknowledge the hijack before starting
|
|
|
|
select {
|
|
|
|
case closer := <-hijacked:
|
|
|
|
// Make sure that the hijack gets closed when returning (results
|
|
|
|
// in closing the hijack chan and freeing server's goroutines)
|
|
|
|
if closer != nil {
|
|
|
|
defer closer.Close()
|
|
|
|
}
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error hijack: %s", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if *flAutoRemove {
|
2015-01-12 14:56:01 -05:00
|
|
|
if _, _, err = readBody(cli.call("DELETE", "/containers/"+createResponse.ID+"?v=1", nil, nil)); err != nil {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintf(cli.err, "Error deleting container: %s\n", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
//start the container
|
2015-01-12 14:56:01 -05:00
|
|
|
if _, _, err = readBody(cli.call("POST", "/containers/"+createResponse.ID+"/start", nil, nil)); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.AttachStdin || config.AttachStdout || config.AttachStderr) && config.Tty && cli.isTerminalOut {
|
|
|
|
if err := cli.monitorTtySize(createResponse.ID, false); err != nil {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errCh != nil {
|
|
|
|
if err := <-errCh; err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error hijack: %s", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detached mode: wait for the id to be displayed and return.
|
|
|
|
if !config.AttachStdout && !config.AttachStderr {
|
|
|
|
// Detached mode
|
2015-03-25 22:31:29 -04:00
|
|
|
<-waitDisplayID
|
2015-03-24 23:57:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var status int
|
|
|
|
|
|
|
|
// Attached mode
|
|
|
|
if *flAutoRemove {
|
|
|
|
// Autoremove: wait for the container to finish, retrieve
|
|
|
|
// the exit code and remove the container
|
2015-01-12 14:56:01 -05:00
|
|
|
if _, _, err := readBody(cli.call("POST", "/containers/"+createResponse.ID+"/wait", nil, nil)); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, status, err = getExitCode(cli, createResponse.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No Autoremove: Simply retrieve the exit code
|
|
|
|
if !config.Tty {
|
|
|
|
// In non-TTY mode, we can't detach, so we must wait for container exit
|
|
|
|
if status, err = waitForExit(cli, createResponse.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In TTY mode, there is a race: if the process dies too slowly, the state could
|
|
|
|
// be updated after the getExitCode call and result in the wrong exit code being reported
|
|
|
|
if _, status, err = getExitCode(cli, createResponse.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if status != 0 {
|
2015-05-05 00:18:28 -04:00
|
|
|
return Cli.StatusError{StatusCode: status}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|