2015-09-05 15:49:06 -04:00
|
|
|
package dockerfile
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// This file contains the dispatchers for each command. Note that
|
|
|
|
// `nullDispatch` is not actually a command, but support for commands we parse
|
|
|
|
// but do nothing with.
|
|
|
|
//
|
|
|
|
// See evaluator.go for a higher level discussion of the whole evaluator
|
|
|
|
// package.
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
import (
|
2017-05-12 15:00:55 -04:00
|
|
|
"bytes"
|
2014-08-05 16:17:40 -04:00
|
|
|
"fmt"
|
2015-05-05 11:39:37 -04:00
|
|
|
"runtime"
|
2014-11-12 03:22:08 -05:00
|
|
|
"sort"
|
2014-08-05 16:17:40 -04:00
|
|
|
"strings"
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2015-12-31 08:57:58 -05:00
|
|
|
"github.com/docker/docker/api"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
2017-05-05 18:52:11 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2017-05-22 11:21:17 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile/instructions"
|
2017-05-05 13:05:25 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2017-05-14 14:18:48 -04:00
|
|
|
"github.com/docker/docker/image"
|
2017-04-13 18:44:36 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-08-18 13:30:44 -04:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2017-05-17 20:08:01 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-12-18 12:58:48 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2017-03-15 18:28:06 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-08-05 16:17:40 -04:00
|
|
|
)
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// ENV foo bar
|
|
|
|
//
|
|
|
|
// Sets the environment variable foo to bar, also makes interpolation
|
|
|
|
// in the dockerfile available from the next statement on via ${foo}.
|
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchEnv(d dispatchRequest, c *instructions.EnvCommand) error {
|
|
|
|
runConfig := d.state.runConfig
|
2017-04-04 12:28:59 -04:00
|
|
|
commitMessage := bytes.NewBufferString("ENV")
|
2017-05-22 11:21:17 -04:00
|
|
|
for _, e := range c.Env {
|
|
|
|
name := e.Key
|
|
|
|
newVar := e.String()
|
2016-08-01 12:38:05 -04:00
|
|
|
|
2017-04-04 12:28:59 -04:00
|
|
|
commitMessage.WriteString(" " + newVar)
|
2014-09-25 22:28:24 -04:00
|
|
|
gotOne := false
|
2017-04-26 17:45:16 -04:00
|
|
|
for i, envVar := range runConfig.Env {
|
2014-09-25 22:28:24 -04:00
|
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
2016-11-22 14:26:02 -05:00
|
|
|
compareFrom := envParts[0]
|
2017-04-04 12:28:59 -04:00
|
|
|
if equalEnvKeys(compareFrom, name) {
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Env[i] = newVar
|
2014-09-25 22:28:24 -04:00
|
|
|
gotOne = true
|
|
|
|
break
|
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
}
|
2014-09-25 22:28:24 -04:00
|
|
|
if !gotOne {
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Env = append(runConfig.Env, newVar)
|
2014-09-25 22:28:24 -04:00
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.commit(d.state, commitMessage.String())
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// MAINTAINER some text <maybe@an.email.address>
|
|
|
|
//
|
|
|
|
// Sets the maintainer metadata.
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchMaintainer(d dispatchRequest, c *instructions.MaintainerCommand) error {
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
d.state.maintainer = c.Maintainer
|
|
|
|
return d.builder.commit(d.state, "MAINTAINER "+c.Maintainer)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-02-17 10:20:06 -05:00
|
|
|
// LABEL some json data describing the image
|
|
|
|
//
|
|
|
|
// Sets the Label variable foo to bar,
|
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchLabel(d dispatchRequest, c *instructions.LabelCommand) error {
|
|
|
|
if d.state.runConfig.Labels == nil {
|
|
|
|
d.state.runConfig.Labels = make(map[string]string)
|
2015-05-05 17:27:42 -04:00
|
|
|
}
|
2015-02-17 10:20:06 -05:00
|
|
|
commitStr := "LABEL"
|
2017-05-22 11:21:17 -04:00
|
|
|
for _, v := range c.Labels {
|
|
|
|
d.state.runConfig.Labels[v.Key] = v.Value
|
|
|
|
commitStr += " " + v.String()
|
2015-02-17 10:20:06 -05:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.commit(d.state, commitStr)
|
2015-02-17 10:20:06 -05:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// ADD foo /path
|
|
|
|
//
|
|
|
|
// Add the file 'foo' to '/path'. Tarball and Remote URL (git, http) handling
|
|
|
|
// exist here. If you do not wish to have this automatic handling, use COPY.
|
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchAdd(d dispatchRequest, c *instructions.AddCommand) error {
|
|
|
|
downloader := newRemoteSourceDownloader(d.builder.Output, d.builder.Stdout)
|
|
|
|
copier := copierFromDispatchRequest(d, downloader, nil)
|
2017-05-07 14:37:46 -04:00
|
|
|
defer copier.Cleanup()
|
2017-05-22 11:21:17 -04:00
|
|
|
|
|
|
|
copyInstruction, err := copier.createCopyInstruction(c.SourcesAndDest, "ADD")
|
2017-05-07 14:37:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
copyInstruction.chownStr = c.Chown
|
2017-05-07 14:37:46 -04:00
|
|
|
copyInstruction.allowLocalDecompression = true
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.performCopy(d.state, copyInstruction)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// COPY foo /path
|
|
|
|
//
|
|
|
|
// Same as 'ADD' but without the tar and remote url handling.
|
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchCopy(d dispatchRequest, c *instructions.CopyCommand) error {
|
|
|
|
var im *imageMount
|
|
|
|
var err error
|
|
|
|
if c.From != "" {
|
|
|
|
im, err = d.getImageMount(c.From)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "invalid from flag value %s", c.From)
|
|
|
|
}
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
copier := copierFromDispatchRequest(d, errOnSourceDownload, im)
|
2017-05-07 14:37:46 -04:00
|
|
|
defer copier.Cleanup()
|
2017-05-22 11:21:17 -04:00
|
|
|
copyInstruction, err := copier.createCopyInstruction(c.SourcesAndDest, "COPY")
|
2017-05-07 14:37:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
copyInstruction.chownStr = c.Chown
|
2017-05-07 14:37:46 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.performCopy(d.state, copyInstruction)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func (d *dispatchRequest) getImageMount(imageRefOrID string) (*imageMount, error) {
|
|
|
|
if imageRefOrID == "" {
|
2017-05-05 18:52:11 -04:00
|
|
|
// TODO: this could return the source in the default case as well?
|
2017-03-27 21:36:28 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
|
2017-06-19 20:15:23 -04:00
|
|
|
var localOnly bool
|
2017-05-22 11:21:17 -04:00
|
|
|
stage, err := d.stages.get(imageRefOrID)
|
2017-05-05 18:52:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if stage != nil {
|
2017-05-22 11:21:17 -04:00
|
|
|
imageRefOrID = stage.Image
|
2017-06-19 20:15:23 -04:00
|
|
|
localOnly = true
|
2017-03-27 21:36:28 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.imageSources.Get(imageRefOrID, localOnly)
|
2017-03-27 21:36:28 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 20:51:42 -04:00
|
|
|
// FROM imagename[:tag | @digest] [AS build-stage-name]
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func initializeStage(d dispatchRequest, cmd *instructions.Stage) error {
|
|
|
|
d.builder.imageProber.Reset()
|
|
|
|
image, err := d.getFromImage(d.shlex, cmd.BaseName)
|
2017-02-24 17:19:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
state := d.state
|
|
|
|
state.beginStage(cmd.Name, image)
|
|
|
|
if len(state.runConfig.OnBuild) > 0 {
|
|
|
|
triggers := state.runConfig.OnBuild
|
|
|
|
state.runConfig.OnBuild = nil
|
|
|
|
return dispatchTriggeredOnBuild(d, triggers)
|
2017-05-05 13:05:25 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return nil
|
2017-03-27 20:51:42 -04:00
|
|
|
}
|
2016-01-03 22:45:06 -05:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchTriggeredOnBuild(d dispatchRequest, triggers []string) error {
|
|
|
|
fmt.Fprintf(d.builder.Stdout, "# Executing %d build trigger", len(triggers))
|
|
|
|
if len(triggers) > 1 {
|
|
|
|
fmt.Fprint(d.builder.Stdout, "s")
|
|
|
|
}
|
|
|
|
fmt.Fprintln(d.builder.Stdout)
|
|
|
|
for _, trigger := range triggers {
|
|
|
|
d.state.updateRunConfig()
|
|
|
|
ast, err := parser.Parse(strings.NewReader(trigger))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(ast.AST.Children) != 1 {
|
|
|
|
return errors.New("onbuild trigger should be a single expression")
|
|
|
|
}
|
|
|
|
cmd, err := instructions.ParseCommand(ast.AST.Children[0])
|
|
|
|
if err != nil {
|
|
|
|
if instructions.IsUnknownInstruction(err) {
|
|
|
|
buildsFailed.WithValues(metricsUnknownInstructionError).Inc()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = dispatch(d, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-27 20:51:42 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return nil
|
2017-03-27 20:51:42 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
// scratchImage is used as a token for the empty base image. It uses buildStage
|
|
|
|
// as a convenient implementation of builder.Image, but is not actually a
|
|
|
|
// buildStage.
|
2017-05-14 14:18:48 -04:00
|
|
|
var scratchImage builder.Image = &image.Image{}
|
2017-05-05 18:52:11 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func (d *dispatchRequest) getExpandedImageName(shlex *ShellLex, name string) (string, error) {
|
2017-03-27 20:51:42 -04:00
|
|
|
substitutionArgs := []string{}
|
2017-05-22 11:21:17 -04:00
|
|
|
for key, value := range d.state.buildArgs.GetAllMeta() {
|
2017-03-27 20:51:42 -04:00
|
|
|
substitutionArgs = append(substitutionArgs, key+"="+value)
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:24:41 -04:00
|
|
|
name, err := shlex.ProcessWord(name, substitutionArgs)
|
2017-03-27 20:51:42 -04:00
|
|
|
if err != nil {
|
2017-05-22 11:21:17 -04:00
|
|
|
return "", err
|
2017-03-20 13:28:21 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
func (d *dispatchRequest) getImageOrStage(name string) (builder.Image, error) {
|
2017-06-19 20:15:23 -04:00
|
|
|
var localOnly bool
|
2017-05-22 11:21:17 -04:00
|
|
|
if im, ok := d.stages.getByName(name); ok {
|
|
|
|
name = im.Image
|
2017-06-19 20:15:23 -04:00
|
|
|
localOnly = true
|
2017-03-22 21:36:08 -04:00
|
|
|
}
|
2016-01-03 22:45:06 -05:00
|
|
|
|
2017-05-17 20:08:01 -04:00
|
|
|
// Windows cannot support a container with no base image unless it is LCOW.
|
2017-03-27 20:51:42 -04:00
|
|
|
if name == api.NoBaseImageSpecifier {
|
|
|
|
if runtime.GOOS == "windows" {
|
2017-05-22 11:21:17 -04:00
|
|
|
if d.builder.platform == "windows" || (d.builder.platform != "windows" && !system.LCOWSupported()) {
|
2017-05-17 20:08:01 -04:00
|
|
|
return nil, errors.New("Windows does not support FROM scratch")
|
|
|
|
}
|
2017-03-27 20:51:42 -04:00
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
return scratchImage, nil
|
2017-03-27 20:51:42 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
imageMount, err := d.builder.imageSources.Get(name, localOnly)
|
2017-03-27 21:36:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
return imageMount.Image(), nil
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
func (d *dispatchRequest) getFromImage(shlex *ShellLex, name string) (builder.Image, error) {
|
|
|
|
name, err := d.getExpandedImageName(shlex, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-05 13:05:25 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.getImageOrStage(name)
|
2017-05-05 13:05:25 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchOnbuild(d dispatchRequest, c *instructions.OnbuildCommand) error {
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
d.state.runConfig.OnBuild = append(d.state.runConfig.OnBuild, c.Expression)
|
|
|
|
return d.builder.commit(d.state, "ONBUILD "+c.Expression)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// WORKDIR /tmp
|
|
|
|
//
|
|
|
|
// Set the working directory for future RUN/CMD/etc statements.
|
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchWorkdir(d dispatchRequest, c *instructions.WorkdirCommand) error {
|
|
|
|
runConfig := d.state.runConfig
|
|
|
|
var err error
|
|
|
|
runConfig.WorkingDir, err = normalizeWorkdir(d.builder.platform, runConfig.WorkingDir, c.Path)
|
2016-05-02 21:33:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2014-12-12 13:32:11 -05:00
|
|
|
|
2016-11-16 18:02:27 -05:00
|
|
|
// For performance reasons, we explicitly do a create/mkdir now
|
|
|
|
// This avoids having an unnecessary expensive mount/unmount calls
|
|
|
|
// (on Windows in particular) during each container create.
|
|
|
|
// Prior to 1.13, the mkdir was deferred and not executed at this step.
|
2017-05-22 11:21:17 -04:00
|
|
|
if d.builder.disableCommit {
|
2016-11-16 18:02:27 -05:00
|
|
|
// Don't call back into the daemon if we're going through docker commit --change "WORKDIR /foo".
|
|
|
|
// We've already updated the runConfig and that's enough.
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-28 18:44:10 -05:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
comment := "WORKDIR " + runConfig.WorkingDir
|
2017-05-22 11:21:17 -04:00
|
|
|
runConfigWithCommentCmd := copyRunConfig(runConfig, withCmdCommentString(comment, d.builder.platform))
|
|
|
|
containerID, err := d.builder.probeAndCreate(d.state, runConfigWithCommentCmd)
|
2017-04-13 18:44:36 -04:00
|
|
|
if err != nil || containerID == "" {
|
2016-11-28 18:44:10 -05:00
|
|
|
return err
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
if err := d.builder.docker.ContainerCreateWorkdir(containerID); err != nil {
|
2016-11-16 18:02:27 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-10-28 22:24:37 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.commitContainer(d.state, containerID, runConfigWithCommentCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolveCmdLine(cmd instructions.ShellDependantCmdLine, runConfig *container.Config, platform string) []string {
|
|
|
|
result := cmd.CmdLine
|
|
|
|
if cmd.PrependShell && result != nil {
|
|
|
|
result = append(getShell(runConfig, platform), result...)
|
|
|
|
}
|
|
|
|
return result
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// RUN some command yo
|
|
|
|
//
|
|
|
|
// run a command and commit the image. Args are automatically prepended with
|
2016-05-03 16:56:59 -04:00
|
|
|
// the current SHELL which defaults to 'sh -c' under linux or 'cmd /S /C' under
|
|
|
|
// Windows, in the event there is only one argument The difference in processing:
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
2017-06-20 18:08:58 -04:00
|
|
|
// RUN echo hi # sh -c echo hi (Linux and LCOW)
|
2015-05-05 11:39:37 -04:00
|
|
|
// RUN echo hi # cmd /S /C echo hi (Windows)
|
2014-08-07 01:56:44 -04:00
|
|
|
// RUN [ "echo", "hi" ] # echo hi
|
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchRun(d dispatchRequest, c *instructions.RunCommand) error {
|
2014-09-11 08:58:50 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
stateRunConfig := d.state.runConfig
|
|
|
|
cmdFromArgs := resolveCmdLine(c.ShellDependantCmdLine, stateRunConfig, d.builder.platform)
|
|
|
|
buildArgs := d.state.buildArgs.FilterAllowed(stateRunConfig.Env)
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2017-04-21 15:08:11 -04:00
|
|
|
saveCmd := cmdFromArgs
|
|
|
|
if len(buildArgs) > 0 {
|
2017-05-22 11:21:17 -04:00
|
|
|
saveCmd = prependEnvOnCmd(d.state.buildArgs, buildArgs, cmdFromArgs)
|
2016-01-05 11:48:09 -05:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfigForCacheProbe := copyRunConfig(stateRunConfig,
|
2017-04-25 12:21:43 -04:00
|
|
|
withCmd(saveCmd),
|
|
|
|
withEntrypointOverride(saveCmd, nil))
|
2017-05-22 11:21:17 -04:00
|
|
|
hit, err := d.builder.probeCache(d.state, runConfigForCacheProbe)
|
2017-04-21 14:11:21 -04:00
|
|
|
if err != nil || hit {
|
2014-08-05 18:41:09 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := copyRunConfig(stateRunConfig,
|
2017-04-21 15:08:11 -04:00
|
|
|
withCmd(cmdFromArgs),
|
2017-04-26 17:45:16 -04:00
|
|
|
withEnv(append(stateRunConfig.Env, buildArgs...)),
|
2017-04-25 12:21:43 -04:00
|
|
|
withEntrypointOverride(saveCmd, strslice.StrSlice{""}))
|
2017-04-21 15:08:11 -04:00
|
|
|
|
2015-11-09 14:49:16 -05:00
|
|
|
// set config as already being escaped, this prevents double escaping on windows
|
2017-04-21 15:08:11 -04:00
|
|
|
runConfig.ArgsEscaped = true
|
2014-11-14 13:59:14 -05:00
|
|
|
|
2017-04-21 15:08:11 -04:00
|
|
|
logrus.Debugf("[BUILDER] Command to be executed: %v", runConfig.Cmd)
|
2017-05-22 11:21:17 -04:00
|
|
|
cID, err := d.builder.create(runConfig)
|
2014-08-05 18:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
if err := d.builder.containerManager.Run(d.builder.clientCtx, cID, d.builder.Stdout, d.builder.Stderr); err != nil {
|
2017-04-13 18:44:36 -04:00
|
|
|
if err, ok := err.(*statusCodeError); ok {
|
|
|
|
// TODO: change error type, because jsonmessage.JSONError assumes HTTP
|
|
|
|
return &jsonmessage.JSONError{
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"The command '%s' returned a non-zero code: %d",
|
|
|
|
strings.Join(runConfig.Cmd, " "), err.StatusCode()),
|
|
|
|
Code: err.StatusCode(),
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-11-14 13:59:14 -05:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.commitContainer(d.state, cID, runConfigForCacheProbe)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-04-21 15:08:11 -04:00
|
|
|
// Derive the command to use for probeCache() and to commit in this container.
|
|
|
|
// Note that we only do this if there are any build-time env vars. Also, we
|
|
|
|
// use the special argument "|#" at the start of the args array. This will
|
|
|
|
// avoid conflicts with any RUN command since commands can not
|
|
|
|
// start with | (vertical bar). The "#" (number of build envs) is there to
|
|
|
|
// help ensure proper cache matches. We don't want a RUN command
|
|
|
|
// that starts with "foo=abc" to be considered part of a build-time env var.
|
|
|
|
//
|
|
|
|
// remove any unreferenced built-in args from the environment variables.
|
|
|
|
// These args are transparent so resulting image should be the same regardless
|
|
|
|
// of the value.
|
2017-04-28 12:49:50 -04:00
|
|
|
func prependEnvOnCmd(buildArgs *buildArgs, buildArgVars []string, cmd strslice.StrSlice) strslice.StrSlice {
|
|
|
|
var tmpBuildEnv []string
|
|
|
|
for _, env := range buildArgVars {
|
|
|
|
key := strings.SplitN(env, "=", 2)[0]
|
2017-05-09 11:25:33 -04:00
|
|
|
if buildArgs.IsReferencedOrNotBuiltin(key) {
|
2017-04-28 12:49:50 -04:00
|
|
|
tmpBuildEnv = append(tmpBuildEnv, env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(tmpBuildEnv)
|
|
|
|
tmpEnv := append([]string{fmt.Sprintf("|%d", len(tmpBuildEnv))}, tmpBuildEnv...)
|
|
|
|
return strslice.StrSlice(append(tmpEnv, cmd...))
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// CMD foo
|
|
|
|
//
|
|
|
|
// Set the default command to run in the container (which may be empty).
|
|
|
|
// Argument handling is the same as RUN.
|
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchCmd(d dispatchRequest, c *instructions.CmdCommand) error {
|
|
|
|
runConfig := d.state.runConfig
|
|
|
|
cmd := resolveCmdLine(c.ShellDependantCmdLine, runConfig, d.builder.platform)
|
|
|
|
runConfig.Cmd = cmd
|
2016-05-20 19:05:14 -04:00
|
|
|
// set config as already being escaped, this prevents double escaping on windows
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.ArgsEscaped = true
|
2015-04-10 20:05:21 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
if err := d.builder.commit(d.state, fmt.Sprintf("CMD %q", cmd)); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
if len(c.ShellDependantCmdLine.CmdLine) != 0 {
|
|
|
|
d.state.cmdSet = true
|
2014-08-30 07:34:09 -04:00
|
|
|
}
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 05:48:13 -04:00
|
|
|
// HEALTHCHECK foo
|
|
|
|
//
|
|
|
|
// Set the default healthcheck command to run in the container (which may be empty).
|
|
|
|
// Argument handling is the same as RUN.
|
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchHealthcheck(d dispatchRequest, c *instructions.HealthCheckCommand) error {
|
|
|
|
runConfig := d.state.runConfig
|
|
|
|
if runConfig.Healthcheck != nil {
|
|
|
|
oldCmd := runConfig.Healthcheck.Test
|
|
|
|
if len(oldCmd) > 0 && oldCmd[0] != "NONE" {
|
|
|
|
fmt.Fprintf(d.builder.Stdout, "Note: overriding previous HEALTHCHECK: %v\n", oldCmd)
|
2016-04-18 05:48:13 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
runConfig.Healthcheck = c.Health
|
|
|
|
return d.builder.commit(d.state, fmt.Sprintf("HEALTHCHECK %q", runConfig.Healthcheck))
|
2016-04-18 05:48:13 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// ENTRYPOINT /usr/sbin/nginx
|
|
|
|
//
|
2016-05-03 16:56:59 -04:00
|
|
|
// Set the entrypoint to /usr/sbin/nginx. Will accept the CMD as the arguments
|
|
|
|
// to /usr/sbin/nginx. Uses the default shell if not in JSON format.
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
// Handles command processing similar to CMD and RUN, only req.runConfig.Entrypoint
|
2017-04-13 14:37:32 -04:00
|
|
|
// is initialized at newBuilder time instead of through argument parsing.
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchEntrypoint(d dispatchRequest, c *instructions.EntrypointCommand) error {
|
|
|
|
runConfig := d.state.runConfig
|
|
|
|
cmd := resolveCmdLine(c.ShellDependantCmdLine, runConfig, d.builder.platform)
|
|
|
|
runConfig.Entrypoint = cmd
|
|
|
|
if !d.state.cmdSet {
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Cmd = nil
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.commit(d.state, fmt.Sprintf("ENTRYPOINT %q", runConfig.Entrypoint))
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// EXPOSE 6667/tcp 7000/tcp
|
|
|
|
//
|
|
|
|
// Expose ports for links and port mappings. This all ends up in
|
2017-04-11 14:34:05 -04:00
|
|
|
// req.runConfig.ExposedPorts for runconfig.
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchExpose(d dispatchRequest, c *instructions.ExposeCommand, envs []string) error {
|
|
|
|
// custom multi word expansion
|
|
|
|
// expose $FOO with FOO="80 443" is expanded as EXPOSE [80,443]. This is the only command supporting word to words expansion
|
|
|
|
// so the word processing has been de-generalized
|
|
|
|
ports := []string{}
|
|
|
|
for _, p := range c.Ports {
|
|
|
|
ps, err := d.shlex.ProcessWords(p, envs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ports = append(ports, ps...)
|
2015-02-04 12:34:25 -05:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
c.Ports = ports
|
2015-02-04 12:34:25 -05:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
ps, _, err := nat.ParsePortSpecs(ports)
|
|
|
|
if err != nil {
|
2015-05-05 17:27:42 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
if d.state.runConfig.ExposedPorts == nil {
|
|
|
|
d.state.runConfig.ExposedPorts = make(nat.PortSet)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
for p := range ps {
|
|
|
|
d.state.runConfig.ExposedPorts[p] = struct{}{}
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.commit(d.state, "EXPOSE "+strings.Join(c.Ports, " "))
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// USER foo
|
|
|
|
//
|
|
|
|
// Set the user to 'foo' for future commands and when running the
|
|
|
|
// ENTRYPOINT/CMD at container run time.
|
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchUser(d dispatchRequest, c *instructions.UserCommand) error {
|
|
|
|
d.state.runConfig.User = c.User
|
|
|
|
return d.builder.commit(d.state, fmt.Sprintf("USER %v", c.User))
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// VOLUME /foo
|
|
|
|
//
|
2014-09-11 09:27:51 -04:00
|
|
|
// Expose the volume /foo for use. Will also accept the JSON array form.
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchVolume(d dispatchRequest, c *instructions.VolumeCommand) error {
|
|
|
|
if d.state.runConfig.Volumes == nil {
|
|
|
|
d.state.runConfig.Volumes = map[string]struct{}{}
|
2015-05-05 17:27:42 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
for _, v := range c.Volumes {
|
2015-03-21 00:39:49 -04:00
|
|
|
if v == "" {
|
2016-12-25 01:37:31 -05:00
|
|
|
return errors.New("VOLUME specified can not be an empty string")
|
2015-03-21 00:39:49 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
d.state.runConfig.Volumes[v] = struct{}{}
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return d.builder.commit(d.state, fmt.Sprintf("VOLUME %v", c.Volumes))
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2015-08-18 13:30:44 -04:00
|
|
|
|
|
|
|
// STOPSIGNAL signal
|
|
|
|
//
|
|
|
|
// Set the signal that will be used to kill the container.
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchStopSignal(d dispatchRequest, c *instructions.StopSignalCommand) error {
|
2015-08-18 13:30:44 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
_, err := signal.ParseSignal(c.Signal)
|
2015-08-18 13:30:44 -04:00
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return validationError{err}
|
2015-08-18 13:30:44 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
d.state.runConfig.StopSignal = c.Signal
|
|
|
|
return d.builder.commit(d.state, fmt.Sprintf("STOPSIGNAL %v", c.Signal))
|
2015-08-18 13:30:44 -04:00
|
|
|
}
|
2014-11-14 13:59:14 -05:00
|
|
|
|
|
|
|
// ARG name[=value]
|
|
|
|
//
|
|
|
|
// Adds the variable foo to the trusted list of variables that can be passed
|
2017-02-16 10:56:53 -05:00
|
|
|
// to builder using the --build-arg flag for expansion/substitution or passing to 'run'.
|
2014-11-14 13:59:14 -05:00
|
|
|
// Dockerfile author may optionally set a default value of this variable.
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchArg(d dispatchRequest, c *instructions.ArgCommand) error {
|
2014-11-14 13:59:14 -05:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
commitStr := "ARG " + c.Key
|
|
|
|
if c.Value != nil {
|
|
|
|
commitStr += "=" + *c.Value
|
2014-11-14 13:59:14 -05:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
d.state.buildArgs.AddArg(c.Key, c.Value)
|
|
|
|
return d.builder.commit(d.state, commitStr)
|
2014-11-14 13:59:14 -05:00
|
|
|
}
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
|
2016-05-03 16:56:59 -04:00
|
|
|
// SHELL powershell -command
|
|
|
|
//
|
|
|
|
// Set the non-default shell to use.
|
2017-05-22 11:21:17 -04:00
|
|
|
func dispatchShell(d dispatchRequest, c *instructions.ShellCommand) error {
|
|
|
|
d.state.runConfig.Shell = c.Shell
|
|
|
|
return d.builder.commit(d.state, fmt.Sprintf("SHELL %v", d.state.runConfig.Shell))
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
}
|