2014-08-15 16:29:35 +00:00
|
|
|
package builder
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2014-08-07 05:56:44 +00: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 20:17:40 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-04 06:21:51 +00:00
|
|
|
"io/ioutil"
|
2014-08-05 22:41:09 +00:00
|
|
|
"path/filepath"
|
2014-08-05 20:17:40 +00:00
|
|
|
"strings"
|
2014-08-05 22:41:09 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/nat"
|
2014-08-13 10:07:41 +00:00
|
|
|
"github.com/docker/docker/pkg/log"
|
2014-09-04 06:21:51 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2014-08-05 22:41:09 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
2014-08-05 20:17:40 +00:00
|
|
|
)
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// dispatch with no layer / parsing. This is effectively not a command.
|
2014-10-13 20:14:35 +00:00
|
|
|
func nullDispatch(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00: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 20:14:35 +00:00
|
|
|
func env(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 20:17:40 +00:00
|
|
|
if len(args) != 2 {
|
|
|
|
return fmt.Errorf("ENV accepts two arguments")
|
|
|
|
}
|
|
|
|
|
2014-08-13 10:07:41 +00:00
|
|
|
fullEnv := fmt.Sprintf("%s=%s", args[0], args[1])
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2014-08-13 10:07:41 +00:00
|
|
|
for i, envVar := range b.Config.Env {
|
|
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
|
|
|
if args[0] == envParts[0] {
|
|
|
|
b.Config.Env[i] = fullEnv
|
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("ENV %s", fullEnv))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.Config.Env = append(b.Config.Env, fullEnv)
|
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("ENV %s", fullEnv))
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// MAINTAINER some text <maybe@an.email.address>
|
|
|
|
//
|
|
|
|
// Sets the maintainer metadata.
|
2014-10-13 20:14:35 +00:00
|
|
|
func maintainer(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 20:17:40 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("MAINTAINER requires only one argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.maintainer = args[0]
|
2014-08-11 15:44:31 +00:00
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("MAINTAINER %s", b.maintainer))
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00: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 20:14:35 +00:00
|
|
|
func add(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-16 16:58:20 +00:00
|
|
|
if len(args) < 2 {
|
|
|
|
return fmt.Errorf("ADD requires at least two arguments")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.runContextCommand(args, true, true, "ADD")
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// COPY foo /path
|
|
|
|
//
|
|
|
|
// Same as 'ADD' but without the tar and remote url handling.
|
|
|
|
//
|
2014-10-13 20:14:35 +00:00
|
|
|
func dispatchCopy(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-16 16:58:20 +00:00
|
|
|
if len(args) < 2 {
|
|
|
|
return fmt.Errorf("COPY requires at least two arguments")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.runContextCommand(args, false, false, "COPY")
|
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// FROM imagename
|
|
|
|
//
|
|
|
|
// This sets the image the dockerfile will build on top of.
|
|
|
|
//
|
2014-10-13 20:14:35 +00:00
|
|
|
func from(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("FROM requires one argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
name := args[0]
|
|
|
|
|
2014-08-26 19:25:44 +00:00
|
|
|
image, err := b.Daemon.Repositories().LookupImage(name)
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
2014-08-26 19:25:44 +00:00
|
|
|
if b.Daemon.Graph().IsNotExist(err) {
|
2014-08-05 22:41:09 +00:00
|
|
|
image, err = b.pullImage(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that the top level err will still be !nil here if IsNotExist is
|
|
|
|
// not the error. This approach just simplifies hte logic a bit.
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.processImageFrom(image)
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00: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 20:14:35 +00:00
|
|
|
func onbuild(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00: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-13 20:14:35 +00:00
|
|
|
original = strings.TrimSpace(strings.TrimLeft(original, "ONBUILD"))
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-10-13 20:14:35 +00:00
|
|
|
b.Config.OnBuild = append(b.Config.OnBuild, original)
|
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("ONBUILD %s", original))
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// WORKDIR /tmp
|
|
|
|
//
|
|
|
|
// Set the working directory for future RUN/CMD/etc statements.
|
|
|
|
//
|
2014-10-13 20:14:35 +00:00
|
|
|
func workdir(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("WORKDIR requires exactly one argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
workdir := args[0]
|
|
|
|
|
|
|
|
if workdir[0] == '/' {
|
2014-08-11 15:44:31 +00:00
|
|
|
b.Config.WorkingDir = workdir
|
2014-08-05 22:41:09 +00:00
|
|
|
} else {
|
2014-08-11 15:44:31 +00:00
|
|
|
if b.Config.WorkingDir == "" {
|
|
|
|
b.Config.WorkingDir = "/"
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-08-11 15:44:31 +00:00
|
|
|
b.Config.WorkingDir = filepath.Join(b.Config.WorkingDir, workdir)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 15:44:31 +00:00
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("WORKDIR %v", workdir))
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// RUN some command yo
|
|
|
|
//
|
|
|
|
// run a command and commit the image. Args are automatically prepended with
|
|
|
|
// 'sh -c' in the event there is only one argument. The difference in
|
|
|
|
// processing:
|
|
|
|
//
|
|
|
|
// RUN echo hi # sh -c echo hi
|
|
|
|
// RUN [ "echo", "hi" ] # echo hi
|
|
|
|
//
|
2014-10-13 20:14:35 +00:00
|
|
|
func run(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-11 12:58:50 +00:00
|
|
|
if b.image == "" {
|
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to run")
|
|
|
|
}
|
|
|
|
|
2014-08-13 10:07:41 +00:00
|
|
|
args = handleJsonArgs(args, attributes)
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-08-30 11:34:09 +00:00
|
|
|
if len(args) == 1 {
|
|
|
|
args = append([]string{"/bin/sh", "-c"}, args[0])
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:21:51 +00:00
|
|
|
runCmd := flag.NewFlagSet("run", flag.ContinueOnError)
|
|
|
|
runCmd.SetOutput(ioutil.Discard)
|
|
|
|
runCmd.Usage = nil
|
2014-08-30 11:34:09 +00:00
|
|
|
|
2014-09-04 06:21:51 +00:00
|
|
|
config, _, _, err := runconfig.Parse(runCmd, append([]string{b.image}, args...), nil)
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-11 15:44:31 +00:00
|
|
|
cmd := b.Config.Cmd
|
2014-08-05 22:41:09 +00:00
|
|
|
// set Cmd manually, this is special case only for Dockerfiles
|
2014-08-11 15:44:31 +00:00
|
|
|
b.Config.Cmd = config.Cmd
|
|
|
|
runconfig.Merge(b.Config, config)
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-08-11 15:44:31 +00:00
|
|
|
defer func(cmd []string) { b.Config.Cmd = cmd }(cmd)
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-08-13 10:07:41 +00:00
|
|
|
log.Debugf("Command to be executed: %v", b.Config.Cmd)
|
2014-08-05 22:41:09 +00: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 10:07:41 +00:00
|
|
|
|
2014-08-05 22:41:09 +00: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 05:56:44 +00: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 20:14:35 +00:00
|
|
|
func cmd(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-13 10:07:41 +00:00
|
|
|
b.Config.Cmd = handleJsonArgs(args, attributes)
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-08-30 11:34:09 +00:00
|
|
|
if !attributes["json"] && len(b.Config.Entrypoint) == 0 {
|
2014-09-29 17:23:10 +00:00
|
|
|
b.Config.Cmd = append([]string{"/bin/sh", "-c"}, b.Config.Cmd...)
|
2014-08-30 11:34:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 22:21:49 +00:00
|
|
|
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("CMD %v", b.Config.Cmd)); err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-30 11:34:09 +00:00
|
|
|
if len(args) != 0 {
|
|
|
|
b.cmdSet = true
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// ENTRYPOINT /usr/sbin/nginx
|
|
|
|
//
|
|
|
|
// Set the entrypoint (which defaults to sh -c) to /usr/sbin/nginx. Will
|
|
|
|
// accept the CMD as the arguments to /usr/sbin/nginx.
|
|
|
|
//
|
2014-08-11 15:44:31 +00:00
|
|
|
// Handles command processing similar to CMD and RUN, only b.Config.Entrypoint
|
2014-08-07 05:56:44 +00:00
|
|
|
// is initialized at NewBuilder time instead of through argument parsing.
|
|
|
|
//
|
2014-10-13 20:14:35 +00:00
|
|
|
func entrypoint(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-10-07 22:39:50 +00:00
|
|
|
parsed := handleJsonArgs(args, attributes)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(parsed) == 0:
|
|
|
|
// ENTYRPOINT []
|
|
|
|
b.Config.Entrypoint = nil
|
|
|
|
case attributes["json"]:
|
|
|
|
// ENTRYPOINT ["echo", "hi"]
|
|
|
|
b.Config.Entrypoint = parsed
|
|
|
|
default:
|
|
|
|
// ENTYRPOINT echo hi
|
|
|
|
b.Config.Entrypoint = []string{"/bin/sh", "-c", parsed[0]}
|
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-10-07 22:39:50 +00:00
|
|
|
// when setting the entrypoint if a CMD was not explicitly set then
|
|
|
|
// set the command to nil
|
|
|
|
if !b.cmdSet {
|
2014-08-11 15:44:31 +00:00
|
|
|
b.Config.Cmd = nil
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-08-13 10:07:41 +00:00
|
|
|
|
|
|
|
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("ENTRYPOINT %v", b.Config.Entrypoint)); err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-10-07 22:39:50 +00:00
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// EXPOSE 6667/tcp 7000/tcp
|
|
|
|
//
|
|
|
|
// Expose ports for links and port mappings. This all ends up in
|
2014-08-11 15:44:31 +00:00
|
|
|
// b.Config.ExposedPorts for runconfig.
|
2014-08-07 05:56:44 +00:00
|
|
|
//
|
2014-10-13 20:14:35 +00:00
|
|
|
func expose(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
portsTab := args
|
|
|
|
|
2014-08-11 15:44:31 +00:00
|
|
|
if b.Config.ExposedPorts == nil {
|
|
|
|
b.Config.ExposedPorts = make(nat.PortSet)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 15:44:31 +00:00
|
|
|
ports, _, err := nat.ParsePortSpecs(append(portsTab, b.Config.PortSpecs...))
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for port := range ports {
|
2014-08-11 15:44:31 +00:00
|
|
|
if _, exists := b.Config.ExposedPorts[port]; !exists {
|
|
|
|
b.Config.ExposedPorts[port] = struct{}{}
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 15:44:31 +00:00
|
|
|
b.Config.PortSpecs = nil
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-08-11 15:44:31 +00:00
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("EXPOSE %v", ports))
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// USER foo
|
|
|
|
//
|
|
|
|
// Set the user to 'foo' for future commands and when running the
|
|
|
|
// ENTRYPOINT/CMD at container run time.
|
|
|
|
//
|
2014-10-13 20:14:35 +00:00
|
|
|
func user(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("USER requires exactly one argument")
|
|
|
|
}
|
|
|
|
|
2014-08-11 15:44:31 +00:00
|
|
|
b.Config.User = args[0]
|
|
|
|
return b.commit("", b.Config.Cmd, fmt.Sprintf("USER %v", args))
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// VOLUME /foo
|
|
|
|
//
|
2014-09-11 13:27:51 +00:00
|
|
|
// Expose the volume /foo for use. Will also accept the JSON array form.
|
2014-08-07 05:56:44 +00:00
|
|
|
//
|
2014-10-13 20:14:35 +00:00
|
|
|
func volume(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-11 13:27:51 +00:00
|
|
|
if len(args) == 0 {
|
2014-08-05 22:41:09 +00:00
|
|
|
return fmt.Errorf("Volume cannot be empty")
|
|
|
|
}
|
|
|
|
|
2014-08-11 15:44:31 +00:00
|
|
|
if b.Config.Volumes == nil {
|
|
|
|
b.Config.Volumes = map[string]struct{}{}
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-09-11 13:27:51 +00:00
|
|
|
for _, v := range args {
|
2014-08-11 15:44:31 +00:00
|
|
|
b.Config.Volumes[v] = struct{}{}
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-09-11 13:27:51 +00:00
|
|
|
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("VOLUME %v", args)); err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// INSERT is no longer accepted, but we still parse it.
|
2014-10-13 20:14:35 +00:00
|
|
|
func insert(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
return fmt.Errorf("INSERT has been deprecated. Please use ADD instead")
|
|
|
|
}
|