2014-08-15 16:29:35 +00:00
|
|
|
// builder is the evaluation step in the Dockerfile parse/evaluate pipeline.
|
2014-08-07 05:56:44 +00:00
|
|
|
//
|
|
|
|
// It incorporates a dispatch table based on the parser.Node values (see the
|
|
|
|
// parser package for more information) that are yielded from the parser itself.
|
|
|
|
// Calling NewBuilder with the BuildOpts struct can be used to customize the
|
|
|
|
// experience for execution purposes only. Parsing is controlled in the parser
|
|
|
|
// package, and this division of resposibility should be respected.
|
|
|
|
//
|
|
|
|
// Please see the jump table targets for the actual invocations, most of which
|
|
|
|
// will call out to the functions in internals.go to deal with their tasks.
|
|
|
|
//
|
|
|
|
// ONBUILD is a special case, which is covered in the onbuild() func in
|
|
|
|
// dispatchers.go.
|
|
|
|
//
|
|
|
|
// The evaluator uses the concept of "steps", which are usually each processable
|
|
|
|
// line in the Dockerfile. Each step is numbered and certain actions are taken
|
|
|
|
// before and after each step, such as creating an image ID and removing temporary
|
|
|
|
// containers and images. Note that ONBUILD creates a kinda-sorta "sub run" which
|
|
|
|
// includes its own set of steps (usually only one of them).
|
2014-08-15 16:29:35 +00:00
|
|
|
package builder
|
2014-08-05 20:17:40 +00:00
|
|
|
|
|
|
|
import (
|
2014-08-05 22:41:09 +00:00
|
|
|
"errors"
|
2014-08-05 20:17:40 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-08-05 22:41:09 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2014-08-05 20:17:40 +00:00
|
|
|
"strings"
|
|
|
|
|
2014-10-24 17:12:35 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-08-05 22:41:09 +00:00
|
|
|
"github.com/docker/docker/builder/parser"
|
2014-08-05 20:17:40 +00:00
|
|
|
"github.com/docker/docker/daemon"
|
|
|
|
"github.com/docker/docker/engine"
|
2014-08-05 22:41:09 +00:00
|
|
|
"github.com/docker/docker/pkg/tarsum"
|
2014-08-05 20:17:40 +00:00
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-08-05 22:41:09 +00:00
|
|
|
ErrDockerfileEmpty = errors.New("Dockerfile cannot be empty")
|
2014-08-05 20:17:40 +00:00
|
|
|
)
|
|
|
|
|
2014-10-25 18:29:18 +00:00
|
|
|
// Environment variable interpolation will happen on these statements only.
|
|
|
|
var replaceEnvAllowed = map[string]struct{}{
|
|
|
|
"env": {},
|
|
|
|
"add": {},
|
|
|
|
"copy": {},
|
|
|
|
"workdir": {},
|
|
|
|
"expose": {},
|
|
|
|
"volume": {},
|
|
|
|
"user": {},
|
|
|
|
}
|
|
|
|
|
2014-10-13 20:14:35 +00:00
|
|
|
var evaluateTable map[string]func(*Builder, []string, map[string]bool, string) error
|
2014-08-05 22:41:09 +00:00
|
|
|
|
|
|
|
func init() {
|
2014-10-13 20:14:35 +00:00
|
|
|
evaluateTable = map[string]func(*Builder, []string, map[string]bool, string) error{
|
2014-10-07 03:24:43 +00:00
|
|
|
"env": env,
|
|
|
|
"maintainer": maintainer,
|
|
|
|
"add": add,
|
|
|
|
"copy": dispatchCopy, // copy() is a go builtin
|
|
|
|
"from": from,
|
|
|
|
"onbuild": onbuild,
|
|
|
|
"workdir": workdir,
|
|
|
|
"run": run,
|
|
|
|
"cmd": cmd,
|
|
|
|
"entrypoint": entrypoint,
|
|
|
|
"expose": expose,
|
|
|
|
"volume": volume,
|
|
|
|
"user": user,
|
|
|
|
"insert": insert,
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// internal struct, used to maintain configuration of the Dockerfile's
|
|
|
|
// processing as it evaluates the parsing result.
|
2014-08-26 19:25:44 +00:00
|
|
|
type Builder struct {
|
2014-08-07 05:56:44 +00:00
|
|
|
Daemon *daemon.Daemon
|
|
|
|
Engine *engine.Engine
|
|
|
|
|
|
|
|
// effectively stdio for the run. Because it is not stdio, I said
|
|
|
|
// "Effectively". Do not use stdio anywhere in this package for any reason.
|
|
|
|
OutStream io.Writer
|
|
|
|
ErrStream io.Writer
|
|
|
|
|
|
|
|
Verbose bool
|
|
|
|
UtilizeCache bool
|
|
|
|
|
|
|
|
// controls how images and containers are handled between steps.
|
|
|
|
Remove bool
|
|
|
|
ForceRemove bool
|
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
AuthConfig *registry.AuthConfig
|
|
|
|
AuthConfigFile *registry.ConfigFile
|
|
|
|
|
|
|
|
// Deprecated, original writer used for ImagePull. To be removed.
|
2014-08-05 20:17:40 +00:00
|
|
|
OutOld io.Writer
|
|
|
|
StreamFormatter *utils.StreamFormatter
|
2014-08-26 19:25:44 +00:00
|
|
|
|
|
|
|
Config *runconfig.Config // runconfig for cmd, run, entrypoint etc.
|
|
|
|
|
|
|
|
// both of these are controlled by the Remove and ForceRemove options in BuildOpts
|
|
|
|
TmpContainers map[string]struct{} // a map of containers used for removes
|
|
|
|
|
2014-09-11 01:56:20 +00:00
|
|
|
dockerfile *parser.Node // the syntax tree of the dockerfile
|
|
|
|
image string // image name for commit processing
|
|
|
|
maintainer string // maintainer name. could probably be removed.
|
|
|
|
cmdSet bool // indicates is CMD was set in current Dockerfile
|
|
|
|
context tarsum.TarSum // the context is a tarball that is uploaded by the client
|
|
|
|
contextPath string // the path of the temporary directory the local context is unpacked to (server side)
|
2014-08-26 19:25:44 +00:00
|
|
|
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// Run the builder with the context. This is the lynchpin of this package. This
|
|
|
|
// will (barring errors):
|
|
|
|
//
|
|
|
|
// * call readContext() which will set up the temporary directory and unpack
|
|
|
|
// the context into it.
|
|
|
|
// * read the dockerfile
|
|
|
|
// * parse the dockerfile
|
|
|
|
// * walk the parse tree and execute it by dispatching to handlers. If Remove
|
|
|
|
// or ForceRemove is set, additional cleanup around containers happens after
|
|
|
|
// processing.
|
|
|
|
// * Print a happy message and return the image ID.
|
|
|
|
//
|
2014-08-26 19:25:44 +00:00
|
|
|
func (b *Builder) Run(context io.Reader) (string, error) {
|
2014-08-07 05:56:44 +00:00
|
|
|
if err := b.readContext(context); err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-09-15 13:01:23 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(b.contextPath); err != nil {
|
|
|
|
log.Debugf("[BUILDER] failed to remove temporary context: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
filename := path.Join(b.contextPath, "Dockerfile")
|
2014-08-26 19:25:44 +00:00
|
|
|
|
|
|
|
fi, err := os.Stat(filename)
|
|
|
|
if os.IsNotExist(err) {
|
2014-08-05 22:41:09 +00:00
|
|
|
return "", fmt.Errorf("Cannot build a directory without a Dockerfile")
|
|
|
|
}
|
2014-08-26 19:25:44 +00:00
|
|
|
if fi.Size() == 0 {
|
|
|
|
return "", ErrDockerfileEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(filename)
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-08-26 19:25:44 +00:00
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
ast, err := parser.Parse(f)
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-08-26 19:25:44 +00:00
|
|
|
b.dockerfile = ast
|
|
|
|
|
|
|
|
// some initializations that would not have been supplied by the caller.
|
2014-10-24 00:23:25 +00:00
|
|
|
b.Config = &runconfig.Config{}
|
2014-08-26 19:25:44 +00:00
|
|
|
b.TmpContainers = map[string]struct{}{}
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2014-08-26 19:25:44 +00:00
|
|
|
for i, n := range b.dockerfile.Children {
|
2014-08-05 20:17:40 +00:00
|
|
|
if err := b.dispatch(i, n); err != nil {
|
2014-08-26 19:25:44 +00:00
|
|
|
if b.ForceRemove {
|
2014-08-19 11:14:21 +00:00
|
|
|
b.clearTmp()
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
return "", err
|
2014-08-13 10:07:41 +00:00
|
|
|
}
|
2014-08-26 19:25:44 +00:00
|
|
|
fmt.Fprintf(b.OutStream, " ---> %s\n", utils.TruncateID(b.image))
|
|
|
|
if b.Remove {
|
2014-08-19 11:14:21 +00:00
|
|
|
b.clearTmp()
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
if b.image == "" {
|
2014-08-07 05:56:44 +00:00
|
|
|
return "", fmt.Errorf("No image was generated. Is your Dockerfile empty?\n")
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 19:25:44 +00:00
|
|
|
fmt.Fprintf(b.OutStream, "Successfully built %s\n", utils.TruncateID(b.image))
|
2014-08-05 22:41:09 +00:00
|
|
|
return b.image, nil
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// This method is the entrypoint to all statement handling routines.
|
|
|
|
//
|
|
|
|
// Almost all nodes will have this structure:
|
|
|
|
// Child[Node, Node, Node] where Child is from parser.Node.Children and each
|
|
|
|
// node comes from parser.Node.Next. This forms a "line" with a statement and
|
|
|
|
// arguments and we process them in this normalized form by hitting
|
2014-08-26 19:25:44 +00:00
|
|
|
// evaluateTable with the leaf nodes of the command and the Builder object.
|
2014-08-07 05:56:44 +00:00
|
|
|
//
|
|
|
|
// ONBUILD is a special case; in this case the parser will emit:
|
|
|
|
// Child[Node, Child[Node, Node...]] where the first node is the literal
|
|
|
|
// "onbuild" and the child entrypoint is the command of the ONBUILD statmeent,
|
|
|
|
// such as `RUN` in ONBUILD RUN foo. There is special case logic in here to
|
|
|
|
// deal with that, at least until it becomes more of a general concern with new
|
|
|
|
// features.
|
2014-08-26 19:25:44 +00:00
|
|
|
func (b *Builder) dispatch(stepN int, ast *parser.Node) error {
|
2014-08-05 20:17:40 +00:00
|
|
|
cmd := ast.Value
|
2014-08-13 10:07:41 +00:00
|
|
|
attrs := ast.Attributes
|
2014-10-13 20:14:35 +00:00
|
|
|
original := ast.Original
|
2014-08-05 20:17:40 +00:00
|
|
|
strs := []string{}
|
2014-08-13 10:07:41 +00:00
|
|
|
msg := fmt.Sprintf("Step %d : %s", stepN, strings.ToUpper(cmd))
|
2014-08-05 22:41:09 +00:00
|
|
|
|
|
|
|
if cmd == "onbuild" {
|
|
|
|
ast = ast.Next.Children[0]
|
2014-10-25 18:29:18 +00:00
|
|
|
strs = append(strs, ast.Value)
|
2014-08-13 10:07:41 +00:00
|
|
|
msg += " " + ast.Value
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 20:17:40 +00:00
|
|
|
for ast.Next != nil {
|
|
|
|
ast = ast.Next
|
2014-10-25 18:29:18 +00:00
|
|
|
var str string
|
|
|
|
str = ast.Value
|
|
|
|
if _, ok := replaceEnvAllowed[cmd]; ok {
|
|
|
|
str = b.replaceEnv(ast.Value)
|
|
|
|
}
|
|
|
|
strs = append(strs, str)
|
2014-08-13 10:07:41 +00:00
|
|
|
msg += " " + ast.Value
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 19:25:44 +00:00
|
|
|
fmt.Fprintln(b.OutStream, msg)
|
2014-08-05 20:17:40 +00:00
|
|
|
|
|
|
|
// XXX yes, we skip any cmds that are not valid; the parser should have
|
|
|
|
// picked these out already.
|
|
|
|
if f, ok := evaluateTable[cmd]; ok {
|
2014-10-13 20:14:35 +00:00
|
|
|
return f(b, strs, attrs, original)
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 19:25:44 +00:00
|
|
|
fmt.Fprintf(b.ErrStream, "# Skipping unknown instruction %s\n", strings.ToUpper(cmd))
|
2014-08-19 11:14:21 +00:00
|
|
|
|
2014-08-05 20:17:40 +00:00
|
|
|
return nil
|
|
|
|
}
|