|
@@ -10,17 +10,15 @@ package dockerfile
|
|
import (
|
|
import (
|
|
"bytes"
|
|
"bytes"
|
|
"fmt"
|
|
"fmt"
|
|
- "regexp"
|
|
|
|
"runtime"
|
|
"runtime"
|
|
"sort"
|
|
"sort"
|
|
- "strconv"
|
|
|
|
"strings"
|
|
"strings"
|
|
- "time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api"
|
|
"github.com/docker/docker/api"
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/api/types/strslice"
|
|
"github.com/docker/docker/api/types/strslice"
|
|
"github.com/docker/docker/builder"
|
|
"github.com/docker/docker/builder"
|
|
|
|
+ "github.com/docker/docker/builder/dockerfile/instructions"
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
|
"github.com/docker/docker/image"
|
|
"github.com/docker/docker/image"
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
@@ -36,32 +34,14 @@ import (
|
|
// Sets the environment variable foo to bar, also makes interpolation
|
|
// Sets the environment variable foo to bar, also makes interpolation
|
|
// in the dockerfile available from the next statement on via ${foo}.
|
|
// in the dockerfile available from the next statement on via ${foo}.
|
|
//
|
|
//
|
|
-func env(req dispatchRequest) error {
|
|
|
|
- if len(req.args) == 0 {
|
|
|
|
- return errAtLeastOneArgument("ENV")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if len(req.args)%2 != 0 {
|
|
|
|
- // should never get here, but just in case
|
|
|
|
- return errTooManyArguments("ENV")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- runConfig := req.state.runConfig
|
|
|
|
|
|
+func dispatchEnv(d dispatchRequest, c *instructions.EnvCommand) error {
|
|
|
|
+ runConfig := d.state.runConfig
|
|
commitMessage := bytes.NewBufferString("ENV")
|
|
commitMessage := bytes.NewBufferString("ENV")
|
|
|
|
+ for _, e := range c.Env {
|
|
|
|
+ name := e.Key
|
|
|
|
+ newVar := e.String()
|
|
|
|
|
|
- for j := 0; j < len(req.args); j += 2 {
|
|
|
|
- if len(req.args[j]) == 0 {
|
|
|
|
- return errBlankCommandNames("ENV")
|
|
|
|
- }
|
|
|
|
- name := req.args[j]
|
|
|
|
- value := req.args[j+1]
|
|
|
|
- newVar := name + "=" + value
|
|
|
|
commitMessage.WriteString(" " + newVar)
|
|
commitMessage.WriteString(" " + newVar)
|
|
-
|
|
|
|
gotOne := false
|
|
gotOne := false
|
|
for i, envVar := range runConfig.Env {
|
|
for i, envVar := range runConfig.Env {
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
|
@@ -76,64 +56,32 @@ func env(req dispatchRequest) error {
|
|
runConfig.Env = append(runConfig.Env, newVar)
|
|
runConfig.Env = append(runConfig.Env, newVar)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- return req.builder.commit(req.state, commitMessage.String())
|
|
|
|
|
|
+ return d.builder.commit(d.state, commitMessage.String())
|
|
}
|
|
}
|
|
|
|
|
|
// MAINTAINER some text <maybe@an.email.address>
|
|
// MAINTAINER some text <maybe@an.email.address>
|
|
//
|
|
//
|
|
// Sets the maintainer metadata.
|
|
// Sets the maintainer metadata.
|
|
-func maintainer(req dispatchRequest) error {
|
|
|
|
- if len(req.args) != 1 {
|
|
|
|
- return errExactlyOneArgument("MAINTAINER")
|
|
|
|
- }
|
|
|
|
|
|
+func dispatchMaintainer(d dispatchRequest, c *instructions.MaintainerCommand) error {
|
|
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- maintainer := req.args[0]
|
|
|
|
- req.state.maintainer = maintainer
|
|
|
|
- return req.builder.commit(req.state, "MAINTAINER "+maintainer)
|
|
|
|
|
|
+ d.state.maintainer = c.Maintainer
|
|
|
|
+ return d.builder.commit(d.state, "MAINTAINER "+c.Maintainer)
|
|
}
|
|
}
|
|
|
|
|
|
// LABEL some json data describing the image
|
|
// LABEL some json data describing the image
|
|
//
|
|
//
|
|
// Sets the Label variable foo to bar,
|
|
// Sets the Label variable foo to bar,
|
|
//
|
|
//
|
|
-func label(req dispatchRequest) error {
|
|
|
|
- if len(req.args) == 0 {
|
|
|
|
- return errAtLeastOneArgument("LABEL")
|
|
|
|
- }
|
|
|
|
- if len(req.args)%2 != 0 {
|
|
|
|
- // should never get here, but just in case
|
|
|
|
- return errTooManyArguments("LABEL")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
|
|
+func dispatchLabel(d dispatchRequest, c *instructions.LabelCommand) error {
|
|
|
|
+ if d.state.runConfig.Labels == nil {
|
|
|
|
+ d.state.runConfig.Labels = make(map[string]string)
|
|
}
|
|
}
|
|
-
|
|
|
|
commitStr := "LABEL"
|
|
commitStr := "LABEL"
|
|
- runConfig := req.state.runConfig
|
|
|
|
-
|
|
|
|
- if runConfig.Labels == nil {
|
|
|
|
- runConfig.Labels = map[string]string{}
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for j := 0; j < len(req.args); j++ {
|
|
|
|
- name := req.args[j]
|
|
|
|
- if name == "" {
|
|
|
|
- return errBlankCommandNames("LABEL")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- value := req.args[j+1]
|
|
|
|
- commitStr += " " + name + "=" + value
|
|
|
|
-
|
|
|
|
- runConfig.Labels[name] = value
|
|
|
|
- j++
|
|
|
|
|
|
+ for _, v := range c.Labels {
|
|
|
|
+ d.state.runConfig.Labels[v.Key] = v.Value
|
|
|
|
+ commitStr += " " + v.String()
|
|
}
|
|
}
|
|
- return req.builder.commit(req.state, commitStr)
|
|
|
|
|
|
+ return d.builder.commit(d.state, commitStr)
|
|
}
|
|
}
|
|
|
|
|
|
// ADD foo /path
|
|
// ADD foo /path
|
|
@@ -141,257 +89,172 @@ func label(req dispatchRequest) error {
|
|
// Add the file 'foo' to '/path'. Tarball and Remote URL (git, http) handling
|
|
// 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.
|
|
// exist here. If you do not wish to have this automatic handling, use COPY.
|
|
//
|
|
//
|
|
-func add(req dispatchRequest) error {
|
|
|
|
- if len(req.args) < 2 {
|
|
|
|
- return errAtLeastTwoArguments("ADD")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- flChown := req.flags.AddString("chown", "")
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- downloader := newRemoteSourceDownloader(req.builder.Output, req.builder.Stdout)
|
|
|
|
- copier := copierFromDispatchRequest(req, downloader, nil)
|
|
|
|
|
|
+func dispatchAdd(d dispatchRequest, c *instructions.AddCommand) error {
|
|
|
|
+ downloader := newRemoteSourceDownloader(d.builder.Output, d.builder.Stdout)
|
|
|
|
+ copier := copierFromDispatchRequest(d, downloader, nil)
|
|
defer copier.Cleanup()
|
|
defer copier.Cleanup()
|
|
- copyInstruction, err := copier.createCopyInstruction(req.args, "ADD")
|
|
|
|
|
|
+
|
|
|
|
+ copyInstruction, err := copier.createCopyInstruction(c.SourcesAndDest, "ADD")
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- copyInstruction.chownStr = flChown.Value
|
|
|
|
|
|
+ copyInstruction.chownStr = c.Chown
|
|
copyInstruction.allowLocalDecompression = true
|
|
copyInstruction.allowLocalDecompression = true
|
|
|
|
|
|
- return req.builder.performCopy(req.state, copyInstruction)
|
|
|
|
|
|
+ return d.builder.performCopy(d.state, copyInstruction)
|
|
}
|
|
}
|
|
|
|
|
|
// COPY foo /path
|
|
// COPY foo /path
|
|
//
|
|
//
|
|
// Same as 'ADD' but without the tar and remote url handling.
|
|
// Same as 'ADD' but without the tar and remote url handling.
|
|
//
|
|
//
|
|
-func dispatchCopy(req dispatchRequest) error {
|
|
|
|
- if len(req.args) < 2 {
|
|
|
|
- return errAtLeastTwoArguments("COPY")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- flFrom := req.flags.AddString("from", "")
|
|
|
|
- flChown := req.flags.AddString("chown", "")
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- im, err := req.builder.getImageMount(flFrom)
|
|
|
|
- if err != nil {
|
|
|
|
- return errors.Wrapf(err, "invalid from flag value %s", flFrom.Value)
|
|
|
|
|
|
+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)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
- copier := copierFromDispatchRequest(req, errOnSourceDownload, im)
|
|
|
|
|
|
+ copier := copierFromDispatchRequest(d, errOnSourceDownload, im)
|
|
defer copier.Cleanup()
|
|
defer copier.Cleanup()
|
|
- copyInstruction, err := copier.createCopyInstruction(req.args, "COPY")
|
|
|
|
|
|
+ copyInstruction, err := copier.createCopyInstruction(c.SourcesAndDest, "COPY")
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- copyInstruction.chownStr = flChown.Value
|
|
|
|
|
|
+ copyInstruction.chownStr = c.Chown
|
|
|
|
|
|
- return req.builder.performCopy(req.state, copyInstruction)
|
|
|
|
|
|
+ return d.builder.performCopy(d.state, copyInstruction)
|
|
}
|
|
}
|
|
|
|
|
|
-func (b *Builder) getImageMount(fromFlag *Flag) (*imageMount, error) {
|
|
|
|
- if !fromFlag.IsUsed() {
|
|
|
|
|
|
+func (d *dispatchRequest) getImageMount(imageRefOrID string) (*imageMount, error) {
|
|
|
|
+ if imageRefOrID == "" {
|
|
// TODO: this could return the source in the default case as well?
|
|
// TODO: this could return the source in the default case as well?
|
|
return nil, nil
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
|
|
var localOnly bool
|
|
var localOnly bool
|
|
- imageRefOrID := fromFlag.Value
|
|
|
|
- stage, err := b.buildStages.get(fromFlag.Value)
|
|
|
|
|
|
+ stage, err := d.stages.get(imageRefOrID)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
if stage != nil {
|
|
if stage != nil {
|
|
- imageRefOrID = stage.ImageID()
|
|
|
|
|
|
+ imageRefOrID = stage.Image
|
|
localOnly = true
|
|
localOnly = true
|
|
}
|
|
}
|
|
- return b.imageSources.Get(imageRefOrID, localOnly)
|
|
|
|
|
|
+ return d.builder.imageSources.Get(imageRefOrID, localOnly)
|
|
}
|
|
}
|
|
|
|
|
|
// FROM imagename[:tag | @digest] [AS build-stage-name]
|
|
// FROM imagename[:tag | @digest] [AS build-stage-name]
|
|
//
|
|
//
|
|
-func from(req dispatchRequest) error {
|
|
|
|
- stageName, err := parseBuildStageName(req.args)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- req.builder.imageProber.Reset()
|
|
|
|
- image, err := req.builder.getFromImage(req.shlex, req.args[0])
|
|
|
|
|
|
+func initializeStage(d dispatchRequest, cmd *instructions.Stage) error {
|
|
|
|
+ d.builder.imageProber.Reset()
|
|
|
|
+ image, err := d.getFromImage(d.shlex, cmd.BaseName)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- if err := req.builder.buildStages.add(stageName, image); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- req.state.beginStage(stageName, image)
|
|
|
|
- req.builder.buildArgs.ResetAllowed()
|
|
|
|
- if image.ImageID() == "" {
|
|
|
|
- // Typically this means they used "FROM scratch"
|
|
|
|
- return nil
|
|
|
|
|
|
+ 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)
|
|
}
|
|
}
|
|
-
|
|
|
|
- return processOnBuild(req)
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func parseBuildStageName(args []string) (string, error) {
|
|
|
|
- stageName := ""
|
|
|
|
- switch {
|
|
|
|
- case len(args) == 3 && strings.EqualFold(args[1], "as"):
|
|
|
|
- stageName = strings.ToLower(args[2])
|
|
|
|
- if ok, _ := regexp.MatchString("^[a-z][a-z0-9-_\\.]*$", stageName); !ok {
|
|
|
|
- return "", errors.Errorf("invalid name for build stage: %q, name can't start with a number or contain symbols", stageName)
|
|
|
|
|
|
+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
|
|
}
|
|
}
|
|
- case len(args) != 1:
|
|
|
|
- return "", errors.New("FROM requires either one or three arguments")
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
- return stageName, nil
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
-// scratchImage is used as a token for the empty base image.
|
|
|
|
|
|
+// 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.
|
|
var scratchImage builder.Image = &image.Image{}
|
|
var scratchImage builder.Image = &image.Image{}
|
|
|
|
|
|
-func (b *Builder) getFromImage(shlex *ShellLex, name string) (builder.Image, error) {
|
|
|
|
|
|
+func (d *dispatchRequest) getExpandedImageName(shlex *ShellLex, name string) (string, error) {
|
|
substitutionArgs := []string{}
|
|
substitutionArgs := []string{}
|
|
- for key, value := range b.buildArgs.GetAllMeta() {
|
|
|
|
|
|
+ for key, value := range d.state.buildArgs.GetAllMeta() {
|
|
substitutionArgs = append(substitutionArgs, key+"="+value)
|
|
substitutionArgs = append(substitutionArgs, key+"="+value)
|
|
}
|
|
}
|
|
|
|
|
|
name, err := shlex.ProcessWord(name, substitutionArgs)
|
|
name, err := shlex.ProcessWord(name, substitutionArgs)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return "", err
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ return name, nil
|
|
|
|
+}
|
|
|
|
+func (d *dispatchRequest) getImageOrStage(name string) (builder.Image, error) {
|
|
var localOnly bool
|
|
var localOnly bool
|
|
- if stage, ok := b.buildStages.getByName(name); ok {
|
|
|
|
- name = stage.ImageID()
|
|
|
|
|
|
+ if im, ok := d.stages.getByName(name); ok {
|
|
|
|
+ name = im.Image
|
|
localOnly = true
|
|
localOnly = true
|
|
}
|
|
}
|
|
|
|
|
|
// Windows cannot support a container with no base image unless it is LCOW.
|
|
// Windows cannot support a container with no base image unless it is LCOW.
|
|
if name == api.NoBaseImageSpecifier {
|
|
if name == api.NoBaseImageSpecifier {
|
|
if runtime.GOOS == "windows" {
|
|
if runtime.GOOS == "windows" {
|
|
- if b.platform == "windows" || (b.platform != "windows" && !system.LCOWSupported()) {
|
|
|
|
|
|
+ if d.builder.platform == "windows" || (d.builder.platform != "windows" && !system.LCOWSupported()) {
|
|
return nil, errors.New("Windows does not support FROM scratch")
|
|
return nil, errors.New("Windows does not support FROM scratch")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return scratchImage, nil
|
|
return scratchImage, nil
|
|
}
|
|
}
|
|
- imageMount, err := b.imageSources.Get(name, localOnly)
|
|
|
|
|
|
+ imageMount, err := d.builder.imageSources.Get(name, localOnly)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
return imageMount.Image(), nil
|
|
return imageMount.Image(), nil
|
|
}
|
|
}
|
|
-
|
|
|
|
-func processOnBuild(req dispatchRequest) error {
|
|
|
|
- dispatchState := req.state
|
|
|
|
- // Process ONBUILD triggers if they exist
|
|
|
|
- if nTriggers := len(dispatchState.runConfig.OnBuild); nTriggers != 0 {
|
|
|
|
- word := "trigger"
|
|
|
|
- if nTriggers > 1 {
|
|
|
|
- word = "triggers"
|
|
|
|
- }
|
|
|
|
- fmt.Fprintf(req.builder.Stderr, "# Executing %d build %s...\n", nTriggers, word)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // Copy the ONBUILD triggers, and remove them from the config, since the config will be committed.
|
|
|
|
- onBuildTriggers := dispatchState.runConfig.OnBuild
|
|
|
|
- dispatchState.runConfig.OnBuild = []string{}
|
|
|
|
-
|
|
|
|
- // Reset stdin settings as all build actions run without stdin
|
|
|
|
- dispatchState.runConfig.OpenStdin = false
|
|
|
|
- dispatchState.runConfig.StdinOnce = false
|
|
|
|
-
|
|
|
|
- // parse the ONBUILD triggers by invoking the parser
|
|
|
|
- for _, step := range onBuildTriggers {
|
|
|
|
- dockerfile, err := parser.Parse(strings.NewReader(step))
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for _, n := range dockerfile.AST.Children {
|
|
|
|
- if err := checkDispatch(n); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- upperCasedCmd := strings.ToUpper(n.Value)
|
|
|
|
- switch upperCasedCmd {
|
|
|
|
- case "ONBUILD":
|
|
|
|
- return errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
|
|
|
|
- case "MAINTAINER", "FROM":
|
|
|
|
- return errors.Errorf("%s isn't allowed as an ONBUILD trigger", upperCasedCmd)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if _, err := dispatchFromDockerfile(req.builder, dockerfile, dispatchState, req.source); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
|
|
+func (d *dispatchRequest) getFromImage(shlex *ShellLex, name string) (builder.Image, error) {
|
|
|
|
+ name, err := d.getExpandedImageName(shlex, name)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
}
|
|
}
|
|
- return nil
|
|
|
|
|
|
+ return d.getImageOrStage(name)
|
|
}
|
|
}
|
|
|
|
|
|
-// ONBUILD RUN echo yo
|
|
|
|
-//
|
|
|
|
-// ONBUILD triggers run when the image is used in a FROM statement.
|
|
|
|
-//
|
|
|
|
-// ONBUILD handling has a lot of special-case functionality, the heading in
|
|
|
|
-// evaluator.go and comments around dispatch() in the same file explain the
|
|
|
|
-// special cases. search for 'OnBuild' in internals.go for additional special
|
|
|
|
-// cases.
|
|
|
|
-//
|
|
|
|
-func onbuild(req dispatchRequest) error {
|
|
|
|
- if len(req.args) == 0 {
|
|
|
|
- return errAtLeastOneArgument("ONBUILD")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- triggerInstruction := strings.ToUpper(strings.TrimSpace(req.args[0]))
|
|
|
|
- switch triggerInstruction {
|
|
|
|
- case "ONBUILD":
|
|
|
|
- return errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
|
|
|
|
- case "MAINTAINER", "FROM":
|
|
|
|
- return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", triggerInstruction)
|
|
|
|
- }
|
|
|
|
|
|
+func dispatchOnbuild(d dispatchRequest, c *instructions.OnbuildCommand) error {
|
|
|
|
|
|
- runConfig := req.state.runConfig
|
|
|
|
- original := regexp.MustCompile(`(?i)^\s*ONBUILD\s*`).ReplaceAllString(req.original, "")
|
|
|
|
- runConfig.OnBuild = append(runConfig.OnBuild, original)
|
|
|
|
- return req.builder.commit(req.state, "ONBUILD "+original)
|
|
|
|
|
|
+ d.state.runConfig.OnBuild = append(d.state.runConfig.OnBuild, c.Expression)
|
|
|
|
+ return d.builder.commit(d.state, "ONBUILD "+c.Expression)
|
|
}
|
|
}
|
|
|
|
|
|
// WORKDIR /tmp
|
|
// WORKDIR /tmp
|
|
//
|
|
//
|
|
// Set the working directory for future RUN/CMD/etc statements.
|
|
// Set the working directory for future RUN/CMD/etc statements.
|
|
//
|
|
//
|
|
-func workdir(req dispatchRequest) error {
|
|
|
|
- if len(req.args) != 1 {
|
|
|
|
- return errExactlyOneArgument("WORKDIR")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- err := req.flags.Parse()
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- runConfig := req.state.runConfig
|
|
|
|
- // This is from the Dockerfile and will not necessarily be in platform
|
|
|
|
- // specific semantics, hence ensure it is converted.
|
|
|
|
- runConfig.WorkingDir, err = normalizeWorkdir(req.builder.platform, runConfig.WorkingDir, req.args[0])
|
|
|
|
|
|
+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)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
@@ -400,23 +263,31 @@ func workdir(req dispatchRequest) error {
|
|
// This avoids having an unnecessary expensive mount/unmount calls
|
|
// This avoids having an unnecessary expensive mount/unmount calls
|
|
// (on Windows in particular) during each container create.
|
|
// (on Windows in particular) during each container create.
|
|
// Prior to 1.13, the mkdir was deferred and not executed at this step.
|
|
// Prior to 1.13, the mkdir was deferred and not executed at this step.
|
|
- if req.builder.disableCommit {
|
|
|
|
|
|
+ if d.builder.disableCommit {
|
|
// Don't call back into the daemon if we're going through docker commit --change "WORKDIR /foo".
|
|
// 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.
|
|
// We've already updated the runConfig and that's enough.
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
comment := "WORKDIR " + runConfig.WorkingDir
|
|
comment := "WORKDIR " + runConfig.WorkingDir
|
|
- runConfigWithCommentCmd := copyRunConfig(runConfig, withCmdCommentString(comment, req.builder.platform))
|
|
|
|
- containerID, err := req.builder.probeAndCreate(req.state, runConfigWithCommentCmd)
|
|
|
|
|
|
+ runConfigWithCommentCmd := copyRunConfig(runConfig, withCmdCommentString(comment, d.builder.platform))
|
|
|
|
+ containerID, err := d.builder.probeAndCreate(d.state, runConfigWithCommentCmd)
|
|
if err != nil || containerID == "" {
|
|
if err != nil || containerID == "" {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- if err := req.builder.docker.ContainerCreateWorkdir(containerID); err != nil {
|
|
|
|
|
|
+ if err := d.builder.docker.ContainerCreateWorkdir(containerID); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- return req.builder.commitContainer(req.state, containerID, runConfigWithCommentCmd)
|
|
|
|
|
|
+ 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
|
|
}
|
|
}
|
|
|
|
|
|
// RUN some command yo
|
|
// RUN some command yo
|
|
@@ -429,32 +300,21 @@ func workdir(req dispatchRequest) error {
|
|
// RUN echo hi # cmd /S /C echo hi (Windows)
|
|
// RUN echo hi # cmd /S /C echo hi (Windows)
|
|
// RUN [ "echo", "hi" ] # echo hi
|
|
// RUN [ "echo", "hi" ] # echo hi
|
|
//
|
|
//
|
|
-func run(req dispatchRequest) error {
|
|
|
|
- if !req.state.hasFromImage() {
|
|
|
|
- return errors.New("Please provide a source image with `from` prior to run")
|
|
|
|
- }
|
|
|
|
|
|
+func dispatchRun(d dispatchRequest, c *instructions.RunCommand) error {
|
|
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- stateRunConfig := req.state.runConfig
|
|
|
|
- args := handleJSONArgs(req.args, req.attributes)
|
|
|
|
- if !req.attributes["json"] {
|
|
|
|
- args = append(getShell(stateRunConfig, req.builder.platform), args...)
|
|
|
|
- }
|
|
|
|
- cmdFromArgs := strslice.StrSlice(args)
|
|
|
|
- buildArgs := req.builder.buildArgs.FilterAllowed(stateRunConfig.Env)
|
|
|
|
|
|
+ stateRunConfig := d.state.runConfig
|
|
|
|
+ cmdFromArgs := resolveCmdLine(c.ShellDependantCmdLine, stateRunConfig, d.builder.platform)
|
|
|
|
+ buildArgs := d.state.buildArgs.FilterAllowed(stateRunConfig.Env)
|
|
|
|
|
|
saveCmd := cmdFromArgs
|
|
saveCmd := cmdFromArgs
|
|
if len(buildArgs) > 0 {
|
|
if len(buildArgs) > 0 {
|
|
- saveCmd = prependEnvOnCmd(req.builder.buildArgs, buildArgs, cmdFromArgs)
|
|
|
|
|
|
+ saveCmd = prependEnvOnCmd(d.state.buildArgs, buildArgs, cmdFromArgs)
|
|
}
|
|
}
|
|
|
|
|
|
runConfigForCacheProbe := copyRunConfig(stateRunConfig,
|
|
runConfigForCacheProbe := copyRunConfig(stateRunConfig,
|
|
withCmd(saveCmd),
|
|
withCmd(saveCmd),
|
|
withEntrypointOverride(saveCmd, nil))
|
|
withEntrypointOverride(saveCmd, nil))
|
|
- hit, err := req.builder.probeCache(req.state, runConfigForCacheProbe)
|
|
|
|
|
|
+ hit, err := d.builder.probeCache(d.state, runConfigForCacheProbe)
|
|
if err != nil || hit {
|
|
if err != nil || hit {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
@@ -468,11 +328,11 @@ func run(req dispatchRequest) error {
|
|
runConfig.ArgsEscaped = true
|
|
runConfig.ArgsEscaped = true
|
|
|
|
|
|
logrus.Debugf("[BUILDER] Command to be executed: %v", runConfig.Cmd)
|
|
logrus.Debugf("[BUILDER] Command to be executed: %v", runConfig.Cmd)
|
|
- cID, err := req.builder.create(runConfig)
|
|
|
|
|
|
+ cID, err := d.builder.create(runConfig)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- if err := req.builder.containerManager.Run(req.builder.clientCtx, cID, req.builder.Stdout, req.builder.Stderr); err != nil {
|
|
|
|
|
|
+ if err := d.builder.containerManager.Run(d.builder.clientCtx, cID, d.builder.Stdout, d.builder.Stderr); err != nil {
|
|
if err, ok := err.(*statusCodeError); ok {
|
|
if err, ok := err.(*statusCodeError); ok {
|
|
// TODO: change error type, because jsonmessage.JSONError assumes HTTP
|
|
// TODO: change error type, because jsonmessage.JSONError assumes HTTP
|
|
return &jsonmessage.JSONError{
|
|
return &jsonmessage.JSONError{
|
|
@@ -485,7 +345,7 @@ func run(req dispatchRequest) error {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- return req.builder.commitContainer(req.state, cID, runConfigForCacheProbe)
|
|
|
|
|
|
+ return d.builder.commitContainer(d.state, cID, runConfigForCacheProbe)
|
|
}
|
|
}
|
|
|
|
|
|
// Derive the command to use for probeCache() and to commit in this container.
|
|
// Derive the command to use for probeCache() and to commit in this container.
|
|
@@ -518,139 +378,39 @@ func prependEnvOnCmd(buildArgs *buildArgs, buildArgVars []string, cmd strslice.S
|
|
// Set the default command to run in the container (which may be empty).
|
|
// Set the default command to run in the container (which may be empty).
|
|
// Argument handling is the same as RUN.
|
|
// Argument handling is the same as RUN.
|
|
//
|
|
//
|
|
-func cmd(req dispatchRequest) error {
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- runConfig := req.state.runConfig
|
|
|
|
- cmdSlice := handleJSONArgs(req.args, req.attributes)
|
|
|
|
- if !req.attributes["json"] {
|
|
|
|
- cmdSlice = append(getShell(runConfig, req.builder.platform), cmdSlice...)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- runConfig.Cmd = strslice.StrSlice(cmdSlice)
|
|
|
|
|
|
+func dispatchCmd(d dispatchRequest, c *instructions.CmdCommand) error {
|
|
|
|
+ runConfig := d.state.runConfig
|
|
|
|
+ cmd := resolveCmdLine(c.ShellDependantCmdLine, runConfig, d.builder.platform)
|
|
|
|
+ runConfig.Cmd = cmd
|
|
// set config as already being escaped, this prevents double escaping on windows
|
|
// set config as already being escaped, this prevents double escaping on windows
|
|
runConfig.ArgsEscaped = true
|
|
runConfig.ArgsEscaped = true
|
|
|
|
|
|
- if err := req.builder.commit(req.state, fmt.Sprintf("CMD %q", cmdSlice)); err != nil {
|
|
|
|
|
|
+ if err := d.builder.commit(d.state, fmt.Sprintf("CMD %q", cmd)); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- if len(req.args) != 0 {
|
|
|
|
- req.state.cmdSet = true
|
|
|
|
|
|
+ if len(c.ShellDependantCmdLine.CmdLine) != 0 {
|
|
|
|
+ d.state.cmdSet = true
|
|
}
|
|
}
|
|
|
|
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-// parseOptInterval(flag) is the duration of flag.Value, or 0 if
|
|
|
|
-// empty. An error is reported if the value is given and less than minimum duration.
|
|
|
|
-func parseOptInterval(f *Flag) (time.Duration, error) {
|
|
|
|
- s := f.Value
|
|
|
|
- if s == "" {
|
|
|
|
- return 0, nil
|
|
|
|
- }
|
|
|
|
- d, err := time.ParseDuration(s)
|
|
|
|
- if err != nil {
|
|
|
|
- return 0, err
|
|
|
|
- }
|
|
|
|
- if d < container.MinimumDuration {
|
|
|
|
- return 0, fmt.Errorf("Interval %#v cannot be less than %s", f.name, container.MinimumDuration)
|
|
|
|
- }
|
|
|
|
- return d, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// HEALTHCHECK foo
|
|
// HEALTHCHECK foo
|
|
//
|
|
//
|
|
// Set the default healthcheck command to run in the container (which may be empty).
|
|
// Set the default healthcheck command to run in the container (which may be empty).
|
|
// Argument handling is the same as RUN.
|
|
// Argument handling is the same as RUN.
|
|
//
|
|
//
|
|
-func healthcheck(req dispatchRequest) error {
|
|
|
|
- if len(req.args) == 0 {
|
|
|
|
- return errAtLeastOneArgument("HEALTHCHECK")
|
|
|
|
- }
|
|
|
|
- runConfig := req.state.runConfig
|
|
|
|
- typ := strings.ToUpper(req.args[0])
|
|
|
|
- args := req.args[1:]
|
|
|
|
- if typ == "NONE" {
|
|
|
|
- if len(args) != 0 {
|
|
|
|
- return errors.New("HEALTHCHECK NONE takes no arguments")
|
|
|
|
- }
|
|
|
|
- test := strslice.StrSlice{typ}
|
|
|
|
- runConfig.Healthcheck = &container.HealthConfig{
|
|
|
|
- Test: test,
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- if runConfig.Healthcheck != nil {
|
|
|
|
- oldCmd := runConfig.Healthcheck.Test
|
|
|
|
- if len(oldCmd) > 0 && oldCmd[0] != "NONE" {
|
|
|
|
- fmt.Fprintf(req.builder.Stdout, "Note: overriding previous HEALTHCHECK: %v\n", oldCmd)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- healthcheck := container.HealthConfig{}
|
|
|
|
-
|
|
|
|
- flInterval := req.flags.AddString("interval", "")
|
|
|
|
- flTimeout := req.flags.AddString("timeout", "")
|
|
|
|
- flStartPeriod := req.flags.AddString("start-period", "")
|
|
|
|
- flRetries := req.flags.AddString("retries", "")
|
|
|
|
-
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- switch typ {
|
|
|
|
- case "CMD":
|
|
|
|
- cmdSlice := handleJSONArgs(args, req.attributes)
|
|
|
|
- if len(cmdSlice) == 0 {
|
|
|
|
- return errors.New("Missing command after HEALTHCHECK CMD")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if !req.attributes["json"] {
|
|
|
|
- typ = "CMD-SHELL"
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- healthcheck.Test = strslice.StrSlice(append([]string{typ}, cmdSlice...))
|
|
|
|
- default:
|
|
|
|
- return fmt.Errorf("Unknown type %#v in HEALTHCHECK (try CMD)", typ)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- interval, err := parseOptInterval(flInterval)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- healthcheck.Interval = interval
|
|
|
|
-
|
|
|
|
- timeout, err := parseOptInterval(flTimeout)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- healthcheck.Timeout = timeout
|
|
|
|
-
|
|
|
|
- startPeriod, err := parseOptInterval(flStartPeriod)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- healthcheck.StartPeriod = startPeriod
|
|
|
|
-
|
|
|
|
- if flRetries.Value != "" {
|
|
|
|
- retries, err := strconv.ParseInt(flRetries.Value, 10, 32)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- if retries < 1 {
|
|
|
|
- return fmt.Errorf("--retries must be at least 1 (not %d)", retries)
|
|
|
|
- }
|
|
|
|
- healthcheck.Retries = int(retries)
|
|
|
|
- } else {
|
|
|
|
- healthcheck.Retries = 0
|
|
|
|
|
|
+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)
|
|
}
|
|
}
|
|
-
|
|
|
|
- runConfig.Healthcheck = &healthcheck
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
- return req.builder.commit(req.state, fmt.Sprintf("HEALTHCHECK %q", runConfig.Healthcheck))
|
|
|
|
|
|
+ runConfig.Healthcheck = c.Health
|
|
|
|
+ return d.builder.commit(d.state, fmt.Sprintf("HEALTHCHECK %q", runConfig.Healthcheck))
|
|
}
|
|
}
|
|
|
|
|
|
// ENTRYPOINT /usr/sbin/nginx
|
|
// ENTRYPOINT /usr/sbin/nginx
|
|
@@ -661,33 +421,15 @@ func healthcheck(req dispatchRequest) error {
|
|
// Handles command processing similar to CMD and RUN, only req.runConfig.Entrypoint
|
|
// Handles command processing similar to CMD and RUN, only req.runConfig.Entrypoint
|
|
// is initialized at newBuilder time instead of through argument parsing.
|
|
// is initialized at newBuilder time instead of through argument parsing.
|
|
//
|
|
//
|
|
-func entrypoint(req dispatchRequest) error {
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- runConfig := req.state.runConfig
|
|
|
|
- parsed := handleJSONArgs(req.args, req.attributes)
|
|
|
|
-
|
|
|
|
- switch {
|
|
|
|
- case req.attributes["json"]:
|
|
|
|
- // ENTRYPOINT ["echo", "hi"]
|
|
|
|
- runConfig.Entrypoint = strslice.StrSlice(parsed)
|
|
|
|
- case len(parsed) == 0:
|
|
|
|
- // ENTRYPOINT []
|
|
|
|
- runConfig.Entrypoint = nil
|
|
|
|
- default:
|
|
|
|
- // ENTRYPOINT echo hi
|
|
|
|
- runConfig.Entrypoint = strslice.StrSlice(append(getShell(runConfig, req.builder.platform), parsed[0]))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // when setting the entrypoint if a CMD was not explicitly set then
|
|
|
|
- // set the command to nil
|
|
|
|
- if !req.state.cmdSet {
|
|
|
|
|
|
+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 {
|
|
runConfig.Cmd = nil
|
|
runConfig.Cmd = nil
|
|
}
|
|
}
|
|
|
|
|
|
- return req.builder.commit(req.state, fmt.Sprintf("ENTRYPOINT %q", runConfig.Entrypoint))
|
|
|
|
|
|
+ return d.builder.commit(d.state, fmt.Sprintf("ENTRYPOINT %q", runConfig.Entrypoint))
|
|
}
|
|
}
|
|
|
|
|
|
// EXPOSE 6667/tcp 7000/tcp
|
|
// EXPOSE 6667/tcp 7000/tcp
|
|
@@ -695,41 +437,33 @@ func entrypoint(req dispatchRequest) error {
|
|
// Expose ports for links and port mappings. This all ends up in
|
|
// Expose ports for links and port mappings. This all ends up in
|
|
// req.runConfig.ExposedPorts for runconfig.
|
|
// req.runConfig.ExposedPorts for runconfig.
|
|
//
|
|
//
|
|
-func expose(req dispatchRequest) error {
|
|
|
|
- portsTab := req.args
|
|
|
|
-
|
|
|
|
- if len(req.args) == 0 {
|
|
|
|
- return errAtLeastOneArgument("EXPOSE")
|
|
|
|
|
|
+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...)
|
|
}
|
|
}
|
|
|
|
+ c.Ports = ports
|
|
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
|
|
+ ps, _, err := nat.ParsePortSpecs(ports)
|
|
|
|
+ if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- runConfig := req.state.runConfig
|
|
|
|
- if runConfig.ExposedPorts == nil {
|
|
|
|
- runConfig.ExposedPorts = make(nat.PortSet)
|
|
|
|
|
|
+ if d.state.runConfig.ExposedPorts == nil {
|
|
|
|
+ d.state.runConfig.ExposedPorts = make(nat.PortSet)
|
|
}
|
|
}
|
|
-
|
|
|
|
- ports, _, err := nat.ParsePortSpecs(portsTab)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
|
|
+ for p := range ps {
|
|
|
|
+ d.state.runConfig.ExposedPorts[p] = struct{}{}
|
|
}
|
|
}
|
|
|
|
|
|
- // instead of using ports directly, we build a list of ports and sort it so
|
|
|
|
- // the order is consistent. This prevents cache burst where map ordering
|
|
|
|
- // changes between builds
|
|
|
|
- portList := make([]string, len(ports))
|
|
|
|
- var i int
|
|
|
|
- for port := range ports {
|
|
|
|
- if _, exists := runConfig.ExposedPorts[port]; !exists {
|
|
|
|
- runConfig.ExposedPorts[port] = struct{}{}
|
|
|
|
- }
|
|
|
|
- portList[i] = string(port)
|
|
|
|
- i++
|
|
|
|
- }
|
|
|
|
- sort.Strings(portList)
|
|
|
|
- return req.builder.commit(req.state, "EXPOSE "+strings.Join(portList, " "))
|
|
|
|
|
|
+ return d.builder.commit(d.state, "EXPOSE "+strings.Join(c.Ports, " "))
|
|
}
|
|
}
|
|
|
|
|
|
// USER foo
|
|
// USER foo
|
|
@@ -737,62 +471,39 @@ func expose(req dispatchRequest) error {
|
|
// Set the user to 'foo' for future commands and when running the
|
|
// Set the user to 'foo' for future commands and when running the
|
|
// ENTRYPOINT/CMD at container run time.
|
|
// ENTRYPOINT/CMD at container run time.
|
|
//
|
|
//
|
|
-func user(req dispatchRequest) error {
|
|
|
|
- if len(req.args) != 1 {
|
|
|
|
- return errExactlyOneArgument("USER")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- req.state.runConfig.User = req.args[0]
|
|
|
|
- return req.builder.commit(req.state, fmt.Sprintf("USER %v", req.args))
|
|
|
|
|
|
+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))
|
|
}
|
|
}
|
|
|
|
|
|
// VOLUME /foo
|
|
// VOLUME /foo
|
|
//
|
|
//
|
|
// Expose the volume /foo for use. Will also accept the JSON array form.
|
|
// Expose the volume /foo for use. Will also accept the JSON array form.
|
|
//
|
|
//
|
|
-func volume(req dispatchRequest) error {
|
|
|
|
- if len(req.args) == 0 {
|
|
|
|
- return errAtLeastOneArgument("VOLUME")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
|
|
+func dispatchVolume(d dispatchRequest, c *instructions.VolumeCommand) error {
|
|
|
|
+ if d.state.runConfig.Volumes == nil {
|
|
|
|
+ d.state.runConfig.Volumes = map[string]struct{}{}
|
|
}
|
|
}
|
|
-
|
|
|
|
- runConfig := req.state.runConfig
|
|
|
|
- if runConfig.Volumes == nil {
|
|
|
|
- runConfig.Volumes = map[string]struct{}{}
|
|
|
|
- }
|
|
|
|
- for _, v := range req.args {
|
|
|
|
- v = strings.TrimSpace(v)
|
|
|
|
|
|
+ for _, v := range c.Volumes {
|
|
if v == "" {
|
|
if v == "" {
|
|
return errors.New("VOLUME specified can not be an empty string")
|
|
return errors.New("VOLUME specified can not be an empty string")
|
|
}
|
|
}
|
|
- runConfig.Volumes[v] = struct{}{}
|
|
|
|
|
|
+ d.state.runConfig.Volumes[v] = struct{}{}
|
|
}
|
|
}
|
|
- return req.builder.commit(req.state, fmt.Sprintf("VOLUME %v", req.args))
|
|
|
|
|
|
+ return d.builder.commit(d.state, fmt.Sprintf("VOLUME %v", c.Volumes))
|
|
}
|
|
}
|
|
|
|
|
|
// STOPSIGNAL signal
|
|
// STOPSIGNAL signal
|
|
//
|
|
//
|
|
// Set the signal that will be used to kill the container.
|
|
// Set the signal that will be used to kill the container.
|
|
-func stopSignal(req dispatchRequest) error {
|
|
|
|
- if len(req.args) != 1 {
|
|
|
|
- return errExactlyOneArgument("STOPSIGNAL")
|
|
|
|
- }
|
|
|
|
|
|
+func dispatchStopSignal(d dispatchRequest, c *instructions.StopSignalCommand) error {
|
|
|
|
|
|
- sig := req.args[0]
|
|
|
|
- _, err := signal.ParseSignal(sig)
|
|
|
|
|
|
+ _, err := signal.ParseSignal(c.Signal)
|
|
if err != nil {
|
|
if err != nil {
|
|
return validationError{err}
|
|
return validationError{err}
|
|
}
|
|
}
|
|
-
|
|
|
|
- req.state.runConfig.StopSignal = sig
|
|
|
|
- return req.builder.commit(req.state, fmt.Sprintf("STOPSIGNAL %v", req.args))
|
|
|
|
|
|
+ d.state.runConfig.StopSignal = c.Signal
|
|
|
|
+ return d.builder.commit(d.state, fmt.Sprintf("STOPSIGNAL %v", c.Signal))
|
|
}
|
|
}
|
|
|
|
|
|
// ARG name[=value]
|
|
// ARG name[=value]
|
|
@@ -800,89 +511,21 @@ func stopSignal(req dispatchRequest) error {
|
|
// Adds the variable foo to the trusted list of variables that can be passed
|
|
// Adds the variable foo to the trusted list of variables that can be passed
|
|
// to builder using the --build-arg flag for expansion/substitution or passing to 'run'.
|
|
// to builder using the --build-arg flag for expansion/substitution or passing to 'run'.
|
|
// Dockerfile author may optionally set a default value of this variable.
|
|
// Dockerfile author may optionally set a default value of this variable.
|
|
-func arg(req dispatchRequest) error {
|
|
|
|
- if len(req.args) != 1 {
|
|
|
|
- return errExactlyOneArgument("ARG")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- var (
|
|
|
|
- name string
|
|
|
|
- newValue string
|
|
|
|
- hasDefault bool
|
|
|
|
- )
|
|
|
|
-
|
|
|
|
- arg := req.args[0]
|
|
|
|
- // 'arg' can just be a name or name-value pair. Note that this is different
|
|
|
|
- // from 'env' that handles the split of name and value at the parser level.
|
|
|
|
- // The reason for doing it differently for 'arg' is that we support just
|
|
|
|
- // defining an arg and not assign it a value (while 'env' always expects a
|
|
|
|
- // name-value pair). If possible, it will be good to harmonize the two.
|
|
|
|
- if strings.Contains(arg, "=") {
|
|
|
|
- parts := strings.SplitN(arg, "=", 2)
|
|
|
|
- if len(parts[0]) == 0 {
|
|
|
|
- return errBlankCommandNames("ARG")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- name = parts[0]
|
|
|
|
- newValue = parts[1]
|
|
|
|
- hasDefault = true
|
|
|
|
- } else {
|
|
|
|
- name = arg
|
|
|
|
- hasDefault = false
|
|
|
|
- }
|
|
|
|
|
|
+func dispatchArg(d dispatchRequest, c *instructions.ArgCommand) error {
|
|
|
|
|
|
- var value *string
|
|
|
|
- if hasDefault {
|
|
|
|
- value = &newValue
|
|
|
|
|
|
+ commitStr := "ARG " + c.Key
|
|
|
|
+ if c.Value != nil {
|
|
|
|
+ commitStr += "=" + *c.Value
|
|
}
|
|
}
|
|
- req.builder.buildArgs.AddArg(name, value)
|
|
|
|
|
|
|
|
- // Arg before FROM doesn't add a layer
|
|
|
|
- if !req.state.hasFromImage() {
|
|
|
|
- req.builder.buildArgs.AddMetaArg(name, value)
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
- return req.builder.commit(req.state, "ARG "+arg)
|
|
|
|
|
|
+ d.state.buildArgs.AddArg(c.Key, c.Value)
|
|
|
|
+ return d.builder.commit(d.state, commitStr)
|
|
}
|
|
}
|
|
|
|
|
|
// SHELL powershell -command
|
|
// SHELL powershell -command
|
|
//
|
|
//
|
|
// Set the non-default shell to use.
|
|
// Set the non-default shell to use.
|
|
-func shell(req dispatchRequest) error {
|
|
|
|
- if err := req.flags.Parse(); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- shellSlice := handleJSONArgs(req.args, req.attributes)
|
|
|
|
- switch {
|
|
|
|
- case len(shellSlice) == 0:
|
|
|
|
- // SHELL []
|
|
|
|
- return errAtLeastOneArgument("SHELL")
|
|
|
|
- case req.attributes["json"]:
|
|
|
|
- // SHELL ["powershell", "-command"]
|
|
|
|
- req.state.runConfig.Shell = strslice.StrSlice(shellSlice)
|
|
|
|
- default:
|
|
|
|
- // SHELL powershell -command - not JSON
|
|
|
|
- return errNotJSON("SHELL", req.original)
|
|
|
|
- }
|
|
|
|
- return req.builder.commit(req.state, fmt.Sprintf("SHELL %v", shellSlice))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func errAtLeastOneArgument(command string) error {
|
|
|
|
- return fmt.Errorf("%s requires at least one argument", command)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func errExactlyOneArgument(command string) error {
|
|
|
|
- return fmt.Errorf("%s requires exactly one argument", command)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func errAtLeastTwoArguments(command string) error {
|
|
|
|
- return fmt.Errorf("%s requires at least two arguments", command)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func errBlankCommandNames(command string) error {
|
|
|
|
- return fmt.Errorf("%s names can not be blank", command)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func errTooManyArguments(command string) error {
|
|
|
|
- return fmt.Errorf("Bad input to %s, too many arguments", command)
|
|
|
|
|
|
+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))
|
|
}
|
|
}
|