evaluator.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Package dockerfile is the evaluation step in the Dockerfile parse/evaluate pipeline.
  2. //
  3. // It incorporates a dispatch table based on the parser.Node values (see the
  4. // parser package for more information) that are yielded from the parser itself.
  5. // Calling NewBuilder with the BuildOpts struct can be used to customize the
  6. // experience for execution purposes only. Parsing is controlled in the parser
  7. // package, and this division of responsibility should be respected.
  8. //
  9. // Please see the jump table targets for the actual invocations, most of which
  10. // will call out to the functions in internals.go to deal with their tasks.
  11. //
  12. // ONBUILD is a special case, which is covered in the onbuild() func in
  13. // dispatchers.go.
  14. //
  15. // The evaluator uses the concept of "steps", which are usually each processable
  16. // line in the Dockerfile. Each step is numbered and certain actions are taken
  17. // before and after each step, such as creating an image ID and removing temporary
  18. // containers and images. Note that ONBUILD creates a kinda-sorta "sub run" which
  19. // includes its own set of steps (usually only one of them).
  20. package dockerfile
  21. import (
  22. "fmt"
  23. "strings"
  24. "github.com/docker/docker/builder/dockerfile/command"
  25. "github.com/docker/docker/builder/dockerfile/parser"
  26. )
  27. // Environment variable interpolation will happen on these statements only.
  28. var replaceEnvAllowed = map[string]bool{
  29. command.Env: true,
  30. command.Label: true,
  31. command.Add: true,
  32. command.Copy: true,
  33. command.Workdir: true,
  34. command.Expose: true,
  35. command.Volume: true,
  36. command.User: true,
  37. command.StopSignal: true,
  38. command.Arg: true,
  39. }
  40. // Certain commands are allowed to have their args split into more
  41. // words after env var replacements. Meaning:
  42. // ENV foo="123 456"
  43. // EXPOSE $foo
  44. // should result in the same thing as:
  45. // EXPOSE 123 456
  46. // and not treat "123 456" as a single word.
  47. // Note that: EXPOSE "$foo" and EXPOSE $foo are not the same thing.
  48. // Quotes will cause it to still be treated as single word.
  49. var allowWordExpansion = map[string]bool{
  50. command.Expose: true,
  51. }
  52. var evaluateTable map[string]func(*Builder, []string, map[string]bool, string) error
  53. func init() {
  54. evaluateTable = map[string]func(*Builder, []string, map[string]bool, string) error{
  55. command.Env: env,
  56. command.Label: label,
  57. command.Maintainer: maintainer,
  58. command.Add: add,
  59. command.Copy: dispatchCopy, // copy() is a go builtin
  60. command.From: from,
  61. command.Onbuild: onbuild,
  62. command.Workdir: workdir,
  63. command.Run: run,
  64. command.Cmd: cmd,
  65. command.Entrypoint: entrypoint,
  66. command.Expose: expose,
  67. command.Volume: volume,
  68. command.User: user,
  69. command.StopSignal: stopSignal,
  70. command.Arg: arg,
  71. command.Healthcheck: healthcheck,
  72. }
  73. }
  74. // This method is the entrypoint to all statement handling routines.
  75. //
  76. // Almost all nodes will have this structure:
  77. // Child[Node, Node, Node] where Child is from parser.Node.Children and each
  78. // node comes from parser.Node.Next. This forms a "line" with a statement and
  79. // arguments and we process them in this normalized form by hitting
  80. // evaluateTable with the leaf nodes of the command and the Builder object.
  81. //
  82. // ONBUILD is a special case; in this case the parser will emit:
  83. // Child[Node, Child[Node, Node...]] where the first node is the literal
  84. // "onbuild" and the child entrypoint is the command of the ONBUILD statement,
  85. // such as `RUN` in ONBUILD RUN foo. There is special case logic in here to
  86. // deal with that, at least until it becomes more of a general concern with new
  87. // features.
  88. func (b *Builder) dispatch(stepN int, ast *parser.Node) error {
  89. cmd := ast.Value
  90. upperCasedCmd := strings.ToUpper(cmd)
  91. // To ensure the user is given a decent error message if the platform
  92. // on which the daemon is running does not support a builder command.
  93. if err := platformSupports(strings.ToLower(cmd)); err != nil {
  94. return err
  95. }
  96. attrs := ast.Attributes
  97. original := ast.Original
  98. flags := ast.Flags
  99. strList := []string{}
  100. msg := fmt.Sprintf("Step %d : %s", stepN+1, upperCasedCmd)
  101. if len(ast.Flags) > 0 {
  102. msg += " " + strings.Join(ast.Flags, " ")
  103. }
  104. if cmd == "onbuild" {
  105. if ast.Next == nil {
  106. return fmt.Errorf("ONBUILD requires at least one argument")
  107. }
  108. ast = ast.Next.Children[0]
  109. strList = append(strList, ast.Value)
  110. msg += " " + ast.Value
  111. if len(ast.Flags) > 0 {
  112. msg += " " + strings.Join(ast.Flags, " ")
  113. }
  114. }
  115. // count the number of nodes that we are going to traverse first
  116. // so we can pre-create the argument and message array. This speeds up the
  117. // allocation of those list a lot when they have a lot of arguments
  118. cursor := ast
  119. var n int
  120. for cursor.Next != nil {
  121. cursor = cursor.Next
  122. n++
  123. }
  124. msgList := make([]string, n)
  125. var i int
  126. // Append the build-time args to config-environment.
  127. // This allows builder config to override the variables, making the behavior similar to
  128. // a shell script i.e. `ENV foo bar` overrides value of `foo` passed in build
  129. // context. But `ENV foo $foo` will use the value from build context if one
  130. // isn't already been defined by a previous ENV primitive.
  131. // Note, we get this behavior because we know that ProcessWord() will
  132. // stop on the first occurrence of a variable name and not notice
  133. // a subsequent one. So, putting the buildArgs list after the Config.Env
  134. // list, in 'envs', is safe.
  135. envs := b.runConfig.Env
  136. for key, val := range b.options.BuildArgs {
  137. if !b.isBuildArgAllowed(key) {
  138. // skip build-args that are not in allowed list, meaning they have
  139. // not been defined by an "ARG" Dockerfile command yet.
  140. // This is an error condition but only if there is no "ARG" in the entire
  141. // Dockerfile, so we'll generate any necessary errors after we parsed
  142. // the entire file (see 'leftoverArgs' processing in evaluator.go )
  143. continue
  144. }
  145. envs = append(envs, fmt.Sprintf("%s=%s", key, val))
  146. }
  147. for ast.Next != nil {
  148. ast = ast.Next
  149. var str string
  150. str = ast.Value
  151. if replaceEnvAllowed[cmd] {
  152. var err error
  153. var words []string
  154. if allowWordExpansion[cmd] {
  155. words, err = ProcessWords(str, envs)
  156. if err != nil {
  157. return err
  158. }
  159. strList = append(strList, words...)
  160. } else {
  161. str, err = ProcessWord(str, envs)
  162. if err != nil {
  163. return err
  164. }
  165. strList = append(strList, str)
  166. }
  167. } else {
  168. strList = append(strList, str)
  169. }
  170. msgList[i] = ast.Value
  171. i++
  172. }
  173. msg += " " + strings.Join(msgList, " ")
  174. fmt.Fprintln(b.Stdout, msg)
  175. // XXX yes, we skip any cmds that are not valid; the parser should have
  176. // picked these out already.
  177. if f, ok := evaluateTable[cmd]; ok {
  178. b.flags = NewBFlags()
  179. b.flags.Args = flags
  180. return f(b, strList, attrs, original)
  181. }
  182. return fmt.Errorf("Unknown instruction: %s", upperCasedCmd)
  183. }