2015-09-05 15:49:06 -04:00
|
|
|
// Package dockerfile is the evaluation step in the Dockerfile parse/evaluate pipeline.
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
|
|
|
// It incorporates a dispatch table based on the parser.Node values (see the
|
|
|
|
// parser package for more information) that are yielded from the parser itself.
|
|
|
|
// Calling NewBuilder with the BuildOpts struct can be used to customize the
|
|
|
|
// experience for execution purposes only. Parsing is controlled in the parser
|
2015-12-13 11:00:39 -05:00
|
|
|
// package, and this division of responsibility should be respected.
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
|
|
|
// Please see the jump table targets for the actual invocations, most of which
|
|
|
|
// will call out to the functions in internals.go to deal with their tasks.
|
|
|
|
//
|
|
|
|
// ONBUILD is a special case, which is covered in the onbuild() func in
|
|
|
|
// dispatchers.go.
|
|
|
|
//
|
|
|
|
// The evaluator uses the concept of "steps", which are usually each processable
|
|
|
|
// line in the Dockerfile. Each step is numbered and certain actions are taken
|
|
|
|
// before and after each step, such as creating an image ID and removing temporary
|
|
|
|
// containers and images. Note that ONBUILD creates a kinda-sorta "sub run" which
|
|
|
|
// includes its own set of steps (usually only one of them).
|
2015-09-05 15:49:06 -04:00
|
|
|
package dockerfile
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2015-09-05 15:49:06 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile/command"
|
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2014-08-05 16:17:40 -04:00
|
|
|
)
|
|
|
|
|
2014-10-25 14:29:18 -04:00
|
|
|
// Environment variable interpolation will happen on these statements only.
|
2015-11-10 15:57:51 -05:00
|
|
|
var replaceEnvAllowed = map[string]bool{
|
|
|
|
command.Env: true,
|
|
|
|
command.Label: true,
|
|
|
|
command.Add: true,
|
|
|
|
command.Copy: true,
|
|
|
|
command.Workdir: true,
|
|
|
|
command.Expose: true,
|
|
|
|
command.Volume: true,
|
|
|
|
command.User: true,
|
|
|
|
command.StopSignal: true,
|
|
|
|
command.Arg: true,
|
2014-10-25 14:29:18 -04:00
|
|
|
}
|
|
|
|
|
2015-11-07 15:05:55 -05:00
|
|
|
// Certain commands are allowed to have their args split into more
|
|
|
|
// words after env var replacements. Meaning:
|
|
|
|
// ENV foo="123 456"
|
|
|
|
// EXPOSE $foo
|
|
|
|
// should result in the same thing as:
|
|
|
|
// EXPOSE 123 456
|
|
|
|
// and not treat "123 456" as a single word.
|
|
|
|
// Note that: EXPOSE "$foo" and EXPOSE $foo are not the same thing.
|
|
|
|
// Quotes will cause it to still be treated as single word.
|
|
|
|
var allowWordExpansion = map[string]bool{
|
|
|
|
command.Expose: true,
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
var evaluateTable map[string]func(*Builder, []string, map[string]bool, string) error
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
func init() {
|
2015-09-06 13:26:40 -04:00
|
|
|
evaluateTable = map[string]func(*Builder, []string, map[string]bool, string) error{
|
2016-04-18 05:48:13 -04:00
|
|
|
command.Add: add,
|
2016-05-03 16:56:59 -04:00
|
|
|
command.Arg: arg,
|
|
|
|
command.Cmd: cmd,
|
2016-04-18 05:48:13 -04:00
|
|
|
command.Copy: dispatchCopy, // copy() is a go builtin
|
2016-05-03 16:56:59 -04:00
|
|
|
command.Entrypoint: entrypoint,
|
|
|
|
command.Env: env,
|
|
|
|
command.Expose: expose,
|
2016-04-18 05:48:13 -04:00
|
|
|
command.From: from,
|
2016-05-03 16:56:59 -04:00
|
|
|
command.Healthcheck: healthcheck,
|
|
|
|
command.Label: label,
|
|
|
|
command.Maintainer: maintainer,
|
2016-04-18 05:48:13 -04:00
|
|
|
command.Onbuild: onbuild,
|
|
|
|
command.Run: run,
|
2016-05-03 16:56:59 -04:00
|
|
|
command.Shell: shell,
|
2016-04-18 05:48:13 -04:00
|
|
|
command.StopSignal: stopSignal,
|
2016-05-03 16:56:59 -04:00
|
|
|
command.User: user,
|
|
|
|
command.Volume: volume,
|
|
|
|
command.Workdir: workdir,
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// This method is the entrypoint to all statement handling routines.
|
|
|
|
//
|
|
|
|
// Almost all nodes will have this structure:
|
|
|
|
// Child[Node, Node, Node] where Child is from parser.Node.Children and each
|
|
|
|
// node comes from parser.Node.Next. This forms a "line" with a statement and
|
|
|
|
// arguments and we process them in this normalized form by hitting
|
2015-02-12 00:18:48 -05:00
|
|
|
// evaluateTable with the leaf nodes of the command and the Builder object.
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
|
|
|
// ONBUILD is a special case; in this case the parser will emit:
|
|
|
|
// Child[Node, Child[Node, Node...]] where the first node is the literal
|
2015-09-24 12:45:59 -04:00
|
|
|
// "onbuild" and the child entrypoint is the command of the ONBUILD statement,
|
2014-08-07 01:56:44 -04:00
|
|
|
// such as `RUN` in ONBUILD RUN foo. There is special case logic in here to
|
|
|
|
// deal with that, at least until it becomes more of a general concern with new
|
|
|
|
// features.
|
2016-07-24 04:06:10 -04:00
|
|
|
func (b *Builder) dispatch(stepN int, stepTotal int, ast *parser.Node) error {
|
2014-08-05 16:17:40 -04:00
|
|
|
cmd := ast.Value
|
2015-09-06 13:26:40 -04:00
|
|
|
upperCasedCmd := strings.ToUpper(cmd)
|
2015-08-12 13:00:04 -04:00
|
|
|
|
2015-09-24 12:45:59 -04:00
|
|
|
// To ensure the user is given a decent error message if the platform
|
2015-08-12 13:00:04 -04:00
|
|
|
// on which the daemon is running does not support a builder command.
|
|
|
|
if err := platformSupports(strings.ToLower(cmd)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-13 06:07:41 -04:00
|
|
|
attrs := ast.Attributes
|
2014-10-13 16:14:35 -04:00
|
|
|
original := ast.Original
|
2015-01-27 10:57:34 -05:00
|
|
|
flags := ast.Flags
|
2015-11-07 15:05:55 -05:00
|
|
|
strList := []string{}
|
2016-07-24 04:06:10 -04:00
|
|
|
msg := fmt.Sprintf("Step %d/%d : %s", stepN+1, stepTotal, upperCasedCmd)
|
2015-05-05 17:27:42 -04:00
|
|
|
|
|
|
|
if len(ast.Flags) > 0 {
|
|
|
|
msg += " " + strings.Join(ast.Flags, " ")
|
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
if cmd == "onbuild" {
|
2015-02-04 12:34:25 -05:00
|
|
|
if ast.Next == nil {
|
|
|
|
return fmt.Errorf("ONBUILD requires at least one argument")
|
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
ast = ast.Next.Children[0]
|
2015-11-07 15:05:55 -05:00
|
|
|
strList = append(strList, ast.Value)
|
2014-08-13 06:07:41 -04:00
|
|
|
msg += " " + ast.Value
|
2015-05-05 17:27:42 -04:00
|
|
|
|
|
|
|
if len(ast.Flags) > 0 {
|
|
|
|
msg += " " + strings.Join(ast.Flags, " ")
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2014-11-11 14:37:47 -05:00
|
|
|
// count the number of nodes that we are going to traverse first
|
|
|
|
// so we can pre-create the argument and message array. This speeds up the
|
|
|
|
// allocation of those list a lot when they have a lot of arguments
|
|
|
|
cursor := ast
|
|
|
|
var n int
|
|
|
|
for cursor.Next != nil {
|
|
|
|
cursor = cursor.Next
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
msgList := make([]string, n)
|
|
|
|
|
|
|
|
var i int
|
2014-11-14 13:59:14 -05:00
|
|
|
// Append the build-time args to config-environment.
|
|
|
|
// This allows builder config to override the variables, making the behavior similar to
|
|
|
|
// a shell script i.e. `ENV foo bar` overrides value of `foo` passed in build
|
|
|
|
// context. But `ENV foo $foo` will use the value from build context if one
|
|
|
|
// isn't already been defined by a previous ENV primitive.
|
|
|
|
// Note, we get this behavior because we know that ProcessWord() will
|
|
|
|
// stop on the first occurrence of a variable name and not notice
|
|
|
|
// a subsequent one. So, putting the buildArgs list after the Config.Env
|
|
|
|
// list, in 'envs', is safe.
|
2015-09-06 13:26:40 -04:00
|
|
|
envs := b.runConfig.Env
|
2015-12-29 15:49:17 -05:00
|
|
|
for key, val := range b.options.BuildArgs {
|
2014-11-14 13:59:14 -05:00
|
|
|
if !b.isBuildArgAllowed(key) {
|
|
|
|
// skip build-args that are not in allowed list, meaning they have
|
|
|
|
// not been defined by an "ARG" Dockerfile command yet.
|
|
|
|
// This is an error condition but only if there is no "ARG" in the entire
|
|
|
|
// Dockerfile, so we'll generate any necessary errors after we parsed
|
|
|
|
// the entire file (see 'leftoverArgs' processing in evaluator.go )
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
envs = append(envs, fmt.Sprintf("%s=%s", key, val))
|
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
for ast.Next != nil {
|
|
|
|
ast = ast.Next
|
2014-10-25 14:29:18 -04:00
|
|
|
var str string
|
|
|
|
str = ast.Value
|
2015-11-10 15:57:51 -05:00
|
|
|
if replaceEnvAllowed[cmd] {
|
2015-01-28 21:28:48 -05:00
|
|
|
var err error
|
2015-11-07 15:05:55 -05:00
|
|
|
var words []string
|
|
|
|
|
2015-11-10 15:57:51 -05:00
|
|
|
if allowWordExpansion[cmd] {
|
2016-10-21 14:55:39 -04:00
|
|
|
words, err = ProcessWords(str, envs, b.directive.EscapeToken)
|
2015-11-07 15:05:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
strList = append(strList, words...)
|
|
|
|
} else {
|
2016-10-21 14:55:39 -04:00
|
|
|
str, err = ProcessWord(str, envs, b.directive.EscapeToken)
|
2015-11-07 15:05:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
strList = append(strList, str)
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
2015-11-07 15:05:55 -05:00
|
|
|
} else {
|
|
|
|
strList = append(strList, str)
|
2014-10-25 14:29:18 -04:00
|
|
|
}
|
2014-11-11 14:37:47 -05:00
|
|
|
msgList[i] = ast.Value
|
|
|
|
i++
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-11-11 14:37:47 -05:00
|
|
|
msg += " " + strings.Join(msgList, " ")
|
2015-09-06 13:26:40 -04:00
|
|
|
fmt.Fprintln(b.Stdout, msg)
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
// XXX yes, we skip any cmds that are not valid; the parser should have
|
|
|
|
// picked these out already.
|
2015-02-12 00:18:48 -05:00
|
|
|
if f, ok := evaluateTable[cmd]; ok {
|
2015-09-06 13:26:40 -04:00
|
|
|
b.flags = NewBFlags()
|
|
|
|
b.flags.Args = flags
|
2015-09-29 13:51:40 -04:00
|
|
|
return f(b, strList, attrs, original)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
return fmt.Errorf("Unknown instruction: %s", upperCasedCmd)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2016-09-13 00:06:04 -04:00
|
|
|
|
|
|
|
// checkDispatch does a simple check for syntax errors of the Dockerfile.
|
|
|
|
// Because some of the instructions can only be validated through runtime,
|
|
|
|
// arg, env, etc., this syntax check will not be complete and could not replace
|
|
|
|
// the runtime check. Instead, this function is only a helper that allows
|
|
|
|
// user to find out the obvious error in Dockerfile earlier on.
|
|
|
|
// onbuild bool: indicate if instruction XXX is part of `ONBUILD XXX` trigger
|
|
|
|
func (b *Builder) checkDispatch(ast *parser.Node, onbuild bool) error {
|
|
|
|
cmd := ast.Value
|
|
|
|
upperCasedCmd := strings.ToUpper(cmd)
|
|
|
|
|
|
|
|
// To ensure the user is given a decent error message if the platform
|
|
|
|
// on which the daemon is running does not support a builder command.
|
|
|
|
if err := platformSupports(strings.ToLower(cmd)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The instruction itself is ONBUILD, we will make sure it follows with at
|
|
|
|
// least one argument
|
|
|
|
if upperCasedCmd == "ONBUILD" {
|
|
|
|
if ast.Next == nil {
|
|
|
|
return fmt.Errorf("ONBUILD requires at least one argument")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The instruction is part of ONBUILD trigger (not the instruction itself)
|
|
|
|
if onbuild {
|
|
|
|
switch upperCasedCmd {
|
|
|
|
case "ONBUILD":
|
|
|
|
return fmt.Errorf("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
|
|
|
|
case "MAINTAINER", "FROM":
|
|
|
|
return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", upperCasedCmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := evaluateTable[cmd]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Unknown instruction: %s", upperCasedCmd)
|
|
|
|
}
|