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.
|
2017-04-13 14:37:32 -04:00
|
|
|
// Calling newBuilder with the BuildOpts struct can be used to customize the
|
2014-08-07 01:56:44 -04:00
|
|
|
// 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 (
|
2017-04-11 18:17:02 -04:00
|
|
|
"bytes"
|
2014-08-05 16:17:40 -04:00
|
|
|
"fmt"
|
2017-05-26 19:14:18 -04:00
|
|
|
"runtime"
|
2014-08-05 16:17:40 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-04-26 17:45:16 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2015-09-05 15:49:06 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile/command"
|
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2017-05-05 13:05:25 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-04-04 12:28:59 -04:00
|
|
|
"github.com/docker/docker/runconfig/opts"
|
|
|
|
"github.com/pkg/errors"
|
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,
|
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
type dispatchRequest struct {
|
|
|
|
builder *Builder // TODO: replace this with a smaller interface
|
|
|
|
args []string
|
|
|
|
attributes map[string]bool
|
|
|
|
flags *BFlags
|
|
|
|
original string
|
2017-04-26 18:24:41 -04:00
|
|
|
shlex *ShellLex
|
2017-04-26 17:45:16 -04:00
|
|
|
state *dispatchState
|
2017-05-07 14:37:46 -04:00
|
|
|
source builder.Source
|
2017-04-11 14:34:05 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
func newDispatchRequestFromOptions(options dispatchOptions, builder *Builder, args []string) dispatchRequest {
|
2017-04-11 14:34:05 -04:00
|
|
|
return dispatchRequest{
|
|
|
|
builder: builder,
|
|
|
|
args: args,
|
2017-04-26 17:45:16 -04:00
|
|
|
attributes: options.node.Attributes,
|
|
|
|
original: options.node.Original,
|
|
|
|
flags: NewBFlagsWithArgs(options.node.Flags),
|
|
|
|
shlex: options.shlex,
|
|
|
|
state: options.state,
|
2017-05-07 14:37:46 -04:00
|
|
|
source: options.source,
|
2017-04-11 14:34:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type dispatcher func(dispatchRequest) error
|
|
|
|
|
|
|
|
var evaluateTable map[string]dispatcher
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
func init() {
|
2017-04-11 14:34:05 -04:00
|
|
|
evaluateTable = map[string]dispatcher{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
func formatStep(stepN int, stepTotal int) string {
|
|
|
|
return fmt.Sprintf("%d/%d", stepN+1, stepTotal)
|
|
|
|
}
|
|
|
|
|
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.
|
2017-04-26 17:45:16 -04:00
|
|
|
func (b *Builder) dispatch(options dispatchOptions) (*dispatchState, error) {
|
|
|
|
node := options.node
|
2017-04-11 14:34:05 -04:00
|
|
|
cmd := node.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 {
|
2017-04-24 07:32:01 -04:00
|
|
|
buildsFailed.WithValues(metricsCommandNotSupportedError).Inc()
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, validationError{err}
|
2015-08-12 13:00:04 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
msg := bytes.NewBufferString(fmt.Sprintf("Step %s : %s%s",
|
|
|
|
options.stepMsg, upperCasedCmd, formatFlags(node.Flags)))
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
args := []string{}
|
2017-04-11 14:34:05 -04:00
|
|
|
ast := node
|
2017-04-26 17:45:16 -04:00
|
|
|
if cmd == command.Onbuild {
|
|
|
|
var err error
|
|
|
|
ast, args, err = handleOnBuildNode(node, msg)
|
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, validationError{err}
|
2015-02-04 12:34:25 -05:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
2015-05-05 17:27:42 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfigEnv := options.state.runConfig.Env
|
|
|
|
envs := append(runConfigEnv, b.buildArgs.FilterAllowed(runConfigEnv)...)
|
|
|
|
processFunc := createProcessWordFunc(options.shlex, cmd, envs)
|
|
|
|
words, err := getDispatchArgsFromNode(ast, processFunc, msg)
|
|
|
|
if err != nil {
|
2017-04-24 07:32:01 -04:00
|
|
|
buildsFailed.WithValues(metricsErrorProcessingCommandsError).Inc()
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, validationError{err}
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
args = append(args, words...)
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
fmt.Fprintln(b.Stdout, msg.String())
|
2017-02-24 16:22:32 -05:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
f, ok := evaluateTable[cmd]
|
|
|
|
if !ok {
|
2017-04-24 07:32:01 -04:00
|
|
|
buildsFailed.WithValues(metricsUnknownInstructionError).Inc()
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, validationError{errors.Errorf("unknown instruction: %s", upperCasedCmd)}
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
|
|
|
options.state.updateRunConfig()
|
2017-06-05 11:12:26 -04:00
|
|
|
err = f(newDispatchRequestFromOptions(options, b, args))
|
|
|
|
return options.state, err
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
type dispatchOptions struct {
|
|
|
|
state *dispatchState
|
|
|
|
stepMsg string
|
|
|
|
node *parser.Node
|
|
|
|
shlex *ShellLex
|
2017-05-07 14:37:46 -04:00
|
|
|
source builder.Source
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
// dispatchState is a data object which is modified by dispatchers
|
|
|
|
type dispatchState struct {
|
2017-05-05 13:05:25 -04:00
|
|
|
runConfig *container.Config
|
|
|
|
maintainer string
|
|
|
|
cmdSet bool
|
|
|
|
imageID string
|
|
|
|
baseImage builder.Image
|
|
|
|
stageName string
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newDispatchState() *dispatchState {
|
|
|
|
return &dispatchState{runConfig: &container.Config{}}
|
|
|
|
}
|
|
|
|
|
2017-05-05 13:05:25 -04:00
|
|
|
func (s *dispatchState) updateRunConfig() {
|
|
|
|
s.runConfig.Image = s.imageID
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// hasFromImage returns true if the builder has processed a `FROM <image>` line
|
2017-05-05 13:05:25 -04:00
|
|
|
func (s *dispatchState) hasFromImage() bool {
|
|
|
|
return s.imageID != "" || (s.baseImage != nil && s.baseImage.ImageID() == "")
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 13:05:25 -04:00
|
|
|
func (s *dispatchState) isCurrentStage(target string) bool {
|
2017-04-26 17:45:16 -04:00
|
|
|
if target == "" {
|
|
|
|
return false
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2017-05-05 13:05:25 -04:00
|
|
|
return strings.EqualFold(s.stageName, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dispatchState) beginStage(stageName string, image builder.Image) {
|
|
|
|
s.stageName = stageName
|
|
|
|
s.imageID = image.ImageID()
|
|
|
|
|
|
|
|
if image.RunConfig() != nil {
|
|
|
|
s.runConfig = image.RunConfig()
|
2017-05-12 15:00:55 -04:00
|
|
|
} else {
|
|
|
|
s.runConfig = &container.Config{}
|
2017-05-05 13:05:25 -04:00
|
|
|
}
|
|
|
|
s.baseImage = image
|
|
|
|
s.setDefaultPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the default PATH to runConfig.ENV if one exists for the platform and there
|
2017-05-26 19:14:18 -04:00
|
|
|
// is no PATH set. Note that Windows containers on Windows won't have one as it's set by HCS
|
2017-05-05 13:05:25 -04:00
|
|
|
func (s *dispatchState) setDefaultPath() {
|
2017-05-26 19:14:18 -04:00
|
|
|
// TODO @jhowardmsft LCOW Support - This will need revisiting later
|
|
|
|
platform := runtime.GOOS
|
2017-06-26 12:11:54 -04:00
|
|
|
if system.LCOWSupported() {
|
2017-05-26 19:14:18 -04:00
|
|
|
platform = "linux"
|
|
|
|
}
|
|
|
|
if system.DefaultPathEnv(platform) == "" {
|
2017-05-05 13:05:25 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
envMap := opts.ConvertKVStringsToMap(s.runConfig.Env)
|
|
|
|
if _, ok := envMap["PATH"]; !ok {
|
2017-05-26 19:14:18 -04:00
|
|
|
s.runConfig.Env = append(s.runConfig.Env, "PATH="+system.DefaultPathEnv(platform))
|
2017-05-05 13:05:25 -04:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
func handleOnBuildNode(ast *parser.Node, msg *bytes.Buffer) (*parser.Node, []string, error) {
|
|
|
|
if ast.Next == nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, nil, validationError{errors.New("ONBUILD requires at least one argument")}
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
|
|
|
ast = ast.Next.Children[0]
|
|
|
|
msg.WriteString(" " + ast.Value + formatFlags(ast.Flags))
|
|
|
|
return ast, []string{ast.Value}, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2016-09-13 00:06:04 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
func formatFlags(flags []string) string {
|
|
|
|
if len(flags) > 0 {
|
|
|
|
return " " + strings.Join(flags, " ")
|
2017-03-13 17:25:37 -04:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
return ""
|
2017-03-13 17:25:37 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
func getDispatchArgsFromNode(ast *parser.Node, processFunc processWordFunc, msg *bytes.Buffer) ([]string, error) {
|
|
|
|
args := []string{}
|
|
|
|
for i := 0; ast.Next != nil; i++ {
|
|
|
|
ast = ast.Next
|
|
|
|
words, err := processFunc(ast.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
args = append(args, words...)
|
|
|
|
msg.WriteString(" " + ast.Value)
|
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type processWordFunc func(string) ([]string, error)
|
2017-04-26 18:24:41 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
func createProcessWordFunc(shlex *ShellLex, cmd string, envs []string) processWordFunc {
|
2017-04-26 18:24:41 -04:00
|
|
|
switch {
|
|
|
|
case !replaceEnvAllowed[cmd]:
|
2017-04-26 17:45:16 -04:00
|
|
|
return func(word string) ([]string, error) {
|
2017-04-26 18:24:41 -04:00
|
|
|
return []string{word}, nil
|
|
|
|
}
|
|
|
|
case allowWordExpansion[cmd]:
|
2017-04-26 17:45:16 -04:00
|
|
|
return func(word string) ([]string, error) {
|
|
|
|
return shlex.ProcessWords(word, envs)
|
|
|
|
}
|
2017-04-26 18:24:41 -04:00
|
|
|
default:
|
2017-04-26 17:45:16 -04:00
|
|
|
return func(word string) ([]string, error) {
|
2017-04-26 18:24:41 -04:00
|
|
|
word, err := shlex.ProcessWord(word, envs)
|
2017-04-04 13:40:37 -04:00
|
|
|
return []string{word}, err
|
|
|
|
}
|
2017-03-13 17:25:37 -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.
|
2017-04-04 16:34:19 -04:00
|
|
|
func checkDispatch(ast *parser.Node) error {
|
2016-09-13 00:06:04 -04:00
|
|
|
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 {
|
2017-04-24 07:32:01 -04:00
|
|
|
buildsFailed.WithValues(metricsMissingOnbuildArgumentsError).Inc()
|
2016-12-25 01:37:31 -05:00
|
|
|
return errors.New("ONBUILD requires at least one argument")
|
2016-09-13 00:06:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := evaluateTable[cmd]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-24 07:32:01 -04:00
|
|
|
buildsFailed.WithValues(metricsUnknownInstructionError).Inc()
|
2017-04-04 12:28:59 -04:00
|
|
|
return errors.Errorf("unknown instruction: %s", upperCasedCmd)
|
2016-09-13 00:06:04 -04:00
|
|
|
}
|