run.go 10 KB

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