2015-09-06 17:26:40 +00:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-01-20 23:32:02 +00:00
|
|
|
"errors"
|
2015-09-06 17:26:40 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-04-21 16:08:37 +00:00
|
|
|
apierrors "github.com/docker/docker/api/errors"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-04-13 17:21:00 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2015-09-06 17:26:40 +00:00
|
|
|
"github.com/docker/docker/builder"
|
2017-04-10 22:27:42 +00:00
|
|
|
"github.com/docker/docker/builder/dockerfile/command"
|
2015-09-06 17:26:40 +00:00
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2016-04-07 21:29:18 +00:00
|
|
|
"github.com/docker/docker/image"
|
2015-09-06 17:26:40 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-04-21 16:08:37 +00:00
|
|
|
perrors "github.com/pkg/errors"
|
2016-03-18 21:42:40 +00:00
|
|
|
"golang.org/x/net/context"
|
2015-09-06 17:26:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var validCommitCommands = map[string]bool{
|
2016-04-18 09:48:13 +00:00
|
|
|
"cmd": true,
|
|
|
|
"entrypoint": true,
|
|
|
|
"healthcheck": true,
|
|
|
|
"env": true,
|
|
|
|
"expose": true,
|
|
|
|
"label": true,
|
|
|
|
"onbuild": true,
|
|
|
|
"user": true,
|
|
|
|
"volume": true,
|
|
|
|
"workdir": true,
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 20:18:06 +00:00
|
|
|
var defaultLogConfig = container.LogConfig{Type: "none"}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// Builder is a Dockerfile builder
|
2015-12-18 00:17:50 +00:00
|
|
|
// It implements the builder.Backend interface.
|
2015-09-06 17:26:40 +00:00
|
|
|
type Builder struct {
|
2015-12-29 20:49:17 +00:00
|
|
|
options *types.ImageBuildOptions
|
2015-09-06 17:26:40 +00:00
|
|
|
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
2016-01-20 23:32:02 +00:00
|
|
|
Output io.Writer
|
2015-09-06 17:26:40 +00:00
|
|
|
|
2016-03-18 21:42:40 +00:00
|
|
|
docker builder.Backend
|
|
|
|
context builder.Context
|
|
|
|
clientCtx context.Context
|
2016-03-22 17:49:03 +00:00
|
|
|
cancel context.CancelFunc
|
2015-09-06 17:26:40 +00:00
|
|
|
|
2017-04-06 21:38:02 +00:00
|
|
|
runConfig *container.Config // runconfig for cmd, run, entrypoint etc.
|
|
|
|
flags *BFlags
|
|
|
|
tmpContainers map[string]struct{}
|
|
|
|
image string // imageID
|
|
|
|
imageContexts *imageContexts // helper for storing contexts from builds
|
|
|
|
noBaseImage bool // A flag to track the use of `scratch` as the base image
|
|
|
|
maintainer string
|
|
|
|
cmdSet bool
|
|
|
|
disableCommit bool
|
|
|
|
cacheBusted bool
|
|
|
|
buildArgs *buildArgs
|
|
|
|
directive parser.Directive
|
2015-09-06 17:26:40 +00:00
|
|
|
|
|
|
|
// TODO: remove once docker.Commit can receive a tag
|
2016-01-20 23:32:02 +00:00
|
|
|
id string
|
2016-09-22 21:38:00 +00:00
|
|
|
|
|
|
|
imageCache builder.ImageCache
|
2016-04-21 16:08:37 +00:00
|
|
|
from builder.Image
|
2016-01-20 23:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuildManager implements builder.Backend and is shared across all Builder objects.
|
|
|
|
type BuildManager struct {
|
2017-03-15 22:28:06 +00:00
|
|
|
backend builder.Backend
|
|
|
|
pathCache *pathCache // TODO: make this persistent
|
2016-01-20 23:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuildManager creates a BuildManager.
|
|
|
|
func NewBuildManager(b builder.Backend) (bm *BuildManager) {
|
2017-03-15 22:28:06 +00:00
|
|
|
return &BuildManager{backend: b, pathCache: &pathCache{}}
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 17:21:00 +00:00
|
|
|
// BuildFromContext builds a new image from a given context.
|
|
|
|
func (bm *BuildManager) BuildFromContext(ctx context.Context, src io.ReadCloser, remote string, buildOptions *types.ImageBuildOptions, pg backend.ProgressWriter) (string, error) {
|
2016-04-21 16:08:37 +00:00
|
|
|
if buildOptions.Squash && !bm.backend.HasExperimental() {
|
|
|
|
return "", apierrors.NewBadRequestError(errors.New("squash is only supported with experimental mode"))
|
|
|
|
}
|
2016-04-13 17:21:00 +00:00
|
|
|
buildContext, dockerfileName, err := builder.DetectContextFromRemoteURL(src, remote, pg.ProgressReaderFunc)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := buildContext.Close(); err != nil {
|
|
|
|
logrus.Debugf("[BUILDER] failed to remove temporary context: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2016-04-21 16:08:37 +00:00
|
|
|
|
2016-04-13 17:21:00 +00:00
|
|
|
if len(dockerfileName) > 0 {
|
|
|
|
buildOptions.Dockerfile = dockerfileName
|
|
|
|
}
|
2017-04-04 18:44:40 +00:00
|
|
|
b, err := NewBuilder(ctx, buildOptions, bm.backend, builder.DockerIgnoreContext{ModifiableContext: buildContext})
|
2016-04-13 17:21:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-03-15 22:28:06 +00:00
|
|
|
b.imageContexts.cache = bm.pathCache
|
2016-04-13 17:21:00 +00:00
|
|
|
return b.build(pg.StdoutFormatter, pg.StderrFormatter, pg.Output)
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// NewBuilder creates a new Dockerfile builder from an optional dockerfile and a Config.
|
|
|
|
// If dockerfile is nil, the Dockerfile specified by Config.DockerfileName,
|
|
|
|
// will be read from the Context passed to Build().
|
2017-04-04 18:44:40 +00:00
|
|
|
func NewBuilder(clientCtx context.Context, config *types.ImageBuildOptions, backend builder.Backend, buildContext builder.Context) (b *Builder, err error) {
|
2015-09-06 17:26:40 +00:00
|
|
|
if config == nil {
|
2015-12-29 20:49:17 +00:00
|
|
|
config = new(types.ImageBuildOptions)
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
2016-03-22 17:49:03 +00:00
|
|
|
ctx, cancel := context.WithCancel(clientCtx)
|
2015-09-06 17:26:40 +00:00
|
|
|
b = &Builder{
|
2017-04-06 21:38:02 +00:00
|
|
|
clientCtx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
options: config,
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
|
|
|
docker: backend,
|
|
|
|
context: buildContext,
|
|
|
|
runConfig: new(container.Config),
|
|
|
|
tmpContainers: map[string]struct{}{},
|
|
|
|
id: stringid.GenerateNonCryptoID(),
|
|
|
|
buildArgs: newBuildArgs(config.BuildArgs),
|
2016-06-27 20:20:47 +00:00
|
|
|
directive: parser.Directive{
|
|
|
|
EscapeSeen: false,
|
|
|
|
LookingForDirectives: true,
|
|
|
|
},
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
2017-03-15 22:28:06 +00:00
|
|
|
b.imageContexts = &imageContexts{b: b}
|
2016-09-22 21:38:00 +00:00
|
|
|
|
2016-06-27 20:20:47 +00:00
|
|
|
parser.SetEscapeToken(parser.DefaultEscapeToken, &b.directive) // Assume the default token for escape
|
2015-09-06 17:26:40 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:17:49 +00:00
|
|
|
func (b *Builder) resetImageCache() {
|
|
|
|
if icb, ok := b.docker.(builder.ImageCacheBuilder); ok {
|
|
|
|
b.imageCache = icb.MakeImageCache(b.options.CacheFrom)
|
|
|
|
}
|
|
|
|
b.noBaseImage = false
|
|
|
|
b.cacheBusted = false
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:32:02 +00:00
|
|
|
// sanitizeRepoAndTags parses the raw "t" parameter received from the client
|
|
|
|
// to a slice of repoAndTag.
|
|
|
|
// It also validates each repoName and tag.
|
|
|
|
func sanitizeRepoAndTags(names []string) ([]reference.Named, error) {
|
|
|
|
var (
|
|
|
|
repoAndTags []reference.Named
|
|
|
|
// This map is used for deduplicating the "-t" parameter.
|
|
|
|
uniqNames = make(map[string]struct{})
|
|
|
|
)
|
|
|
|
for _, repo := range names {
|
|
|
|
if repo == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
ref, err := reference.ParseNormalizedNamed(repo)
|
2016-01-20 23:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
|
|
|
return nil, errors.New("build tag cannot contain a digest")
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
ref = reference.TagNameOnly(ref)
|
2016-01-20 23:32:02 +00:00
|
|
|
|
|
|
|
nameWithTag := ref.String()
|
|
|
|
|
|
|
|
if _, exists := uniqNames[nameWithTag]; !exists {
|
|
|
|
uniqNames[nameWithTag] = struct{}{}
|
|
|
|
repoAndTags = append(repoAndTags, ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return repoAndTags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// build runs the Dockerfile builder from a context and a docker object that allows to make calls
|
2015-09-06 17:26:40 +00:00
|
|
|
// to Docker.
|
|
|
|
//
|
|
|
|
// This will (barring errors):
|
|
|
|
//
|
|
|
|
// * read the dockerfile from context
|
|
|
|
// * parse the dockerfile if not already parsed
|
|
|
|
// * walk the AST and execute it by dispatching to handlers. If Remove
|
|
|
|
// or ForceRemove is set, additional cleanup around containers happens after
|
|
|
|
// processing.
|
2016-01-20 23:32:02 +00:00
|
|
|
// * Tag image, if applicable.
|
2015-09-06 17:26:40 +00:00
|
|
|
// * Print a happy message and return the image ID.
|
|
|
|
//
|
2016-04-13 17:21:00 +00:00
|
|
|
func (b *Builder) build(stdout io.Writer, stderr io.Writer, out io.Writer) (string, error) {
|
2017-03-15 22:28:06 +00:00
|
|
|
defer b.imageContexts.unmount()
|
|
|
|
|
2016-01-20 23:32:02 +00:00
|
|
|
b.Stdout = stdout
|
|
|
|
b.Stderr = stderr
|
|
|
|
b.Output = out
|
|
|
|
|
2017-04-04 18:44:40 +00:00
|
|
|
dockerfile, err := b.readDockerfile()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 17:21:00 +00:00
|
|
|
repoAndTags, err := sanitizeRepoAndTags(b.options.Tags)
|
2016-01-20 23:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-04-04 18:44:40 +00:00
|
|
|
addNodesForLabelOption(dockerfile, b.options.Labels)
|
2016-04-23 19:59:17 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
var shortImgID string
|
2017-04-04 18:44:40 +00:00
|
|
|
total := len(dockerfile.Children)
|
|
|
|
for _, n := range dockerfile.Children {
|
2016-09-13 04:06:04 +00:00
|
|
|
if err := b.checkDispatch(n, false); err != nil {
|
2017-01-10 23:36:08 +00:00
|
|
|
return "", perrors.Wrapf(err, "Dockerfile parse error line %d", n.StartLine)
|
2016-09-13 04:06:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 18:44:40 +00:00
|
|
|
for i, n := range dockerfile.Children {
|
2015-09-06 17:26:40 +00:00
|
|
|
select {
|
2016-03-22 17:49:03 +00:00
|
|
|
case <-b.clientCtx.Done():
|
2015-09-06 17:26:40 +00:00
|
|
|
logrus.Debug("Builder: build cancelled!")
|
2016-12-25 06:37:31 +00:00
|
|
|
fmt.Fprint(b.Stdout, "Build cancelled")
|
|
|
|
return "", errors.New("Build cancelled")
|
2015-09-06 17:26:40 +00:00
|
|
|
default:
|
|
|
|
// Not cancelled yet, keep going...
|
|
|
|
}
|
2016-09-13 04:06:04 +00:00
|
|
|
|
2017-04-10 22:27:42 +00:00
|
|
|
if command.From == n.Value && b.imageContexts.isCurrentTarget(b.options.Target) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-07-24 08:06:10 +00:00
|
|
|
if err := b.dispatch(i, total, n); err != nil {
|
2015-12-29 20:49:17 +00:00
|
|
|
if b.options.ForceRemove {
|
2015-09-06 17:26:40 +00:00
|
|
|
b.clearTmp()
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
2016-03-30 21:26:02 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
shortImgID = stringid.TruncateID(b.image)
|
|
|
|
fmt.Fprintf(b.Stdout, " ---> %s\n", shortImgID)
|
2015-12-29 20:49:17 +00:00
|
|
|
if b.options.Remove {
|
2015-09-06 17:26:40 +00:00
|
|
|
b.clearTmp()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 22:27:42 +00:00
|
|
|
if b.options.Target != "" && !b.imageContexts.isCurrentTarget(b.options.Target) {
|
|
|
|
return "", perrors.Errorf("failed to reach build target %s in Dockerfile", b.options.Target)
|
|
|
|
}
|
|
|
|
|
2017-03-02 19:30:35 +00:00
|
|
|
b.warnOnUnusedBuildArgs()
|
2015-09-06 17:26:40 +00:00
|
|
|
|
|
|
|
if b.image == "" {
|
2016-12-25 06:37:31 +00:00
|
|
|
return "", errors.New("No image was generated. Is your Dockerfile empty?")
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 16:08:37 +00:00
|
|
|
if b.options.Squash {
|
|
|
|
var fromID string
|
|
|
|
if b.from != nil {
|
|
|
|
fromID = b.from.ImageID()
|
|
|
|
}
|
|
|
|
b.image, err = b.docker.SquashImage(b.image, fromID)
|
|
|
|
if err != nil {
|
|
|
|
return "", perrors.Wrap(err, "error squashing image")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 11:21:27 +00:00
|
|
|
fmt.Fprintf(b.Stdout, "Successfully built %s\n", shortImgID)
|
|
|
|
|
2016-04-07 21:29:18 +00:00
|
|
|
imageID := image.ID(b.image)
|
2016-01-20 23:32:02 +00:00
|
|
|
for _, rt := range repoAndTags {
|
2016-04-07 21:29:18 +00:00
|
|
|
if err := b.docker.TagImageWithReference(imageID, rt); err != nil {
|
2016-01-20 23:32:02 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2017-03-24 11:21:27 +00:00
|
|
|
fmt.Fprintf(b.Stdout, "Successfully tagged %s\n", reference.FamiliarString(rt))
|
2016-01-20 23:32:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
return b.image, nil
|
|
|
|
}
|
|
|
|
|
2017-04-04 18:44:40 +00:00
|
|
|
func addNodesForLabelOption(dockerfile *parser.Node, labels map[string]string) {
|
|
|
|
if len(labels) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
node := parser.NodeFromLabels(labels)
|
|
|
|
dockerfile.Children = append(dockerfile.Children, node)
|
|
|
|
}
|
|
|
|
|
2017-03-02 19:30:35 +00:00
|
|
|
// check if there are any leftover build-args that were passed but not
|
|
|
|
// consumed during build. Print a warning, if there are any.
|
|
|
|
func (b *Builder) warnOnUnusedBuildArgs() {
|
2017-04-06 21:38:02 +00:00
|
|
|
leftoverArgs := b.buildArgs.UnreferencedOptionArgs()
|
2017-03-02 19:30:35 +00:00
|
|
|
if len(leftoverArgs) > 0 {
|
|
|
|
fmt.Fprintf(b.Stderr, "[Warning] One or more build-args %v were not consumed\n", leftoverArgs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 22:19:45 +00:00
|
|
|
// hasFromImage returns true if the builder has processed a `FROM <image>` line
|
|
|
|
func (b *Builder) hasFromImage() bool {
|
|
|
|
return b.image != "" || b.noBaseImage
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// Cancel cancels an ongoing Dockerfile build.
|
|
|
|
func (b *Builder) Cancel() {
|
2016-03-22 17:49:03 +00:00
|
|
|
b.cancel()
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 19:28:30 +00:00
|
|
|
// BuildFromConfig builds directly from `changes`, treating it as if it were the contents of a Dockerfile
|
|
|
|
// It will:
|
2016-01-05 13:33:20 +00:00
|
|
|
// - Call parse.Parse() to get an AST root for the concatenated Dockerfile entries.
|
|
|
|
// - Do build by calling builder.dispatch() to call all entries' handling routines
|
|
|
|
//
|
|
|
|
// BuildFromConfig is used by the /commit endpoint, with the changes
|
|
|
|
// coming from the query parameter of the same name.
|
|
|
|
//
|
|
|
|
// TODO: Remove?
|
2015-12-18 18:36:17 +00:00
|
|
|
func BuildFromConfig(config *container.Config, changes []string) (*container.Config, error) {
|
2017-04-04 18:44:40 +00:00
|
|
|
b, err := NewBuilder(context.Background(), nil, nil, nil)
|
2016-06-27 20:20:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ast, err := parser.Parse(bytes.NewBufferString(strings.Join(changes, "\n")), &b.directive)
|
2015-09-06 17:26:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that the commands are valid
|
|
|
|
for _, n := range ast.Children {
|
|
|
|
if !validCommitCommands[n.Value] {
|
|
|
|
return nil, fmt.Errorf("%s is not a valid change command", n.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.runConfig = config
|
|
|
|
b.Stdout = ioutil.Discard
|
|
|
|
b.Stderr = ioutil.Discard
|
|
|
|
b.disableCommit = true
|
|
|
|
|
2016-07-24 08:06:10 +00:00
|
|
|
total := len(ast.Children)
|
2016-09-13 04:06:04 +00:00
|
|
|
for _, n := range ast.Children {
|
|
|
|
if err := b.checkDispatch(n, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
for i, n := range ast.Children {
|
2016-07-24 08:06:10 +00:00
|
|
|
if err := b.dispatch(i, total, n); err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.runConfig, nil
|
|
|
|
}
|