a1522ec0 |
package builder |
22c46af4 |
|
3f5f6b03 |
// 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.
|
22c46af4 |
import (
"fmt" |
9aa71549 |
"io/ioutil" |
3c177dc8 |
"path" |
d6c0bbc3 |
"path/filepath" |
a34831f0 |
"regexp" |
4d2f6fbd |
"runtime" |
87d0562c |
"sort" |
22c46af4 |
"strings" |
d6c0bbc3 |
|
6f4d8470 |
"github.com/Sirupsen/logrus" |
9aa71549 |
flag "github.com/docker/docker/pkg/mflag" |
9c2374d1 |
"github.com/docker/docker/pkg/nat" |
d6c0bbc3 |
"github.com/docker/docker/runconfig" |
22c46af4 |
)
|
89367899 |
const (
// NoBaseImageSpecifier is the symbol used by the FROM
// command to specify that no base image is to be used.
NoBaseImageSpecifier string = "scratch"
)
|
3f5f6b03 |
// dispatch with no layer / parsing. This is effectively not a command. |
8c4a282a |
func nullDispatch(b *builder, args []string, attributes map[string]bool, original string) error { |
d6c0bbc3 |
return nil
}
|
3f5f6b03 |
// ENV foo bar
//
// Sets the environment variable foo to bar, also makes interpolation
// in the dockerfile available from the next statement on via ${foo}.
// |
8c4a282a |
func env(b *builder, args []string, attributes map[string]bool, original string) error { |
1314e158 |
if len(args) == 0 { |
e4f02abb |
return fmt.Errorf("ENV requires at least one argument") |
22c46af4 |
}
|
1314e158 |
if len(args)%2 != 0 {
// should never get here, but just in case
return fmt.Errorf("Bad input to ENV, too many args")
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
a8e871b0 |
// 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)
*/
|
1314e158 |
commitStr := "ENV"
for j := 0; j < len(args); j++ {
// name ==> args[j]
// value ==> args[j+1]
newVar := args[j] + "=" + args[j+1] + ""
commitStr += " " + newVar |
22c46af4 |
|
1314e158 |
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
} |
cb51681a |
} |
1314e158 |
if !gotOne {
b.Config.Env = append(b.Config.Env, newVar)
}
j++ |
cb51681a |
} |
1314e158 |
return b.commit("", b.Config.Cmd, commitStr) |
22c46af4 |
}
|
3f5f6b03 |
// MAINTAINER some text <maybe@an.email.address>
//
// Sets the maintainer metadata. |
8c4a282a |
func maintainer(b *builder, args []string, attributes map[string]bool, original string) error { |
22c46af4 |
if len(args) != 1 { |
e4f02abb |
return fmt.Errorf("MAINTAINER requires exactly one argument") |
22c46af4 |
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
22c46af4 |
b.maintainer = args[0] |
1ae4c00a |
return b.commit("", b.Config.Cmd, fmt.Sprintf("MAINTAINER %s", b.maintainer)) |
22c46af4 |
}
|
cdfdfbfb |
// LABEL some json data describing the image
//
// Sets the Label variable foo to bar,
// |
8c4a282a |
func label(b *builder, args []string, attributes map[string]bool, original string) error { |
cdfdfbfb |
if len(args) == 0 { |
6784a772 |
return fmt.Errorf("LABEL requires at least one argument") |
cdfdfbfb |
}
if len(args)%2 != 0 {
// should never get here, but just in case
return fmt.Errorf("Bad input to LABEL, too many args")
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
cdfdfbfb |
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)
}
|
3f5f6b03 |
// 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.
// |
8c4a282a |
func add(b *builder, args []string, attributes map[string]bool, original string) error { |
05b8a1eb |
if len(args) < 2 {
return fmt.Errorf("ADD requires at least two arguments") |
22c46af4 |
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
22c46af4 |
return b.runContextCommand(args, true, true, "ADD")
}
|
3f5f6b03 |
// COPY foo /path
//
// Same as 'ADD' but without the tar and remote url handling.
// |
8c4a282a |
func dispatchCopy(b *builder, args []string, attributes map[string]bool, original string) error { |
05b8a1eb |
if len(args) < 2 {
return fmt.Errorf("COPY requires at least two arguments") |
22c46af4 |
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
22c46af4 |
return b.runContextCommand(args, false, false, "COPY")
} |
d6c0bbc3 |
|
3f5f6b03 |
// FROM imagename
//
// This sets the image the dockerfile will build on top of.
// |
8c4a282a |
func from(b *builder, args []string, attributes map[string]bool, original string) error { |
d6c0bbc3 |
if len(args) != 1 {
return fmt.Errorf("FROM requires one argument")
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
d6c0bbc3 |
name := args[0]
|
89367899 |
if name == NoBaseImageSpecifier {
b.image = ""
b.noBaseImage = true
return nil
}
|
2ef1dec7 |
image, err := b.Daemon.Repositories().LookupImage(name) |
054e57a6 |
if b.Pull {
image, err = b.pullImage(name)
if err != nil {
return err
}
} |
d6c0bbc3 |
if err != nil { |
013fb875 |
if b.Daemon.Graph().IsNotExist(err, name) { |
d6c0bbc3 |
image, err = b.pullImage(name)
}
// note that the top level err will still be !nil here if IsNotExist is |
bbdf045a |
// not the error. This approach just simplifies the logic a bit. |
d6c0bbc3 |
if err != nil {
return err
}
}
return b.processImageFrom(image)
}
|
3f5f6b03 |
// 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.
// |
8c4a282a |
func onbuild(b *builder, args []string, attributes map[string]bool, original string) error { |
e4f02abb |
if len(args) == 0 {
return fmt.Errorf("ONBUILD requires at least one argument")
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
d6c0bbc3 |
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)
}
|
a34831f0 |
original = regexp.MustCompile(`(?i)^\s*ONBUILD\s*`).ReplaceAllString(original, "") |
d6c0bbc3 |
|
1150c163 |
b.Config.OnBuild = append(b.Config.OnBuild, original)
return b.commit("", b.Config.Cmd, fmt.Sprintf("ONBUILD %s", original)) |
d6c0bbc3 |
}
|
3f5f6b03 |
// WORKDIR /tmp
//
// Set the working directory for future RUN/CMD/etc statements.
// |
8c4a282a |
func workdir(b *builder, args []string, attributes map[string]bool, original string) error { |
d6c0bbc3 |
if len(args) != 1 {
return fmt.Errorf("WORKDIR requires exactly one argument")
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
3c177dc8 |
// 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 `\` |
d6c0bbc3 |
workdir := args[0]
|
3c177dc8 |
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) |
d6c0bbc3 |
}
|
3c177dc8 |
// Convert to platform specific format
if runtime.GOOS == "windows" {
workdir = strings.Replace(workdir, "/", "\\", -1)
} |
f21f9f85 |
b.Config.WorkingDir = workdir
|
1ae4c00a |
return b.commit("", b.Config.Cmd, fmt.Sprintf("WORKDIR %v", workdir)) |
d6c0bbc3 |
}
|
3f5f6b03 |
// RUN some command yo
//
// run a command and commit the image. Args are automatically prepended with |
4d2f6fbd |
// 'sh -c' under linux or 'cmd /S /C' under Windows, in the event there is
// only one argument. The difference in processing: |
3f5f6b03 |
// |
4d2f6fbd |
// RUN echo hi # sh -c echo hi (Linux)
// RUN echo hi # cmd /S /C echo hi (Windows) |
3f5f6b03 |
// RUN [ "echo", "hi" ] # echo hi
// |
8c4a282a |
func run(b *builder, args []string, attributes map[string]bool, original string) error { |
89367899 |
if b.image == "" && !b.noBaseImage { |
91bed436 |
return fmt.Errorf("Please provide a source image with `from` prior to run")
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
8c4a282a |
args = handleJSONArgs(args, attributes) |
d6c0bbc3 |
|
645f8a32 |
if !attributes["json"] { |
4d2f6fbd |
if runtime.GOOS != "windows" {
args = append([]string{"/bin/sh", "-c"}, args...)
} else {
args = append([]string{"cmd", "/S /C"}, args...)
} |
ac107995 |
}
|
9aa71549 |
runCmd := flag.NewFlagSet("run", flag.ContinueOnError)
runCmd.SetOutput(ioutil.Discard)
runCmd.Usage = nil |
ac107995 |
|
e45b0f92 |
config, _, _, err := runconfig.Parse(runCmd, append([]string{b.image}, args...)) |
d6c0bbc3 |
if err != nil {
return err
}
|
1ae4c00a |
cmd := b.Config.Cmd |
d6c0bbc3 |
// set Cmd manually, this is special case only for Dockerfiles |
1ae4c00a |
b.Config.Cmd = config.Cmd
runconfig.Merge(b.Config, config) |
d6c0bbc3 |
|
767df67e |
defer func(cmd *runconfig.Command) { b.Config.Cmd = cmd }(cmd) |
d6c0bbc3 |
|
6f4d8470 |
logrus.Debugf("[BUILDER] Command to be executed: %v", b.Config.Cmd) |
d6c0bbc3 |
hit, err := b.probeCache()
if err != nil {
return err
}
if hit {
return nil
}
c, err := b.create()
if err != nil {
return err
} |
cb51681a |
|
d6c0bbc3 |
// 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
}
|
3f5f6b03 |
// CMD foo
//
// Set the default command to run in the container (which may be empty).
// Argument handling is the same as RUN.
// |
8c4a282a |
func cmd(b *builder, args []string, attributes map[string]bool, original string) error { |
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
8c4a282a |
cmdSlice := handleJSONArgs(args, attributes) |
d6c0bbc3 |
|
24545c18 |
if !attributes["json"] { |
4d2f6fbd |
if runtime.GOOS != "windows" {
cmdSlice = append([]string{"/bin/sh", "-c"}, cmdSlice...)
} else {
cmdSlice = append([]string{"cmd", "/S /C"}, cmdSlice...)
} |
ac107995 |
}
|
767df67e |
b.Config.Cmd = runconfig.NewCommand(cmdSlice...)
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("CMD %q", cmdSlice)); err != nil { |
d6c0bbc3 |
return err
}
|
ac107995 |
if len(args) != 0 {
b.cmdSet = true
}
|
d6c0bbc3 |
return nil
}
|
3f5f6b03 |
// ENTRYPOINT /usr/sbin/nginx
// |
4d2f6fbd |
// 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. |
3f5f6b03 |
// |
1ae4c00a |
// Handles command processing similar to CMD and RUN, only b.Config.Entrypoint |
3f5f6b03 |
// is initialized at NewBuilder time instead of through argument parsing.
// |
8c4a282a |
func entrypoint(b *builder, args []string, attributes map[string]bool, original string) error { |
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
8c4a282a |
parsed := handleJSONArgs(args, attributes) |
50fa9dff |
switch {
case attributes["json"]:
// ENTRYPOINT ["echo", "hi"] |
767df67e |
b.Config.Entrypoint = runconfig.NewEntrypoint(parsed...) |
cdd6e979 |
case len(parsed) == 0:
// ENTRYPOINT []
b.Config.Entrypoint = nil |
50fa9dff |
default: |
cdd6e979 |
// ENTRYPOINT echo hi |
4d2f6fbd |
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])
} |
50fa9dff |
} |
d6c0bbc3 |
|
50fa9dff |
// when setting the entrypoint if a CMD was not explicitly set then
// set the command to nil
if !b.cmdSet { |
1ae4c00a |
b.Config.Cmd = nil |
d6c0bbc3 |
} |
cb51681a |
|
88905793 |
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("ENTRYPOINT %q", b.Config.Entrypoint)); err != nil { |
d6c0bbc3 |
return err
} |
50fa9dff |
|
d6c0bbc3 |
return nil
}
|
3f5f6b03 |
// EXPOSE 6667/tcp 7000/tcp
//
// Expose ports for links and port mappings. This all ends up in |
1ae4c00a |
// b.Config.ExposedPorts for runconfig. |
3f5f6b03 |
// |
8c4a282a |
func expose(b *builder, args []string, attributes map[string]bool, original string) error { |
d6c0bbc3 |
portsTab := args
|
e4f02abb |
if len(args) == 0 {
return fmt.Errorf("EXPOSE requires at least one argument")
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
1ae4c00a |
if b.Config.ExposedPorts == nil {
b.Config.ExposedPorts = make(nat.PortSet) |
d6c0bbc3 |
}
|
15134a33 |
ports, _, err := nat.ParsePortSpecs(portsTab) |
d6c0bbc3 |
if err != nil {
return err
}
|
87d0562c |
// 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 |
d6c0bbc3 |
for port := range ports { |
1ae4c00a |
if _, exists := b.Config.ExposedPorts[port]; !exists {
b.Config.ExposedPorts[port] = struct{}{} |
d6c0bbc3 |
} |
87d0562c |
portList[i] = string(port)
i++ |
d6c0bbc3 |
} |
87d0562c |
sort.Strings(portList)
return b.commit("", b.Config.Cmd, fmt.Sprintf("EXPOSE %s", strings.Join(portList, " "))) |
d6c0bbc3 |
}
|
3f5f6b03 |
// USER foo
//
// Set the user to 'foo' for future commands and when running the
// ENTRYPOINT/CMD at container run time.
// |
8c4a282a |
func user(b *builder, args []string, attributes map[string]bool, original string) error { |
4d2f6fbd |
if runtime.GOOS == "windows" { |
8c4a282a |
return fmt.Errorf("USER is not supported on Windows") |
4d2f6fbd |
}
|
d6c0bbc3 |
if len(args) != 1 {
return fmt.Errorf("USER requires exactly one argument")
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
1ae4c00a |
b.Config.User = args[0]
return b.commit("", b.Config.Cmd, fmt.Sprintf("USER %v", args)) |
d6c0bbc3 |
}
|
3f5f6b03 |
// VOLUME /foo
// |
a5ca549a |
// Expose the volume /foo for use. Will also accept the JSON array form. |
3f5f6b03 |
// |
8c4a282a |
func volume(b *builder, args []string, attributes map[string]bool, original string) error { |
4d2f6fbd |
if runtime.GOOS == "windows" { |
8c4a282a |
return fmt.Errorf("VOLUME is not supported on Windows") |
4d2f6fbd |
} |
a5ca549a |
if len(args) == 0 { |
e4f02abb |
return fmt.Errorf("VOLUME requires at least one argument") |
d6c0bbc3 |
}
|
08b7f30f |
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
|
1ae4c00a |
if b.Config.Volumes == nil {
b.Config.Volumes = map[string]struct{}{} |
d6c0bbc3 |
} |
a5ca549a |
for _, v := range args { |
8071bf39 |
v = strings.TrimSpace(v)
if v == "" {
return fmt.Errorf("Volume specified can not be an empty string")
} |
1ae4c00a |
b.Config.Volumes[v] = struct{}{} |
d6c0bbc3 |
} |
a5ca549a |
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("VOLUME %v", args)); err != nil { |
d6c0bbc3 |
return err
}
return nil
} |