123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- package builder
- // 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.
- import (
- "fmt"
- "io/ioutil"
- "path/filepath"
- "strings"
- "github.com/docker/docker/nat"
- "github.com/docker/docker/pkg/log"
- flag "github.com/docker/docker/pkg/mflag"
- "github.com/docker/docker/runconfig"
- )
- // dispatch with no layer / parsing. This is effectively not a command.
- func nullDispatch(b *Builder, args []string, attributes map[string]bool) error {
- return nil
- }
- // ENV foo bar
- //
- // Sets the environment variable foo to bar, also makes interpolation
- // in the dockerfile available from the next statement on via ${foo}.
- //
- func env(b *Builder, args []string, attributes map[string]bool) error {
- if len(args) != 2 {
- return fmt.Errorf("ENV accepts two arguments")
- }
- fullEnv := fmt.Sprintf("%s=%s", args[0], args[1])
- 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))
- }
- // MAINTAINER some text <maybe@an.email.address>
- //
- // Sets the maintainer metadata.
- func maintainer(b *Builder, args []string, attributes map[string]bool) error {
- if len(args) != 1 {
- return fmt.Errorf("MAINTAINER requires only one argument")
- }
- b.maintainer = args[0]
- return b.commit("", b.Config.Cmd, fmt.Sprintf("MAINTAINER %s", b.maintainer))
- }
- // 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.
- //
- func add(b *Builder, args []string, attributes map[string]bool) error {
- if len(args) != 2 {
- return fmt.Errorf("ADD requires two arguments")
- }
- return b.runContextCommand(args, true, true, "ADD")
- }
- // COPY foo /path
- //
- // Same as 'ADD' but without the tar and remote url handling.
- //
- func dispatchCopy(b *Builder, args []string, attributes map[string]bool) error {
- if len(args) != 2 {
- return fmt.Errorf("COPY requires two arguments")
- }
- return b.runContextCommand(args, false, false, "COPY")
- }
- // FROM imagename
- //
- // This sets the image the dockerfile will build on top of.
- //
- func from(b *Builder, args []string, attributes map[string]bool) error {
- if len(args) != 1 {
- return fmt.Errorf("FROM requires one argument")
- }
- name := args[0]
- image, err := b.Daemon.Repositories().LookupImage(name)
- if err != nil {
- if b.Daemon.Graph().IsNotExist(err) {
- 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)
- }
- // 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(b *Builder, args []string, attributes map[string]bool) error {
- 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)
- }
- trigger := strings.Join(args, " ")
- b.Config.OnBuild = append(b.Config.OnBuild, trigger)
- return b.commit("", b.Config.Cmd, fmt.Sprintf("ONBUILD %s", trigger))
- }
- // WORKDIR /tmp
- //
- // Set the working directory for future RUN/CMD/etc statements.
- //
- func workdir(b *Builder, args []string, attributes map[string]bool) error {
- if len(args) != 1 {
- return fmt.Errorf("WORKDIR requires exactly one argument")
- }
- workdir := args[0]
- if workdir[0] == '/' {
- b.Config.WorkingDir = workdir
- } else {
- if b.Config.WorkingDir == "" {
- b.Config.WorkingDir = "/"
- }
- b.Config.WorkingDir = filepath.Join(b.Config.WorkingDir, workdir)
- }
- return b.commit("", b.Config.Cmd, fmt.Sprintf("WORKDIR %v", workdir))
- }
- // 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
- //
- func run(b *Builder, args []string, attributes map[string]bool) error {
- if b.image == "" {
- return fmt.Errorf("Please provide a source image with `from` prior to run")
- }
- args = handleJsonArgs(args, attributes)
- if len(args) == 1 {
- args = append([]string{"/bin/sh", "-c"}, args[0])
- }
- runCmd := flag.NewFlagSet("run", flag.ContinueOnError)
- runCmd.SetOutput(ioutil.Discard)
- runCmd.Usage = nil
- config, _, _, err := runconfig.Parse(runCmd, append([]string{b.image}, args...), nil)
- if err != nil {
- return err
- }
- cmd := b.Config.Cmd
- // set Cmd manually, this is special case only for Dockerfiles
- b.Config.Cmd = config.Cmd
- runconfig.Merge(b.Config, config)
- defer func(cmd []string) { b.Config.Cmd = cmd }(cmd)
- log.Debugf("Command to be executed: %v", b.Config.Cmd)
- hit, err := b.probeCache()
- if err != nil {
- return err
- }
- if hit {
- return nil
- }
- c, err := b.create()
- if err != nil {
- return err
- }
- // 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
- }
- // CMD foo
- //
- // Set the default command to run in the container (which may be empty).
- // Argument handling is the same as RUN.
- //
- func cmd(b *Builder, args []string, attributes map[string]bool) error {
- b.Config.Cmd = handleJsonArgs(args, attributes)
- if !attributes["json"] && len(b.Config.Entrypoint) == 0 {
- b.Config.Entrypoint = []string{"/bin/sh", "-c"}
- }
- if err := b.commit("", b.Config.Cmd, fmt.Sprintf("CMD %v", b.Config.Cmd)); err != nil {
- return err
- }
- if len(args) != 0 {
- b.cmdSet = true
- }
- return nil
- }
- // 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.
- //
- // Handles command processing similar to CMD and RUN, only b.Config.Entrypoint
- // is initialized at NewBuilder time instead of through argument parsing.
- //
- func entrypoint(b *Builder, args []string, attributes map[string]bool) error {
- b.Config.Entrypoint = handleJsonArgs(args, attributes)
- if len(b.Config.Entrypoint) == 0 && len(b.Config.Cmd) == 0 {
- b.Config.Entrypoint = []string{"/bin/sh", "-c"}
- } else if !b.cmdSet {
- b.Config.Cmd = nil
- }
- if err := b.commit("", b.Config.Cmd, fmt.Sprintf("ENTRYPOINT %v", b.Config.Entrypoint)); err != nil {
- return err
- }
- return nil
- }
- // EXPOSE 6667/tcp 7000/tcp
- //
- // Expose ports for links and port mappings. This all ends up in
- // b.Config.ExposedPorts for runconfig.
- //
- func expose(b *Builder, args []string, attributes map[string]bool) error {
- portsTab := args
- if b.Config.ExposedPorts == nil {
- b.Config.ExposedPorts = make(nat.PortSet)
- }
- ports, _, err := nat.ParsePortSpecs(append(portsTab, b.Config.PortSpecs...))
- if err != nil {
- return err
- }
- for port := range ports {
- if _, exists := b.Config.ExposedPorts[port]; !exists {
- b.Config.ExposedPorts[port] = struct{}{}
- }
- }
- b.Config.PortSpecs = nil
- return b.commit("", b.Config.Cmd, fmt.Sprintf("EXPOSE %v", ports))
- }
- // USER foo
- //
- // Set the user to 'foo' for future commands and when running the
- // ENTRYPOINT/CMD at container run time.
- //
- func user(b *Builder, args []string, attributes map[string]bool) error {
- if len(args) != 1 {
- return fmt.Errorf("USER requires exactly one argument")
- }
- b.Config.User = args[0]
- return b.commit("", b.Config.Cmd, fmt.Sprintf("USER %v", args))
- }
- // VOLUME /foo
- //
- // Expose the volume /foo for use. Will also accept the JSON form, but either
- // way requires exactly one argument.
- //
- func volume(b *Builder, args []string, attributes map[string]bool) error {
- if len(args) != 1 {
- return fmt.Errorf("Volume cannot be empty")
- }
- volume := args
- if b.Config.Volumes == nil {
- b.Config.Volumes = map[string]struct{}{}
- }
- for _, v := range volume {
- b.Config.Volumes[v] = struct{}{}
- }
- if err := b.commit("", b.Config.Cmd, fmt.Sprintf("VOLUME %s", args)); err != nil {
- return err
- }
- return nil
- }
- // INSERT is no longer accepted, but we still parse it.
- func insert(b *Builder, args []string, attributes map[string]bool) error {
- return fmt.Errorf("INSERT has been deprecated. Please use ADD instead")
- }
|