dispatchers.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package builder
  2. // This file contains the dispatchers for each command. Note that
  3. // `nullDispatch` is not actually a command, but support for commands we parse
  4. // but do nothing with.
  5. //
  6. // See evaluator.go for a higher level discussion of the whole evaluator
  7. // package.
  8. import (
  9. "fmt"
  10. "path/filepath"
  11. "strings"
  12. "github.com/docker/docker/nat"
  13. "github.com/docker/docker/pkg/log"
  14. "github.com/docker/docker/runconfig"
  15. )
  16. // dispatch with no layer / parsing. This is effectively not a command.
  17. func nullDispatch(b *Builder, args []string, attributes map[string]bool) error {
  18. return nil
  19. }
  20. // ENV foo bar
  21. //
  22. // Sets the environment variable foo to bar, also makes interpolation
  23. // in the dockerfile available from the next statement on via ${foo}.
  24. //
  25. func env(b *Builder, args []string, attributes map[string]bool) error {
  26. if len(args) != 2 {
  27. return fmt.Errorf("ENV accepts two arguments")
  28. }
  29. fullEnv := fmt.Sprintf("%s=%s", args[0], args[1])
  30. for i, envVar := range b.Config.Env {
  31. envParts := strings.SplitN(envVar, "=", 2)
  32. if args[0] == envParts[0] {
  33. b.Config.Env[i] = fullEnv
  34. return b.commit("", b.Config.Cmd, fmt.Sprintf("ENV %s", fullEnv))
  35. }
  36. }
  37. b.Config.Env = append(b.Config.Env, fullEnv)
  38. return b.commit("", b.Config.Cmd, fmt.Sprintf("ENV %s", fullEnv))
  39. }
  40. // MAINTAINER some text <maybe@an.email.address>
  41. //
  42. // Sets the maintainer metadata.
  43. func maintainer(b *Builder, args []string, attributes map[string]bool) error {
  44. if len(args) != 1 {
  45. return fmt.Errorf("MAINTAINER requires only one argument")
  46. }
  47. b.maintainer = args[0]
  48. return b.commit("", b.Config.Cmd, fmt.Sprintf("MAINTAINER %s", b.maintainer))
  49. }
  50. // ADD foo /path
  51. //
  52. // Add the file 'foo' to '/path'. Tarball and Remote URL (git, http) handling
  53. // exist here. If you do not wish to have this automatic handling, use COPY.
  54. //
  55. func add(b *Builder, args []string, attributes map[string]bool) error {
  56. if len(args) != 2 {
  57. return fmt.Errorf("ADD requires two arguments")
  58. }
  59. return b.runContextCommand(args, true, true, "ADD")
  60. }
  61. // COPY foo /path
  62. //
  63. // Same as 'ADD' but without the tar and remote url handling.
  64. //
  65. func dispatchCopy(b *Builder, args []string, attributes map[string]bool) error {
  66. if len(args) != 2 {
  67. return fmt.Errorf("COPY requires two arguments")
  68. }
  69. return b.runContextCommand(args, false, false, "COPY")
  70. }
  71. // FROM imagename
  72. //
  73. // This sets the image the dockerfile will build on top of.
  74. //
  75. func from(b *Builder, args []string, attributes map[string]bool) error {
  76. if len(args) != 1 {
  77. return fmt.Errorf("FROM requires one argument")
  78. }
  79. name := args[0]
  80. image, err := b.Daemon.Repositories().LookupImage(name)
  81. if err != nil {
  82. if b.Daemon.Graph().IsNotExist(err) {
  83. image, err = b.pullImage(name)
  84. }
  85. // note that the top level err will still be !nil here if IsNotExist is
  86. // not the error. This approach just simplifies hte logic a bit.
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. return b.processImageFrom(image)
  92. }
  93. // ONBUILD RUN echo yo
  94. //
  95. // ONBUILD triggers run when the image is used in a FROM statement.
  96. //
  97. // ONBUILD handling has a lot of special-case functionality, the heading in
  98. // evaluator.go and comments around dispatch() in the same file explain the
  99. // special cases. search for 'OnBuild' in internals.go for additional special
  100. // cases.
  101. //
  102. func onbuild(b *Builder, args []string, attributes map[string]bool) error {
  103. triggerInstruction := strings.ToUpper(strings.TrimSpace(args[0]))
  104. switch triggerInstruction {
  105. case "ONBUILD":
  106. return fmt.Errorf("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
  107. case "MAINTAINER", "FROM":
  108. return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", triggerInstruction)
  109. }
  110. trigger := strings.Join(args, " ")
  111. b.Config.OnBuild = append(b.Config.OnBuild, trigger)
  112. return b.commit("", b.Config.Cmd, fmt.Sprintf("ONBUILD %s", trigger))
  113. }
  114. // WORKDIR /tmp
  115. //
  116. // Set the working directory for future RUN/CMD/etc statements.
  117. //
  118. func workdir(b *Builder, args []string, attributes map[string]bool) error {
  119. if len(args) != 1 {
  120. return fmt.Errorf("WORKDIR requires exactly one argument")
  121. }
  122. workdir := args[0]
  123. if workdir[0] == '/' {
  124. b.Config.WorkingDir = workdir
  125. } else {
  126. if b.Config.WorkingDir == "" {
  127. b.Config.WorkingDir = "/"
  128. }
  129. b.Config.WorkingDir = filepath.Join(b.Config.WorkingDir, workdir)
  130. }
  131. return b.commit("", b.Config.Cmd, fmt.Sprintf("WORKDIR %v", workdir))
  132. }
  133. // RUN some command yo
  134. //
  135. // run a command and commit the image. Args are automatically prepended with
  136. // 'sh -c' in the event there is only one argument. The difference in
  137. // processing:
  138. //
  139. // RUN echo hi # sh -c echo hi
  140. // RUN [ "echo", "hi" ] # echo hi
  141. //
  142. func run(b *Builder, args []string, attributes map[string]bool) error {
  143. args = handleJsonArgs(args, attributes)
  144. if b.image == "" {
  145. return fmt.Errorf("Please provide a source image with `from` prior to run")
  146. }
  147. config, _, _, err := runconfig.Parse(append([]string{b.image}, args...), nil)
  148. if err != nil {
  149. return err
  150. }
  151. cmd := b.Config.Cmd
  152. // set Cmd manually, this is special case only for Dockerfiles
  153. b.Config.Cmd = config.Cmd
  154. runconfig.Merge(b.Config, config)
  155. defer func(cmd []string) { b.Config.Cmd = cmd }(cmd)
  156. log.Debugf("Command to be executed: %v", b.Config.Cmd)
  157. hit, err := b.probeCache()
  158. if err != nil {
  159. return err
  160. }
  161. if hit {
  162. return nil
  163. }
  164. c, err := b.create()
  165. if err != nil {
  166. return err
  167. }
  168. // Ensure that we keep the container mounted until the commit
  169. // to avoid unmounting and then mounting directly again
  170. c.Mount()
  171. defer c.Unmount()
  172. err = b.run(c)
  173. if err != nil {
  174. return err
  175. }
  176. if err := b.commit(c.ID, cmd, "run"); err != nil {
  177. return err
  178. }
  179. return nil
  180. }
  181. // CMD foo
  182. //
  183. // Set the default command to run in the container (which may be empty).
  184. // Argument handling is the same as RUN.
  185. //
  186. func cmd(b *Builder, args []string, attributes map[string]bool) error {
  187. b.Config.Cmd = handleJsonArgs(args, attributes)
  188. if err := b.commit("", b.Config.Cmd, fmt.Sprintf("CMD %v", cmd)); err != nil {
  189. return err
  190. }
  191. b.cmdSet = true
  192. return nil
  193. }
  194. // ENTRYPOINT /usr/sbin/nginx
  195. //
  196. // Set the entrypoint (which defaults to sh -c) to /usr/sbin/nginx. Will
  197. // accept the CMD as the arguments to /usr/sbin/nginx.
  198. //
  199. // Handles command processing similar to CMD and RUN, only b.Config.Entrypoint
  200. // is initialized at NewBuilder time instead of through argument parsing.
  201. //
  202. func entrypoint(b *Builder, args []string, attributes map[string]bool) error {
  203. b.Config.Entrypoint = handleJsonArgs(args, attributes)
  204. // if there is no cmd in current Dockerfile - cleanup cmd
  205. if !b.cmdSet {
  206. b.Config.Cmd = nil
  207. }
  208. if err := b.commit("", b.Config.Cmd, fmt.Sprintf("ENTRYPOINT %v", b.Config.Entrypoint)); err != nil {
  209. return err
  210. }
  211. return nil
  212. }
  213. // EXPOSE 6667/tcp 7000/tcp
  214. //
  215. // Expose ports for links and port mappings. This all ends up in
  216. // b.Config.ExposedPorts for runconfig.
  217. //
  218. func expose(b *Builder, args []string, attributes map[string]bool) error {
  219. portsTab := args
  220. if b.Config.ExposedPorts == nil {
  221. b.Config.ExposedPorts = make(nat.PortSet)
  222. }
  223. ports, _, err := nat.ParsePortSpecs(append(portsTab, b.Config.PortSpecs...))
  224. if err != nil {
  225. return err
  226. }
  227. for port := range ports {
  228. if _, exists := b.Config.ExposedPorts[port]; !exists {
  229. b.Config.ExposedPorts[port] = struct{}{}
  230. }
  231. }
  232. b.Config.PortSpecs = nil
  233. return b.commit("", b.Config.Cmd, fmt.Sprintf("EXPOSE %v", ports))
  234. }
  235. // USER foo
  236. //
  237. // Set the user to 'foo' for future commands and when running the
  238. // ENTRYPOINT/CMD at container run time.
  239. //
  240. func user(b *Builder, args []string, attributes map[string]bool) error {
  241. if len(args) != 1 {
  242. return fmt.Errorf("USER requires exactly one argument")
  243. }
  244. b.Config.User = args[0]
  245. return b.commit("", b.Config.Cmd, fmt.Sprintf("USER %v", args))
  246. }
  247. // VOLUME /foo
  248. //
  249. // Expose the volume /foo for use. Will also accept the JSON form, but either
  250. // way requires exactly one argument.
  251. //
  252. func volume(b *Builder, args []string, attributes map[string]bool) error {
  253. if len(args) != 1 {
  254. return fmt.Errorf("Volume cannot be empty")
  255. }
  256. volume := args
  257. if b.Config.Volumes == nil {
  258. b.Config.Volumes = map[string]struct{}{}
  259. }
  260. for _, v := range volume {
  261. b.Config.Volumes[v] = struct{}{}
  262. }
  263. if err := b.commit("", b.Config.Cmd, fmt.Sprintf("VOLUME %s", args)); err != nil {
  264. return err
  265. }
  266. return nil
  267. }
  268. // INSERT is no longer accepted, but we still parse it.
  269. func insert(b *Builder, args []string, attributes map[string]bool) error {
  270. return fmt.Errorf("INSERT has been deprecated. Please use ADD instead")
  271. }