2014-08-15 12:29:35 -04:00
|
|
|
package builder
|
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 (
|
|
|
|
"fmt"
|
2014-09-04 02:21:51 -04:00
|
|
|
"io/ioutil"
|
2015-06-01 19:42:27 -04:00
|
|
|
"path"
|
2014-08-05 18:41:09 -04:00
|
|
|
"path/filepath"
|
2014-10-21 15:26:20 -04:00
|
|
|
"regexp"
|
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-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-08-05 18:41:09 -04:00
|
|
|
"github.com/docker/docker/nat"
|
2014-09-04 02:21:51 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2014-08-05 18:41:09 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
2014-08-05 16:17:40 -04:00
|
|
|
)
|
|
|
|
|
2014-10-28 17:06:23 -04:00
|
|
|
const (
|
|
|
|
// NoBaseImageSpecifier is the symbol used by the FROM
|
|
|
|
// command to specify that no base image is to be used.
|
|
|
|
NoBaseImageSpecifier string = "scratch"
|
|
|
|
)
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// dispatch with no layer / parsing. This is effectively not a command.
|
2014-10-13 16:14:35 -04:00
|
|
|
func nullDispatch(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 18:41:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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}.
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func env(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-25 22:28:24 -04:00
|
|
|
if len(args) == 0 {
|
2015-02-04 12:34:25 -05:00
|
|
|
return fmt.Errorf("ENV requires at least one argument")
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-09-25 22:28:24 -04:00
|
|
|
if len(args)%2 != 0 {
|
|
|
|
// should never get here, but just in case
|
|
|
|
return fmt.Errorf("Bad input to ENV, too many args")
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-27 10:57:34 -05:00
|
|
|
// TODO/FIXME/NOT USED
|
|
|
|
// Just here to show how to use the builder flags stuff within the
|
|
|
|
// context of a builder command. Will remove once we actually add
|
|
|
|
// a builder command to something!
|
|
|
|
/*
|
|
|
|
flBool1 := b.BuilderFlags.AddBool("bool1", false)
|
|
|
|
flStr1 := b.BuilderFlags.AddString("str1", "HI")
|
|
|
|
|
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Bool1:%v\n", flBool1)
|
|
|
|
fmt.Printf("Str1:%v\n", flStr1)
|
|
|
|
*/
|
|
|
|
|
2014-09-25 22:28:24 -04:00
|
|
|
commitStr := "ENV"
|
|
|
|
|
|
|
|
for j := 0; j < len(args); j++ {
|
|
|
|
// name ==> args[j]
|
|
|
|
// value ==> args[j+1]
|
|
|
|
newVar := args[j] + "=" + args[j+1] + ""
|
|
|
|
commitStr += " " + newVar
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-09-25 22:28:24 -04:00
|
|
|
gotOne := false
|
|
|
|
for i, envVar := range b.Config.Env {
|
|
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
|
|
|
if envParts[0] == args[j] {
|
|
|
|
b.Config.Env[i] = newVar
|
|
|
|
gotOne = true
|
|
|
|
break
|
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
}
|
2014-09-25 22:28:24 -04:00
|
|
|
if !gotOne {
|
|
|
|
b.Config.Env = append(b.Config.Env, newVar)
|
|
|
|
}
|
|
|
|
j++
|
2014-08-13 06:07:41 -04:00
|
|
|
}
|
2014-09-25 22:28:24 -04:00
|
|
|
|
|
|
|
return b.commit("", b.Config.Cmd, commitStr)
|
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.
|
2014-10-13 16:14:35 -04:00
|
|
|
func maintainer(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 16:17:40 -04:00
|
|
|
if len(args) != 1 {
|
2015-02-04 12:34:25 -05:00
|
|
|
return fmt.Errorf("MAINTAINER requires exactly one argument")
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
b.maintainer = args[0]
|
2014-08-11 11:44:31 -04:00
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("MAINTAINER %s", b.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,
|
|
|
|
//
|
|
|
|
func label(b *Builder, args []string, attributes map[string]bool, original string) error {
|
|
|
|
if len(args) == 0 {
|
2015-03-17 01:23:40 -04:00
|
|
|
return fmt.Errorf("LABEL requires at least one argument")
|
2015-02-17 10:20:06 -05:00
|
|
|
}
|
|
|
|
if len(args)%2 != 0 {
|
|
|
|
// should never get here, but just in case
|
|
|
|
return fmt.Errorf("Bad input to LABEL, too many args")
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-17 10:20:06 -05:00
|
|
|
commitStr := "LABEL"
|
|
|
|
|
|
|
|
if b.Config.Labels == nil {
|
|
|
|
b.Config.Labels = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < len(args); j++ {
|
|
|
|
// name ==> args[j]
|
|
|
|
// value ==> args[j+1]
|
|
|
|
newVar := args[j] + "=" + args[j+1] + ""
|
|
|
|
commitStr += " " + newVar
|
|
|
|
|
|
|
|
b.Config.Labels[args[j]] = args[j+1]
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
return b.commit("", b.Config.Cmd, commitStr)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func add(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-16 12:58:20 -04:00
|
|
|
if len(args) < 2 {
|
|
|
|
return fmt.Errorf("ADD requires at least two arguments")
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
return b.runContextCommand(args, true, true, "ADD")
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// COPY foo /path
|
|
|
|
//
|
|
|
|
// Same as 'ADD' but without the tar and remote url handling.
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func dispatchCopy(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-16 12:58:20 -04:00
|
|
|
if len(args) < 2 {
|
|
|
|
return fmt.Errorf("COPY requires at least two arguments")
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
return b.runContextCommand(args, false, false, "COPY")
|
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// FROM imagename
|
|
|
|
//
|
|
|
|
// This sets the image the dockerfile will build on top of.
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func from(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 18:41:09 -04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("FROM requires one argument")
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
name := args[0]
|
|
|
|
|
2014-10-28 17:06:23 -04:00
|
|
|
if name == NoBaseImageSpecifier {
|
|
|
|
b.image = ""
|
|
|
|
b.noBaseImage = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
image, err := b.Daemon.Repositories().LookupImage(name)
|
2014-11-21 12:51:32 -05:00
|
|
|
if b.Pull {
|
|
|
|
image, err = b.pullImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
if err != nil {
|
2015-03-27 21:07:20 -04:00
|
|
|
if b.Daemon.Graph().IsNotExist(err, name) {
|
2014-08-05 18:41:09 -04:00
|
|
|
image, err = b.pullImage(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that the top level err will still be !nil here if IsNotExist is
|
2015-04-09 14:43:05 -04:00
|
|
|
// not the error. This approach just simplifies the logic a bit.
|
2014-08-05 18:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.processImageFrom(image)
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// 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.
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func onbuild(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2015-02-04 12:34:25 -05:00
|
|
|
if len(args) == 0 {
|
|
|
|
return fmt.Errorf("ONBUILD requires at least one argument")
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
triggerInstruction := strings.ToUpper(strings.TrimSpace(args[0]))
|
|
|
|
switch triggerInstruction {
|
|
|
|
case "ONBUILD":
|
|
|
|
return fmt.Errorf("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
|
|
|
|
case "MAINTAINER", "FROM":
|
|
|
|
return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", triggerInstruction)
|
|
|
|
}
|
|
|
|
|
2014-10-21 15:26:20 -04:00
|
|
|
original = regexp.MustCompile(`(?i)^\s*ONBUILD\s*`).ReplaceAllString(original, "")
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2014-10-13 16:14:35 -04:00
|
|
|
b.Config.OnBuild = append(b.Config.OnBuild, original)
|
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("ONBUILD %s", original))
|
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.
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func workdir(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 18:41:09 -04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("WORKDIR requires exactly one argument")
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-01 19:42:27 -04:00
|
|
|
// Note that workdir passed comes from the Dockerfile. Hence it is in
|
|
|
|
// Linux format using forward-slashes, even on Windows. However,
|
|
|
|
// b.Config.WorkingDir is in platform-specific notation (in other words
|
|
|
|
// on Windows will use `\`
|
2014-08-05 18:41:09 -04:00
|
|
|
workdir := args[0]
|
|
|
|
|
2015-06-01 19:42:27 -04:00
|
|
|
isAbs := false
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Alternate processing for Windows here is necessary as we can't call
|
|
|
|
// filepath.IsAbs(workDir) as that would verify Windows style paths,
|
|
|
|
// along with drive-letters (eg c:\pathto\file.txt). We (arguably
|
|
|
|
// correctly or not) check for both forward and back slashes as this
|
|
|
|
// is what the 1.4.2 GoLang implementation of IsAbs() does in the
|
|
|
|
// isSlash() function.
|
|
|
|
isAbs = workdir[0] == '\\' || workdir[0] == '/'
|
|
|
|
} else {
|
|
|
|
isAbs = filepath.IsAbs(workdir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isAbs {
|
|
|
|
current := b.Config.WorkingDir
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Convert to Linux format before join
|
|
|
|
current = strings.Replace(current, "\\", "/", -1)
|
|
|
|
}
|
|
|
|
// Must use path.Join so works correctly on Windows, not filepath
|
|
|
|
workdir = path.Join("/", current, workdir)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 19:42:27 -04:00
|
|
|
// Convert to platform specific format
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
workdir = strings.Replace(workdir, "/", "\\", -1)
|
|
|
|
}
|
2014-12-12 13:32:11 -05:00
|
|
|
b.Config.WorkingDir = workdir
|
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("WORKDIR %v", workdir))
|
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
|
2015-05-05 11:39:37 -04:00
|
|
|
// '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
|
|
|
//
|
2015-05-05 11:39:37 -04:00
|
|
|
// RUN echo hi # sh -c echo hi (Linux)
|
|
|
|
// RUN echo hi # cmd /S /C echo hi (Windows)
|
2014-08-07 01:56:44 -04:00
|
|
|
// RUN [ "echo", "hi" ] # echo hi
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func run(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-10-28 17:06:23 -04:00
|
|
|
if b.image == "" && !b.noBaseImage {
|
2014-09-11 08:58:50 -04:00
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to run")
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-13 06:07:41 -04:00
|
|
|
args = handleJsonArgs(args, attributes)
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2015-03-09 17:44:14 -04:00
|
|
|
if !attributes["json"] {
|
2015-05-05 11:39:37 -04:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
args = append([]string{"/bin/sh", "-c"}, args...)
|
|
|
|
} else {
|
|
|
|
args = append([]string{"cmd", "/S /C"}, args...)
|
|
|
|
}
|
2014-08-30 07:34:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 02:21:51 -04:00
|
|
|
runCmd := flag.NewFlagSet("run", flag.ContinueOnError)
|
|
|
|
runCmd.SetOutput(ioutil.Discard)
|
|
|
|
runCmd.Usage = nil
|
2014-08-30 07:34:09 -04:00
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err := runconfig.Parse(runCmd, append([]string{b.image}, args...))
|
2014-08-05 18:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
cmd := b.Config.Cmd
|
2014-08-05 18:41:09 -04:00
|
|
|
// set Cmd manually, this is special case only for Dockerfiles
|
2014-08-11 11:44:31 -04:00
|
|
|
b.Config.Cmd = config.Cmd
|
|
|
|
runconfig.Merge(b.Config, config)
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2015-04-10 20:05:21 -04:00
|
|
|
defer func(cmd *runconfig.Command) { b.Config.Cmd = cmd }(cmd)
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("[BUILDER] Command to be executed: %v", b.Config.Cmd)
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := b.create()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
// Ensure that we keep the container mounted until the commit
|
|
|
|
// to avoid unmounting and then mounting directly again
|
|
|
|
c.Mount()
|
|
|
|
defer c.Unmount()
|
|
|
|
|
|
|
|
err = b.run(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.commit(c.ID, cmd, "run"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func cmd(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-10 20:05:21 -04:00
|
|
|
cmdSlice := handleJsonArgs(args, attributes)
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2014-10-27 17:15:28 -04:00
|
|
|
if !attributes["json"] {
|
2015-05-05 11:39:37 -04:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
cmdSlice = append([]string{"/bin/sh", "-c"}, cmdSlice...)
|
|
|
|
} else {
|
|
|
|
cmdSlice = append([]string{"cmd", "/S /C"}, cmdSlice...)
|
|
|
|
}
|
2014-08-30 07:34:09 -04:00
|
|
|
}
|
|
|
|
|
2015-04-10 20:05:21 -04:00
|
|
|
b.Config.Cmd = runconfig.NewCommand(cmdSlice...)
|
|
|
|
|
|
|
|
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("CMD %q", cmdSlice)); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-30 07:34:09 -04:00
|
|
|
if len(args) != 0 {
|
|
|
|
b.cmdSet = true
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// ENTRYPOINT /usr/sbin/nginx
|
|
|
|
//
|
2015-05-05 11:39:37 -04:00
|
|
|
// Set the entrypoint (which defaults to sh -c on linux, or cmd /S /C on Windows) to
|
|
|
|
// /usr/sbin/nginx. Will accept the CMD as the arguments to /usr/sbin/nginx.
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
2014-08-11 11:44:31 -04:00
|
|
|
// Handles command processing similar to CMD and RUN, only b.Config.Entrypoint
|
2014-08-07 01:56:44 -04:00
|
|
|
// is initialized at NewBuilder time instead of through argument parsing.
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func entrypoint(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-07 18:39:50 -04:00
|
|
|
parsed := handleJsonArgs(args, attributes)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case attributes["json"]:
|
|
|
|
// ENTRYPOINT ["echo", "hi"]
|
2015-04-10 20:05:21 -04:00
|
|
|
b.Config.Entrypoint = runconfig.NewEntrypoint(parsed...)
|
2014-10-23 20:23:25 -04:00
|
|
|
case len(parsed) == 0:
|
|
|
|
// ENTRYPOINT []
|
|
|
|
b.Config.Entrypoint = nil
|
2014-10-07 18:39:50 -04:00
|
|
|
default:
|
2014-10-23 20:23:25 -04:00
|
|
|
// ENTRYPOINT echo hi
|
2015-05-05 11:39:37 -04:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
b.Config.Entrypoint = runconfig.NewEntrypoint("/bin/sh", "-c", parsed[0])
|
|
|
|
} else {
|
|
|
|
b.Config.Entrypoint = runconfig.NewEntrypoint("cmd", "/S /C", parsed[0])
|
|
|
|
}
|
2014-10-07 18:39:50 -04:00
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2014-10-07 18:39:50 -04:00
|
|
|
// when setting the entrypoint if a CMD was not explicitly set then
|
|
|
|
// set the command to nil
|
|
|
|
if !b.cmdSet {
|
2014-08-11 11:44:31 -04:00
|
|
|
b.Config.Cmd = nil
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
|
2015-01-15 14:34:59 -05:00
|
|
|
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("ENTRYPOINT %q", b.Config.Entrypoint)); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-10-07 18:39:50 -04:00
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
2014-08-11 11:44:31 -04:00
|
|
|
// b.Config.ExposedPorts for runconfig.
|
2014-08-07 01:56:44 -04:00
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func expose(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 18:41:09 -04:00
|
|
|
portsTab := args
|
|
|
|
|
2015-02-04 12:34:25 -05:00
|
|
|
if len(args) == 0 {
|
|
|
|
return fmt.Errorf("EXPOSE requires at least one argument")
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
if b.Config.ExposedPorts == nil {
|
|
|
|
b.Config.ExposedPorts = make(nat.PortSet)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-05-24 09:17:29 -04:00
|
|
|
ports, _, err := nat.ParsePortSpecs(portsTab)
|
2014-08-05 18:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:22:08 -05:00
|
|
|
// 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
|
2014-08-05 18:41:09 -04:00
|
|
|
for port := range ports {
|
2014-08-11 11:44:31 -04:00
|
|
|
if _, exists := b.Config.ExposedPorts[port]; !exists {
|
|
|
|
b.Config.ExposedPorts[port] = struct{}{}
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2014-11-12 03:22:08 -05:00
|
|
|
portList[i] = string(port)
|
|
|
|
i++
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2014-11-12 03:22:08 -05:00
|
|
|
sort.Strings(portList)
|
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("EXPOSE %s", strings.Join(portList, " ")))
|
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.
|
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func user(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2015-05-05 11:39:37 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return fmt.Errorf("USER is not supported on Windows.")
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("USER requires exactly one argument")
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
b.Config.User = args[0]
|
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("USER %v", args))
|
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
|
|
|
//
|
2014-10-13 16:14:35 -04:00
|
|
|
func volume(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2015-05-05 11:39:37 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return fmt.Errorf("VOLUME is not supported on Windows.")
|
|
|
|
}
|
2014-09-11 09:27:51 -04:00
|
|
|
if len(args) == 0 {
|
2015-02-04 12:34:25 -05:00
|
|
|
return fmt.Errorf("VOLUME requires at least one argument")
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:42 -04:00
|
|
|
if err := b.BuilderFlags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
if b.Config.Volumes == nil {
|
|
|
|
b.Config.Volumes = map[string]struct{}{}
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2014-09-11 09:27:51 -04:00
|
|
|
for _, v := range args {
|
2015-03-21 00:39:49 -04:00
|
|
|
v = strings.TrimSpace(v)
|
|
|
|
if v == "" {
|
|
|
|
return fmt.Errorf("Volume specified can not be an empty string")
|
|
|
|
}
|
2014-08-11 11:44:31 -04:00
|
|
|
b.Config.Volumes[v] = struct{}{}
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2014-09-11 09:27:51 -04:00
|
|
|
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("VOLUME %v", args)); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|