evaluator.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 resposibility 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. "runtime"
  24. "strings"
  25. "github.com/docker/docker/builder/dockerfile/command"
  26. "github.com/docker/docker/builder/dockerfile/parser"
  27. )
  28. // Environment variable interpolation will happen on these statements only.
  29. var replaceEnvAllowed = map[string]struct{}{
  30. command.Env: {},
  31. command.Label: {},
  32. command.Add: {},
  33. command.Copy: {},
  34. command.Workdir: {},
  35. command.Expose: {},
  36. command.Volume: {},
  37. command.User: {},
  38. command.StopSignal: {},
  39. command.Arg: {},
  40. }
  41. var evaluateTable map[string]func(*Builder, []string, map[string]bool, string) error
  42. func init() {
  43. evaluateTable = map[string]func(*Builder, []string, map[string]bool, string) error{
  44. command.Env: env,
  45. command.Label: label,
  46. command.Maintainer: maintainer,
  47. command.Add: add,
  48. command.Copy: dispatchCopy, // copy() is a go builtin
  49. command.From: from,
  50. command.Onbuild: onbuild,
  51. command.Workdir: workdir,
  52. command.Run: run,
  53. command.Cmd: cmd,
  54. command.Entrypoint: entrypoint,
  55. command.Expose: expose,
  56. command.Volume: volume,
  57. command.User: user,
  58. command.StopSignal: stopSignal,
  59. command.Arg: arg,
  60. }
  61. }
  62. // This method is the entrypoint to all statement handling routines.
  63. //
  64. // Almost all nodes will have this structure:
  65. // Child[Node, Node, Node] where Child is from parser.Node.Children and each
  66. // node comes from parser.Node.Next. This forms a "line" with a statement and
  67. // arguments and we process them in this normalized form by hitting
  68. // evaluateTable with the leaf nodes of the command and the Builder object.
  69. //
  70. // ONBUILD is a special case; in this case the parser will emit:
  71. // Child[Node, Child[Node, Node...]] where the first node is the literal
  72. // "onbuild" and the child entrypoint is the command of the ONBUILD statement,
  73. // such as `RUN` in ONBUILD RUN foo. There is special case logic in here to
  74. // deal with that, at least until it becomes more of a general concern with new
  75. // features.
  76. func (b *Builder) dispatch(stepN int, ast *parser.Node) error {
  77. cmd := ast.Value
  78. upperCasedCmd := strings.ToUpper(cmd)
  79. // To ensure the user is given a decent error message if the platform
  80. // on which the daemon is running does not support a builder command.
  81. if err := platformSupports(strings.ToLower(cmd)); err != nil {
  82. return err
  83. }
  84. attrs := ast.Attributes
  85. original := ast.Original
  86. flags := ast.Flags
  87. strs := []string{}
  88. msg := fmt.Sprintf("Step %d : %s", stepN+1, upperCasedCmd)
  89. if len(ast.Flags) > 0 {
  90. msg += " " + strings.Join(ast.Flags, " ")
  91. }
  92. if cmd == "onbuild" {
  93. if ast.Next == nil {
  94. return fmt.Errorf("ONBUILD requires at least one argument")
  95. }
  96. ast = ast.Next.Children[0]
  97. strs = append(strs, ast.Value)
  98. msg += " " + ast.Value
  99. if len(ast.Flags) > 0 {
  100. msg += " " + strings.Join(ast.Flags, " ")
  101. }
  102. }
  103. // count the number of nodes that we are going to traverse first
  104. // so we can pre-create the argument and message array. This speeds up the
  105. // allocation of those list a lot when they have a lot of arguments
  106. cursor := ast
  107. var n int
  108. for cursor.Next != nil {
  109. cursor = cursor.Next
  110. n++
  111. }
  112. l := len(strs)
  113. strList := make([]string, n+l)
  114. copy(strList, strs)
  115. msgList := make([]string, n)
  116. var i int
  117. // Append the build-time args to config-environment.
  118. // This allows builder config to override the variables, making the behavior similar to
  119. // a shell script i.e. `ENV foo bar` overrides value of `foo` passed in build
  120. // context. But `ENV foo $foo` will use the value from build context if one
  121. // isn't already been defined by a previous ENV primitive.
  122. // Note, we get this behavior because we know that ProcessWord() will
  123. // stop on the first occurrence of a variable name and not notice
  124. // a subsequent one. So, putting the buildArgs list after the Config.Env
  125. // list, in 'envs', is safe.
  126. envs := b.runConfig.Env
  127. for key, val := range b.BuildArgs {
  128. if !b.isBuildArgAllowed(key) {
  129. // skip build-args that are not in allowed list, meaning they have
  130. // not been defined by an "ARG" Dockerfile command yet.
  131. // This is an error condition but only if there is no "ARG" in the entire
  132. // Dockerfile, so we'll generate any necessary errors after we parsed
  133. // the entire file (see 'leftoverArgs' processing in evaluator.go )
  134. continue
  135. }
  136. envs = append(envs, fmt.Sprintf("%s=%s", key, val))
  137. }
  138. for ast.Next != nil {
  139. ast = ast.Next
  140. var str string
  141. str = ast.Value
  142. if _, ok := replaceEnvAllowed[cmd]; ok {
  143. var err error
  144. str, err = ProcessWord(ast.Value, envs)
  145. if err != nil {
  146. return err
  147. }
  148. }
  149. strList[i+l] = str
  150. msgList[i] = ast.Value
  151. i++
  152. }
  153. msg += " " + strings.Join(msgList, " ")
  154. fmt.Fprintln(b.Stdout, msg)
  155. // XXX yes, we skip any cmds that are not valid; the parser should have
  156. // picked these out already.
  157. if f, ok := evaluateTable[cmd]; ok {
  158. b.flags = NewBFlags()
  159. b.flags.Args = flags
  160. return f(b, strList, attrs, original)
  161. }
  162. return fmt.Errorf("Unknown instruction: %s", upperCasedCmd)
  163. }
  164. // platformSupports is a short-term function to give users a quality error
  165. // message if a Dockerfile uses a command not supported on the platform.
  166. func platformSupports(command string) error {
  167. if runtime.GOOS != "windows" {
  168. return nil
  169. }
  170. switch command {
  171. case "expose", "user", "stopsignal", "arg":
  172. return fmt.Errorf("The daemon on this platform does not support the command '%s'", command)
  173. }
  174. return nil
  175. }