2015-03-23 16:10:38 -04:00
|
|
|
// Package builder 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
|
|
|
|
// package, and this division of resposibility should be respected.
|
|
|
|
//
|
|
|
|
// 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).
|
2014-08-15 12:29:35 -04:00
|
|
|
package builder
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-08-05 18:41:09 -04:00
|
|
|
"os"
|
2015-01-21 14:08:19 -05:00
|
|
|
"path/filepath"
|
2015-08-12 13:00:04 -04:00
|
|
|
"runtime"
|
2014-08-05 16:17:40 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-02-17 13:25:36 -05:00
|
|
|
"github.com/docker/docker/api"
|
2015-02-12 00:18:48 -05:00
|
|
|
"github.com/docker/docker/builder/command"
|
2014-08-05 18:41:09 -04:00
|
|
|
"github.com/docker/docker/builder/parser"
|
2015-04-22 08:06:58 -04:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2015-09-10 18:01:18 -04:00
|
|
|
"github.com/docker/docker/context"
|
2014-08-05 16:17:40 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2014-10-23 17:30:11 -04:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
2015-03-17 22:18:41 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2015-03-24 07:25:26 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-02-02 16:17:12 -05:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2014-08-05 18:41:09 -04:00
|
|
|
"github.com/docker/docker/pkg/tarsum"
|
2015-07-22 22:26:06 -04:00
|
|
|
"github.com/docker/docker/pkg/ulimit"
|
2014-08-05 16:17:40 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2014-10-25 14:29:18 -04:00
|
|
|
// Environment variable interpolation will happen on these statements only.
|
|
|
|
var replaceEnvAllowed = map[string]struct{}{
|
2015-08-18 13:30:44 -04:00
|
|
|
command.Env: {},
|
|
|
|
command.Label: {},
|
|
|
|
command.Add: {},
|
|
|
|
command.Copy: {},
|
|
|
|
command.Workdir: {},
|
|
|
|
command.Expose: {},
|
|
|
|
command.Volume: {},
|
|
|
|
command.User: {},
|
|
|
|
command.StopSignal: {},
|
2014-11-14 13:59:14 -05:00
|
|
|
command.Arg: {},
|
2014-10-25 14:29:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
var evaluateTable map[string]func(context.Context, *builder, []string, map[string]bool, string) error
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
func init() {
|
2015-09-10 18:01:18 -04:00
|
|
|
evaluateTable = map[string]func(context.Context, *builder, []string, map[string]bool, string) error{
|
2015-02-12 00:18:48 -05:00
|
|
|
command.Env: env,
|
2015-02-17 10:20:06 -05:00
|
|
|
command.Label: label,
|
2015-02-12 00:18:48 -05:00
|
|
|
command.Maintainer: maintainer,
|
|
|
|
command.Add: add,
|
|
|
|
command.Copy: dispatchCopy, // copy() is a go builtin
|
|
|
|
command.From: from,
|
|
|
|
command.Onbuild: onbuild,
|
|
|
|
command.Workdir: workdir,
|
|
|
|
command.Run: run,
|
|
|
|
command.Cmd: cmd,
|
|
|
|
command.Entrypoint: entrypoint,
|
|
|
|
command.Expose: expose,
|
|
|
|
command.Volume: volume,
|
|
|
|
command.User: user,
|
2015-08-18 13:30:44 -04:00
|
|
|
command.StopSignal: stopSignal,
|
2014-11-14 13:59:14 -05:00
|
|
|
command.Arg: arg,
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
// builder is an internal struct, used to maintain configuration of the Dockerfile's
|
2014-08-07 01:56:44 -04:00
|
|
|
// processing as it evaluates the parsing result.
|
2015-07-22 01:29:03 -04:00
|
|
|
type builder struct {
|
2014-08-07 01:56:44 -04:00
|
|
|
Daemon *daemon.Daemon
|
|
|
|
|
|
|
|
// effectively stdio for the run. Because it is not stdio, I said
|
|
|
|
// "Effectively". Do not use stdio anywhere in this package for any reason.
|
|
|
|
OutStream io.Writer
|
|
|
|
ErrStream io.Writer
|
|
|
|
|
|
|
|
Verbose bool
|
|
|
|
UtilizeCache bool
|
2015-02-25 13:27:32 -05:00
|
|
|
cacheBusted bool
|
2014-08-07 01:56:44 -04:00
|
|
|
|
|
|
|
// controls how images and containers are handled between steps.
|
|
|
|
Remove bool
|
|
|
|
ForceRemove bool
|
2014-11-21 12:51:32 -05:00
|
|
|
Pull bool
|
2014-08-07 01:56:44 -04:00
|
|
|
|
2015-01-09 16:07:00 -05:00
|
|
|
// set this to true if we want the builder to not commit between steps.
|
|
|
|
// This is useful when we only want to use the evaluator table to generate
|
|
|
|
// the final configs of the Dockerfile but dont want the layers
|
|
|
|
disableCommit bool
|
|
|
|
|
2015-06-23 15:58:17 -04:00
|
|
|
// Registry server auth configs used to pull images when handling `FROM`.
|
|
|
|
AuthConfigs map[string]cliconfig.AuthConfig
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
// Deprecated, original writer used for ImagePull. To be removed.
|
2014-08-05 16:17:40 -04:00
|
|
|
OutOld io.Writer
|
2015-03-17 22:18:41 -04:00
|
|
|
StreamFormatter *streamformatter.StreamFormatter
|
2014-08-26 15:25:44 -04:00
|
|
|
|
|
|
|
Config *runconfig.Config // runconfig for cmd, run, entrypoint etc.
|
|
|
|
|
2014-11-14 13:59:14 -05:00
|
|
|
buildArgs map[string]string // build-time args received in build context for expansion/substitution and commands in 'run'.
|
|
|
|
allowedBuildArgs map[string]bool // list of build-time args that are allowed for expansion/substitution and passing to commands in 'run'.
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
// both of these are controlled by the Remove and ForceRemove options in BuildOpts
|
|
|
|
TmpContainers map[string]struct{} // a map of containers used for removes
|
|
|
|
|
2014-09-11 10:42:17 -04:00
|
|
|
dockerfileName string // name of Dockerfile
|
|
|
|
dockerfile *parser.Node // the syntax tree of the dockerfile
|
|
|
|
image string // image name for commit processing
|
|
|
|
maintainer string // maintainer name. could probably be removed.
|
|
|
|
cmdSet bool // indicates is CMD was set in current Dockerfile
|
2015-07-22 01:29:03 -04:00
|
|
|
BuilderFlags *BFlags // current cmd's BuilderFlags - temporary
|
2014-09-11 10:42:17 -04:00
|
|
|
context tarsum.TarSum // the context is a tarball that is uploaded by the client
|
|
|
|
contextPath string // the path of the temporary directory the local context is unpacked to (server side)
|
|
|
|
noBaseImage bool // indicates that this build does not start from any base image, but is being built from an empty file system.
|
2015-02-06 09:33:01 -05:00
|
|
|
|
|
|
|
// Set resource restrictions for build containers
|
2015-03-26 19:14:31 -04:00
|
|
|
cpuSetCpus string
|
|
|
|
cpuSetMems string
|
|
|
|
cpuShares int64
|
2015-04-08 04:58:59 -04:00
|
|
|
cpuPeriod int64
|
2015-03-26 19:14:31 -04:00
|
|
|
cpuQuota int64
|
|
|
|
cgroupParent string
|
|
|
|
memory int64
|
|
|
|
memorySwap int64
|
2015-07-22 22:26:06 -04:00
|
|
|
ulimits []*ulimit.Ulimit
|
2015-03-10 18:10:00 -04:00
|
|
|
|
|
|
|
cancelled <-chan struct{} // When closed, job was cancelled.
|
2015-04-07 22:29:29 -04:00
|
|
|
|
|
|
|
activeImages []string
|
|
|
|
id string // Used to hold reference images
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// Run the builder with the context. This is the lynchpin of this package. This
|
|
|
|
// will (barring errors):
|
|
|
|
//
|
|
|
|
// * call readContext() which will set up the temporary directory and unpack
|
|
|
|
// the context into it.
|
|
|
|
// * read the dockerfile
|
|
|
|
// * parse the dockerfile
|
|
|
|
// * walk the parse tree and execute it by dispatching to handlers. If Remove
|
|
|
|
// or ForceRemove is set, additional cleanup around containers happens after
|
|
|
|
// processing.
|
|
|
|
// * Print a happy message and return the image ID.
|
|
|
|
//
|
2015-09-10 18:01:18 -04:00
|
|
|
func (b *builder) Run(ctx context.Context, context io.Reader) (string, error) {
|
2014-08-07 01:56:44 -04:00
|
|
|
if err := b.readContext(context); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-09-15 09:01:23 -04:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(b.contextPath); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("[BUILDER] failed to remove temporary context: %s", err)
|
2014-09-15 09:01:23 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-02-17 13:25:36 -05:00
|
|
|
if err := b.readDockerfile(); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2014-08-26 15:25:44 -04:00
|
|
|
|
|
|
|
// some initializations that would not have been supplied by the caller.
|
2014-10-23 20:23:25 -04:00
|
|
|
b.Config = &runconfig.Config{}
|
2015-02-06 09:33:01 -05:00
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
b.TmpContainers = map[string]struct{}{}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
for i, n := range b.dockerfile.Children {
|
2015-03-10 18:10:00 -04:00
|
|
|
select {
|
|
|
|
case <-b.cancelled:
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debug("Builder: build cancelled!")
|
2015-03-10 18:10:00 -04:00
|
|
|
fmt.Fprintf(b.OutStream, "Build cancelled")
|
|
|
|
return "", fmt.Errorf("Build cancelled")
|
|
|
|
default:
|
|
|
|
// Not cancelled yet, keep going...
|
|
|
|
}
|
2015-09-10 18:01:18 -04:00
|
|
|
if err := b.dispatch(ctx, i, n); err != nil {
|
2014-08-26 15:25:44 -04:00
|
|
|
if b.ForceRemove {
|
2015-09-10 18:01:18 -04:00
|
|
|
b.clearTmp(ctx)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
return "", err
|
2014-08-13 06:07:41 -04:00
|
|
|
}
|
2015-03-24 07:25:26 -04:00
|
|
|
fmt.Fprintf(b.OutStream, " ---> %s\n", stringid.TruncateID(b.image))
|
2014-08-26 15:25:44 -04:00
|
|
|
if b.Remove {
|
2015-09-10 18:01:18 -04:00
|
|
|
b.clearTmp(ctx)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 13:59:14 -05:00
|
|
|
// check if there are any leftover build-args that were passed but not
|
|
|
|
// consumed during build. Return an error, if there are any.
|
|
|
|
leftoverArgs := []string{}
|
|
|
|
for arg := range b.buildArgs {
|
|
|
|
if !b.isBuildArgAllowed(arg) {
|
|
|
|
leftoverArgs = append(leftoverArgs, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(leftoverArgs) > 0 {
|
|
|
|
return "", fmt.Errorf("One or more build-args %v were not consumed, failing build.", leftoverArgs)
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
if b.image == "" {
|
2015-02-10 14:10:10 -05:00
|
|
|
return "", fmt.Errorf("No image was generated. Is your Dockerfile empty?")
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-03-24 07:25:26 -04:00
|
|
|
fmt.Fprintf(b.OutStream, "Successfully built %s\n", stringid.TruncateID(b.image))
|
2014-08-05 18:41:09 -04:00
|
|
|
return b.image, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-10-23 17:30:11 -04:00
|
|
|
// Reads a Dockerfile from the current context. It assumes that the
|
|
|
|
// 'filename' is a relative path from the root of the context
|
2015-07-22 01:29:03 -04:00
|
|
|
func (b *builder) readDockerfile() error {
|
2015-02-17 13:25:36 -05:00
|
|
|
// If no -f was specified then look for 'Dockerfile'. If we can't find
|
|
|
|
// that then look for 'dockerfile'. If neither are found then default
|
|
|
|
// back to 'Dockerfile' and use that in the error message.
|
|
|
|
if b.dockerfileName == "" {
|
|
|
|
b.dockerfileName = api.DefaultDockerfileName
|
|
|
|
tmpFN := filepath.Join(b.contextPath, api.DefaultDockerfileName)
|
|
|
|
if _, err := os.Lstat(tmpFN); err != nil {
|
|
|
|
tmpFN = filepath.Join(b.contextPath, strings.ToLower(api.DefaultDockerfileName))
|
|
|
|
if _, err := os.Lstat(tmpFN); err == nil {
|
|
|
|
b.dockerfileName = strings.ToLower(api.DefaultDockerfileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
origFile := b.dockerfileName
|
|
|
|
|
2015-02-02 16:17:12 -05:00
|
|
|
filename, err := symlink.FollowSymlinkInScope(filepath.Join(b.contextPath, origFile), b.contextPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("The Dockerfile (%s) must be within the build context", origFile)
|
2015-01-21 14:08:19 -05:00
|
|
|
}
|
2014-10-23 17:30:11 -04:00
|
|
|
|
2015-02-02 16:17:12 -05:00
|
|
|
fi, err := os.Lstat(filename)
|
2014-10-23 17:30:11 -04:00
|
|
|
if os.IsNotExist(err) {
|
2015-01-21 14:08:19 -05:00
|
|
|
return fmt.Errorf("Cannot locate specified Dockerfile: %s", origFile)
|
2014-10-23 17:30:11 -04:00
|
|
|
}
|
|
|
|
if fi.Size() == 0 {
|
2015-03-23 15:20:43 -04:00
|
|
|
return fmt.Errorf("The Dockerfile (%s) cannot be empty", origFile)
|
2014-10-23 17:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.dockerfile, err = parser.Parse(f)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// After the Dockerfile has been parsed, we need to check the .dockerignore
|
|
|
|
// file for either "Dockerfile" or ".dockerignore", and if either are
|
|
|
|
// present then erase them from the build context. These files should never
|
|
|
|
// have been sent from the client but we did send them to make sure that
|
|
|
|
// we had the Dockerfile to actually parse, and then we also need the
|
|
|
|
// .dockerignore file to know whether either file should be removed.
|
|
|
|
// Note that this assumes the Dockerfile has been read into memory and
|
|
|
|
// is now safe to be removed.
|
|
|
|
|
2015-01-21 14:08:19 -05:00
|
|
|
excludes, _ := utils.ReadDockerIgnore(filepath.Join(b.contextPath, ".dockerignore"))
|
2014-10-23 17:30:11 -04:00
|
|
|
if rm, _ := fileutils.Matches(".dockerignore", excludes); rm == true {
|
2015-01-21 14:08:19 -05:00
|
|
|
os.Remove(filepath.Join(b.contextPath, ".dockerignore"))
|
2014-10-23 17:30:11 -04:00
|
|
|
b.context.(tarsum.BuilderContext).Remove(".dockerignore")
|
|
|
|
}
|
2014-09-11 10:42:17 -04:00
|
|
|
if rm, _ := fileutils.Matches(b.dockerfileName, excludes); rm == true {
|
2015-01-21 14:08:19 -05:00
|
|
|
os.Remove(filepath.Join(b.contextPath, b.dockerfileName))
|
2014-09-11 10:42:17 -04:00
|
|
|
b.context.(tarsum.BuilderContext).Remove(b.dockerfileName)
|
2014-10-23 17:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-14 13:59:14 -05:00
|
|
|
// determine if build arg is part of built-in args or user
|
|
|
|
// defined args in Dockerfile at any point in time.
|
|
|
|
func (b *builder) isBuildArgAllowed(arg string) bool {
|
|
|
|
if _, ok := BuiltinAllowedBuildArgs[arg]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if _, ok := b.allowedBuildArgs[arg]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (b *builder) dispatch(ctx context.Context, stepN int, ast *parser.Node) error {
|
2014-08-05 16:17:40 -04:00
|
|
|
cmd := ast.Value
|
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
|
2014-08-05 16:17:40 -04:00
|
|
|
strs := []string{}
|
2015-08-25 13:05:46 -04:00
|
|
|
msg := fmt.Sprintf("Step %d : %s", stepN+1, strings.ToUpper(cmd))
|
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]
|
2014-10-25 14:29:18 -04:00
|
|
|
strs = append(strs, 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++
|
|
|
|
}
|
|
|
|
l := len(strs)
|
|
|
|
strList := make([]string, n+l)
|
|
|
|
copy(strList, strs)
|
|
|
|
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.
|
|
|
|
envs := b.Config.Env
|
|
|
|
for key, val := range b.buildArgs {
|
|
|
|
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
|
|
|
|
if _, ok := replaceEnvAllowed[cmd]; ok {
|
2015-01-28 21:28:48 -05:00
|
|
|
var err error
|
2014-11-14 13:59:14 -05:00
|
|
|
str, err = ProcessWord(ast.Value, envs)
|
2015-01-28 21:28:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-25 14:29:18 -04:00
|
|
|
}
|
2014-11-11 14:37:47 -05:00
|
|
|
strList[i+l] = str
|
|
|
|
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, " ")
|
2014-08-26 15:25:44 -04:00
|
|
|
fmt.Fprintln(b.OutStream, 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-07-22 01:29:03 -04:00
|
|
|
b.BuilderFlags = NewBFlags()
|
2015-01-27 10:57:34 -05:00
|
|
|
b.BuilderFlags.Args = flags
|
2015-09-10 18:01:18 -04:00
|
|
|
return f(ctx, b, strList, attrs, original)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-02-05 10:23:25 -05:00
|
|
|
return fmt.Errorf("Unknown instruction: %s", strings.ToUpper(cmd))
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2015-08-12 13:00:04 -04:00
|
|
|
|
|
|
|
// platformSupports is a short-term function to give users a quality error
|
|
|
|
// message if a Dockerfile uses a command not supported on the platform.
|
|
|
|
func platformSupports(command string) error {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch command {
|
2015-09-21 12:01:07 -04:00
|
|
|
case "expose", "volume", "user", "stopsignal", "arg":
|
2015-08-12 13:00:04 -04:00
|
|
|
return fmt.Errorf("The daemon on this platform does not support the command '%s'", command)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|