run.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // +build windows
  2. package windows
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. "syscall"
  12. "github.com/Sirupsen/logrus"
  13. "github.com/docker/docker/daemon/execdriver"
  14. "github.com/microsoft/hcsshim"
  15. )
  16. // defaultContainerNAT is the default name of the container NAT device that is
  17. // preconfigured on the server.
  18. const defaultContainerNAT = "ContainerNAT"
  19. type layer struct {
  20. ID string
  21. Path string
  22. }
  23. type defConfig struct {
  24. DefFile string
  25. }
  26. type portBinding struct {
  27. Protocol string
  28. InternalPort int
  29. ExternalPort int
  30. }
  31. type natSettings struct {
  32. Name string
  33. PortBindings []portBinding
  34. }
  35. type networkConnection struct {
  36. NetworkName string
  37. // TODO Windows: Add Ip4Address string to this structure when hooked up in
  38. // docker CLI. This is present in the HCS JSON handler.
  39. EnableNat bool
  40. Nat natSettings
  41. }
  42. type networkSettings struct {
  43. MacAddress string
  44. }
  45. type device struct {
  46. DeviceType string
  47. Connection interface{}
  48. Settings interface{}
  49. }
  50. type mappedDir struct {
  51. HostPath string
  52. ContainerPath string
  53. ReadOnly bool
  54. }
  55. type containerInit struct {
  56. SystemType string // HCS requires this to be hard-coded to "Container"
  57. Name string // Name of the container. We use the docker ID.
  58. Owner string // The management platform that created this container
  59. IsDummy bool // Used for development purposes.
  60. VolumePath string // Windows volume path for scratch space
  61. Devices []device // Devices used by the container
  62. IgnoreFlushesDuringBoot bool // Optimisation hint for container startup in Windows
  63. LayerFolderPath string // Where the layer folders are located
  64. Layers []layer // List of storage layers
  65. ProcessorWeight int64 // CPU Shares 1..9 on Windows; or 0 is platform default.
  66. HostName string // Hostname
  67. MappedDirectories []mappedDir // List of mapped directories (volumes/mounts)
  68. }
  69. // defaultOwner is a tag passed to HCS to allow it to differentiate between
  70. // container creator management stacks. We hard code "docker" in the case
  71. // of docker.
  72. const defaultOwner = "docker"
  73. // Run implements the exec driver Driver interface
  74. func (d *Driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, hooks execdriver.Hooks) (execdriver.ExitStatus, error) {
  75. var (
  76. term execdriver.Terminal
  77. err error
  78. )
  79. // Make sure the client isn't asking for options which aren't supported
  80. err = checkSupportedOptions(c)
  81. if err != nil {
  82. return execdriver.ExitStatus{ExitCode: -1}, err
  83. }
  84. cu := &containerInit{
  85. SystemType: "Container",
  86. Name: c.ID,
  87. Owner: defaultOwner,
  88. IsDummy: dummyMode,
  89. VolumePath: c.Rootfs,
  90. IgnoreFlushesDuringBoot: c.FirstStart,
  91. LayerFolderPath: c.LayerFolder,
  92. ProcessorWeight: c.Resources.CPUShares,
  93. HostName: c.Hostname,
  94. }
  95. for _, layerPath := range c.LayerPaths {
  96. _, filename := filepath.Split(layerPath)
  97. g, err := hcsshim.NameToGuid(filename)
  98. if err != nil {
  99. return execdriver.ExitStatus{ExitCode: -1}, err
  100. }
  101. cu.Layers = append(cu.Layers, layer{
  102. ID: g.ToString(),
  103. Path: layerPath,
  104. })
  105. }
  106. // Add the mounts (volumes, bind mounts etc) to the structure
  107. mds := make([]mappedDir, len(c.Mounts))
  108. for i, mount := range c.Mounts {
  109. mds[i] = mappedDir{
  110. HostPath: mount.Source,
  111. ContainerPath: mount.Destination,
  112. ReadOnly: !mount.Writable}
  113. }
  114. cu.MappedDirectories = mds
  115. // TODO Windows. At some point, when there is CLI on docker run to
  116. // enable the IP Address of the container to be passed into docker run,
  117. // the IP Address needs to be wired through to HCS in the JSON. It
  118. // would be present in c.Network.Interface.IPAddress. See matching
  119. // TODO in daemon\container_windows.go, function populateCommand.
  120. if c.Network.Interface != nil {
  121. var pbs []portBinding
  122. // Enumerate through the port bindings specified by the user and convert
  123. // them into the internal structure matching the JSON blob that can be
  124. // understood by the HCS.
  125. for i, v := range c.Network.Interface.PortBindings {
  126. proto := strings.ToUpper(i.Proto())
  127. if proto != "TCP" && proto != "UDP" {
  128. return execdriver.ExitStatus{ExitCode: -1}, fmt.Errorf("invalid protocol %s", i.Proto())
  129. }
  130. if len(v) > 1 {
  131. return execdriver.ExitStatus{ExitCode: -1}, fmt.Errorf("Windows does not support more than one host port in NAT settings")
  132. }
  133. for _, v2 := range v {
  134. var (
  135. iPort, ePort int
  136. err error
  137. )
  138. if len(v2.HostIP) != 0 {
  139. return execdriver.ExitStatus{ExitCode: -1}, fmt.Errorf("Windows does not support host IP addresses in NAT settings")
  140. }
  141. if ePort, err = strconv.Atoi(v2.HostPort); err != nil {
  142. return execdriver.ExitStatus{ExitCode: -1}, fmt.Errorf("invalid container port %s: %s", v2.HostPort, err)
  143. }
  144. if iPort, err = strconv.Atoi(i.Port()); err != nil {
  145. return execdriver.ExitStatus{ExitCode: -1}, fmt.Errorf("invalid internal port %s: %s", i.Port(), err)
  146. }
  147. if iPort < 0 || iPort > 65535 || ePort < 0 || ePort > 65535 {
  148. return execdriver.ExitStatus{ExitCode: -1}, fmt.Errorf("specified NAT port is not in allowed range")
  149. }
  150. pbs = append(pbs,
  151. portBinding{ExternalPort: ePort,
  152. InternalPort: iPort,
  153. Protocol: proto})
  154. }
  155. }
  156. // TODO Windows: TP3 workaround. Allow the user to override the name of
  157. // the Container NAT device through an environment variable. This will
  158. // ultimately be a global daemon parameter on Windows, similar to -b
  159. // for the name of the virtual switch (aka bridge).
  160. cn := os.Getenv("DOCKER_CONTAINER_NAT")
  161. if len(cn) == 0 {
  162. cn = defaultContainerNAT
  163. }
  164. dev := device{
  165. DeviceType: "Network",
  166. Connection: &networkConnection{
  167. NetworkName: c.Network.Interface.Bridge,
  168. // TODO Windows: Fixme, next line. Needs HCS fix.
  169. EnableNat: false,
  170. Nat: natSettings{
  171. Name: cn,
  172. PortBindings: pbs,
  173. },
  174. },
  175. }
  176. if c.Network.Interface.MacAddress != "" {
  177. windowsStyleMAC := strings.Replace(
  178. c.Network.Interface.MacAddress, ":", "-", -1)
  179. dev.Settings = networkSettings{
  180. MacAddress: windowsStyleMAC,
  181. }
  182. }
  183. cu.Devices = append(cu.Devices, dev)
  184. } else {
  185. logrus.Debugln("No network interface")
  186. }
  187. configurationb, err := json.Marshal(cu)
  188. if err != nil {
  189. return execdriver.ExitStatus{ExitCode: -1}, err
  190. }
  191. configuration := string(configurationb)
  192. err = hcsshim.CreateComputeSystem(c.ID, configuration)
  193. if err != nil {
  194. logrus.Debugln("Failed to create temporary container ", err)
  195. return execdriver.ExitStatus{ExitCode: -1}, err
  196. }
  197. // Start the container
  198. logrus.Debugln("Starting container ", c.ID)
  199. err = hcsshim.StartComputeSystem(c.ID)
  200. if err != nil {
  201. logrus.Errorf("Failed to start compute system: %s", err)
  202. return execdriver.ExitStatus{ExitCode: -1}, err
  203. }
  204. defer func() {
  205. // Stop the container
  206. if forceKill {
  207. logrus.Debugf("Forcibly terminating container %s", c.ID)
  208. if errno, err := hcsshim.TerminateComputeSystem(c.ID, hcsshim.TimeoutInfinite, "exec-run-defer"); err != nil {
  209. logrus.Warnf("Ignoring error from TerminateComputeSystem 0x%X %s", errno, err)
  210. }
  211. } else {
  212. logrus.Debugf("Shutting down container %s", c.ID)
  213. if errno, err := hcsshim.ShutdownComputeSystem(c.ID, hcsshim.TimeoutInfinite, "exec-run-defer"); err != nil {
  214. if errno != hcsshim.Win32SystemShutdownIsInProgress &&
  215. errno != hcsshim.Win32SpecifiedPathInvalid &&
  216. errno != hcsshim.Win32SystemCannotFindThePathSpecified {
  217. logrus.Warnf("Ignoring error from ShutdownComputeSystem 0x%X %s", errno, err)
  218. }
  219. }
  220. }
  221. }()
  222. createProcessParms := hcsshim.CreateProcessParams{
  223. EmulateConsole: c.ProcessConfig.Tty,
  224. WorkingDirectory: c.WorkingDir,
  225. ConsoleSize: c.ProcessConfig.ConsoleSize,
  226. }
  227. // Configure the environment for the process
  228. createProcessParms.Environment = setupEnvironmentVariables(c.ProcessConfig.Env)
  229. // This should get caught earlier, but just in case - validate that we
  230. // have something to run
  231. if c.ProcessConfig.Entrypoint == "" {
  232. err = errors.New("No entrypoint specified")
  233. logrus.Error(err)
  234. return execdriver.ExitStatus{ExitCode: -1}, err
  235. }
  236. // Build the command line of the process
  237. createProcessParms.CommandLine = c.ProcessConfig.Entrypoint
  238. for _, arg := range c.ProcessConfig.Arguments {
  239. logrus.Debugln("appending ", arg)
  240. createProcessParms.CommandLine += " " + syscall.EscapeArg(arg)
  241. }
  242. logrus.Debugf("CommandLine: %s", createProcessParms.CommandLine)
  243. // Start the command running in the container.
  244. pid, stdin, stdout, stderr, err := hcsshim.CreateProcessInComputeSystem(c.ID, pipes.Stdin != nil, true, !c.ProcessConfig.Tty, createProcessParms)
  245. if err != nil {
  246. logrus.Errorf("CreateProcessInComputeSystem() failed %s", err)
  247. return execdriver.ExitStatus{ExitCode: -1}, err
  248. }
  249. // Now that the process has been launched, begin copying data to and from
  250. // the named pipes for the std handles.
  251. setupPipes(stdin, stdout, stderr, pipes)
  252. //Save the PID as we'll need this in Kill()
  253. logrus.Debugf("PID %d", pid)
  254. c.ContainerPid = int(pid)
  255. if c.ProcessConfig.Tty {
  256. term = NewTtyConsole(c.ID, pid)
  257. } else {
  258. term = NewStdConsole()
  259. }
  260. c.ProcessConfig.Terminal = term
  261. // Maintain our list of active containers. We'll need this later for exec
  262. // and other commands.
  263. d.Lock()
  264. d.activeContainers[c.ID] = &activeContainer{
  265. command: c,
  266. }
  267. d.Unlock()
  268. if hooks.Start != nil {
  269. // A closed channel for OOM is returned here as it will be
  270. // non-blocking and return the correct result when read.
  271. chOOM := make(chan struct{})
  272. close(chOOM)
  273. hooks.Start(&c.ProcessConfig, int(pid), chOOM)
  274. }
  275. var (
  276. exitCode int32
  277. errno uint32
  278. )
  279. exitCode, errno, err = hcsshim.WaitForProcessInComputeSystem(c.ID, pid, hcsshim.TimeoutInfinite)
  280. if err != nil {
  281. if errno != hcsshim.Win32PipeHasBeenEnded {
  282. logrus.Warnf("WaitForProcessInComputeSystem failed (container may have been killed): %s", err)
  283. }
  284. // Do NOT return err here as the container would have
  285. // started, otherwise docker will deadlock. It's perfectly legitimate
  286. // for WaitForProcessInComputeSystem to fail in situations such
  287. // as the container being killed on another thread.
  288. return execdriver.ExitStatus{ExitCode: hcsshim.WaitErrExecFailed}, nil
  289. }
  290. logrus.Debugf("Exiting Run() exitCode %d id=%s", exitCode, c.ID)
  291. return execdriver.ExitStatus{ExitCode: int(exitCode)}, nil
  292. }
  293. // SupportsHooks implements the execdriver Driver interface.
  294. // The windows driver does not support the hook mechanism
  295. func (d *Driver) SupportsHooks() bool {
  296. return false
  297. }