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).
|
2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
import (
|
2017-05-22 11:21:17 -04:00
|
|
|
"reflect"
|
2017-11-20 11:33:20 -05:00
|
|
|
"runtime"
|
2017-05-22 11:21:17 -04:00
|
|
|
"strconv"
|
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"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
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"
|
2018-06-02 12:46:53 -04:00
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/shell"
|
2017-04-04 12:28:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2014-08-05 16:17:40 -04:00
|
|
|
)
|
|
|
|
|
2017-10-09 09:17:24 -04:00
|
|
|
func dispatch(d dispatchRequest, cmd instructions.Command) (err error) {
|
2017-05-22 11:21:17 -04:00
|
|
|
if c, ok := cmd.(instructions.PlatformSpecific); ok {
|
2018-02-05 17:41:45 -05:00
|
|
|
err := c.CheckPlatform(d.state.operatingSystem)
|
2017-05-22 11:21:17 -04:00
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.InvalidParameter(err)
|
2017-05-22 11:21:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
runConfigEnv := d.state.runConfig.Env
|
|
|
|
envs := append(runConfigEnv, d.state.buildArgs.FilterAllowed(runConfigEnv)...)
|
2014-10-25 14:29:18 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
if ex, ok := cmd.(instructions.SupportsSingleWordExpansion); ok {
|
|
|
|
err := ex.Expand(func(word string) (string, error) {
|
|
|
|
return d.shlex.ProcessWord(word, envs)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.InvalidParameter(err)
|
2017-05-22 11:21:17 -04:00
|
|
|
}
|
|
|
|
}
|
2015-11-07 15:05:55 -05:00
|
|
|
|
2017-10-09 09:17:24 -04:00
|
|
|
defer func() {
|
|
|
|
if d.builder.options.ForceRemove {
|
|
|
|
d.builder.containerManager.RemoveAll(d.builder.Stdout)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if d.builder.options.Remove && err == nil {
|
|
|
|
d.builder.containerManager.RemoveAll(d.builder.Stdout)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2017-05-22 11:21:17 -04:00
|
|
|
switch c := cmd.(type) {
|
|
|
|
case *instructions.EnvCommand:
|
|
|
|
return dispatchEnv(d, c)
|
|
|
|
case *instructions.MaintainerCommand:
|
|
|
|
return dispatchMaintainer(d, c)
|
|
|
|
case *instructions.LabelCommand:
|
|
|
|
return dispatchLabel(d, c)
|
|
|
|
case *instructions.AddCommand:
|
|
|
|
return dispatchAdd(d, c)
|
|
|
|
case *instructions.CopyCommand:
|
|
|
|
return dispatchCopy(d, c)
|
|
|
|
case *instructions.OnbuildCommand:
|
|
|
|
return dispatchOnbuild(d, c)
|
|
|
|
case *instructions.WorkdirCommand:
|
|
|
|
return dispatchWorkdir(d, c)
|
|
|
|
case *instructions.RunCommand:
|
|
|
|
return dispatchRun(d, c)
|
|
|
|
case *instructions.CmdCommand:
|
|
|
|
return dispatchCmd(d, c)
|
|
|
|
case *instructions.HealthCheckCommand:
|
|
|
|
return dispatchHealthcheck(d, c)
|
|
|
|
case *instructions.EntrypointCommand:
|
|
|
|
return dispatchEntrypoint(d, c)
|
|
|
|
case *instructions.ExposeCommand:
|
|
|
|
return dispatchExpose(d, c, envs)
|
|
|
|
case *instructions.UserCommand:
|
|
|
|
return dispatchUser(d, c)
|
|
|
|
case *instructions.VolumeCommand:
|
|
|
|
return dispatchVolume(d, c)
|
|
|
|
case *instructions.StopSignalCommand:
|
|
|
|
return dispatchStopSignal(d, c)
|
|
|
|
case *instructions.ArgCommand:
|
|
|
|
return dispatchArg(d, c)
|
|
|
|
case *instructions.ShellCommand:
|
|
|
|
return dispatchShell(d, c)
|
|
|
|
}
|
|
|
|
return errors.Errorf("unsupported command type: %v", reflect.TypeOf(cmd))
|
2017-04-11 14:34:05 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
// dispatchState is a data object which is modified by dispatchers
|
|
|
|
type dispatchState struct {
|
2017-09-19 15:14:46 -04:00
|
|
|
runConfig *container.Config
|
|
|
|
maintainer string
|
|
|
|
cmdSet bool
|
|
|
|
imageID string
|
|
|
|
baseImage builder.Image
|
|
|
|
stageName string
|
2018-05-07 13:49:13 -04:00
|
|
|
buildArgs *BuildArgs
|
2017-09-19 15:14:46 -04:00
|
|
|
operatingSystem string
|
2017-04-11 14:34:05 -04:00
|
|
|
}
|
|
|
|
|
2018-05-07 13:49:13 -04:00
|
|
|
func newDispatchState(baseArgs *BuildArgs) *dispatchState {
|
2017-05-22 11:21:17 -04:00
|
|
|
args := baseArgs.Clone()
|
|
|
|
args.ResetAllowed()
|
|
|
|
return &dispatchState{runConfig: &container.Config{}, buildArgs: args}
|
|
|
|
}
|
2017-04-11 14:34:05 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
type stagesBuildResults struct {
|
|
|
|
flat []*container.Config
|
|
|
|
indexed map[string]*container.Config
|
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func newStagesBuildResults() *stagesBuildResults {
|
|
|
|
return &stagesBuildResults{
|
|
|
|
indexed: make(map[string]*container.Config),
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func (r *stagesBuildResults) getByName(name string) (*container.Config, bool) {
|
|
|
|
c, ok := r.indexed[strings.ToLower(name)]
|
|
|
|
return c, ok
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func (r *stagesBuildResults) validateIndex(i int) error {
|
|
|
|
if i == len(r.flat) {
|
|
|
|
return errors.New("refers to current build stage")
|
2015-08-12 13:00:04 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
if i < 0 || i > len(r.flat) {
|
|
|
|
return errors.New("index out of bounds")
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-05-05 17:27:42 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func (r *stagesBuildResults) get(nameOrIndex string) (*container.Config, error) {
|
|
|
|
if c, ok := r.getByName(nameOrIndex); ok {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
ix, err := strconv.ParseInt(nameOrIndex, 10, 0)
|
2017-04-26 17:45:16 -04:00
|
|
|
if err != nil {
|
2017-05-22 11:21:17 -04:00
|
|
|
return nil, nil
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
if err := r.validateIndex(int(ix)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r.flat[ix], nil
|
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func (r *stagesBuildResults) checkStageNameAvailable(name string) error {
|
|
|
|
if name != "" {
|
|
|
|
if _, ok := r.getByName(name); ok {
|
|
|
|
return errors.Errorf("%s stage name already used", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-24 16:22:32 -05:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func (r *stagesBuildResults) commitStage(name string, config *container.Config) error {
|
|
|
|
if name != "" {
|
|
|
|
if _, ok := r.getByName(name); ok {
|
|
|
|
return errors.Errorf("%s stage name already used", name)
|
|
|
|
}
|
|
|
|
r.indexed[strings.ToLower(name)] = config
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
r.flat = append(r.flat, config)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func commitStage(state *dispatchState, stages *stagesBuildResults) error {
|
|
|
|
return stages.commitStage(state.stageName, state.runConfig)
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
type dispatchRequest struct {
|
2017-04-26 17:45:16 -04:00
|
|
|
state *dispatchState
|
2018-01-30 18:58:21 -05:00
|
|
|
shlex *shell.Lex
|
2017-05-22 11:21:17 -04:00
|
|
|
builder *Builder
|
2017-05-07 14:37:46 -04:00
|
|
|
source builder.Source
|
2017-05-22 11:21:17 -04:00
|
|
|
stages *stagesBuildResults
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2018-05-07 13:49:13 -04:00
|
|
|
func newDispatchRequest(builder *Builder, escapeToken rune, source builder.Source, buildArgs *BuildArgs, stages *stagesBuildResults) dispatchRequest {
|
2017-05-22 11:21:17 -04:00
|
|
|
return dispatchRequest{
|
|
|
|
state: newDispatchState(buildArgs),
|
2018-01-30 18:58:21 -05:00
|
|
|
shlex: shell.NewLex(escapeToken),
|
2017-05-22 11:21:17 -04:00
|
|
|
builder: builder,
|
|
|
|
source: source,
|
|
|
|
stages: stages,
|
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
|
|
|
|
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-11-20 11:33:20 -05:00
|
|
|
func (s *dispatchState) beginStage(stageName string, image builder.Image) error {
|
2017-05-05 13:05:25 -04:00
|
|
|
s.stageName = stageName
|
|
|
|
s.imageID = image.ImageID()
|
2017-09-19 15:14:46 -04:00
|
|
|
s.operatingSystem = image.OperatingSystem()
|
2017-11-20 11:33:20 -05:00
|
|
|
if s.operatingSystem == "" { // In case it isn't set
|
|
|
|
s.operatingSystem = runtime.GOOS
|
|
|
|
}
|
|
|
|
if !system.IsOSSupported(s.operatingSystem) {
|
|
|
|
return system.ErrNotSupportedOperatingSystem
|
|
|
|
}
|
2017-05-05 13:05:25 -04:00
|
|
|
|
|
|
|
if image.RunConfig() != nil {
|
2017-11-09 16:20:51 -05:00
|
|
|
// copy avoids referencing the same instance when 2 stages have the same base
|
|
|
|
s.runConfig = copyRunConfig(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()
|
2017-05-22 11:21:17 -04:00
|
|
|
s.runConfig.OpenStdin = false
|
|
|
|
s.runConfig.StdinOnce = false
|
2017-11-20 11:33:20 -05:00
|
|
|
return nil
|
2017-05-05 13:05:25 -04:00
|
|
|
}
|
|
|
|
|
2017-08-08 15:43:48 -04:00
|
|
|
// Add the default PATH to runConfig.ENV if one exists for the operating system 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-09-19 15:14:46 -04:00
|
|
|
defaultPath := system.DefaultPathEnv(s.operatingSystem)
|
2017-08-08 15:43:48 -04:00
|
|
|
if defaultPath == "" {
|
2017-05-05 13:05:25 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
envMap := opts.ConvertKVStringsToMap(s.runConfig.Env)
|
|
|
|
if _, ok := envMap["PATH"]; !ok {
|
2017-08-08 15:43:48 -04:00
|
|
|
s.runConfig.Env = append(s.runConfig.Env, "PATH="+defaultPath)
|
2017-05-05 13:05:25 -04:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|