2015-09-06 13:26:40 -04:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-01-20 18:32:02 -05:00
|
|
|
"errors"
|
2015-09-06 13:26:40 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-04-13 13:21:00 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2016-04-07 17:29:18 -04:00
|
|
|
"github.com/docker/docker/image"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-01-20 18:32:02 -05:00
|
|
|
"github.com/docker/docker/reference"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/container"
|
2016-03-18 17:42:40 -04:00
|
|
|
"golang.org/x/net/context"
|
2015-09-06 13:26:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var validCommitCommands = map[string]bool{
|
2016-04-18 05:48:13 -04:00
|
|
|
"cmd": true,
|
|
|
|
"entrypoint": true,
|
|
|
|
"healthcheck": true,
|
|
|
|
"env": true,
|
|
|
|
"expose": true,
|
|
|
|
"label": true,
|
|
|
|
"onbuild": true,
|
|
|
|
"user": true,
|
|
|
|
"volume": true,
|
|
|
|
"workdir": true,
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuiltinAllowedBuildArgs is list of built-in allowed build args
|
|
|
|
var BuiltinAllowedBuildArgs = map[string]bool{
|
|
|
|
"HTTP_PROXY": true,
|
|
|
|
"http_proxy": true,
|
|
|
|
"HTTPS_PROXY": true,
|
|
|
|
"https_proxy": true,
|
|
|
|
"FTP_PROXY": true,
|
|
|
|
"ftp_proxy": true,
|
|
|
|
"NO_PROXY": true,
|
|
|
|
"no_proxy": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builder is a Dockerfile builder
|
2015-12-17 19:17:50 -05:00
|
|
|
// It implements the builder.Backend interface.
|
2015-09-06 13:26:40 -04:00
|
|
|
type Builder struct {
|
2015-12-29 15:49:17 -05:00
|
|
|
options *types.ImageBuildOptions
|
2015-09-06 13:26:40 -04:00
|
|
|
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
2016-01-20 18:32:02 -05:00
|
|
|
Output io.Writer
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2016-03-18 17:42:40 -04:00
|
|
|
docker builder.Backend
|
|
|
|
context builder.Context
|
|
|
|
clientCtx context.Context
|
2016-03-22 13:49:03 -04:00
|
|
|
cancel context.CancelFunc
|
2015-09-06 13:26:40 -04:00
|
|
|
|
|
|
|
dockerfile *parser.Node
|
2015-12-18 13:36:17 -05:00
|
|
|
runConfig *container.Config // runconfig for cmd, run, entrypoint etc.
|
2015-09-06 13:26:40 -04:00
|
|
|
flags *BFlags
|
|
|
|
tmpContainers map[string]struct{}
|
|
|
|
image string // imageID
|
|
|
|
noBaseImage bool
|
|
|
|
maintainer string
|
|
|
|
cmdSet bool
|
|
|
|
disableCommit bool
|
|
|
|
cacheBusted bool
|
|
|
|
allowedBuildArgs map[string]bool // list of build-time args that are allowed for expansion/substitution and passing to commands in 'run'.
|
2016-06-27 16:20:47 -04:00
|
|
|
directive parser.Directive
|
2015-09-06 13:26:40 -04:00
|
|
|
|
|
|
|
// TODO: remove once docker.Commit can receive a tag
|
2016-01-20 18:32:02 -05:00
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildManager implements builder.Backend and is shared across all Builder objects.
|
|
|
|
type BuildManager struct {
|
|
|
|
backend builder.Backend
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuildManager creates a BuildManager.
|
|
|
|
func NewBuildManager(b builder.Backend) (bm *BuildManager) {
|
|
|
|
return &BuildManager{backend: b}
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2016-04-13 13:21:00 -04: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) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if len(dockerfileName) > 0 {
|
|
|
|
buildOptions.Dockerfile = dockerfileName
|
|
|
|
}
|
|
|
|
b, err := NewBuilder(ctx, buildOptions, bm.backend, builder.DockerIgnoreContext{ModifiableContext: buildContext}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.build(pg.StdoutFormatter, pg.StderrFormatter, pg.Output)
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04: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().
|
2016-03-22 13:49:03 -04:00
|
|
|
func NewBuilder(clientCtx context.Context, config *types.ImageBuildOptions, backend builder.Backend, buildContext builder.Context, dockerfile io.ReadCloser) (b *Builder, err error) {
|
2015-09-06 13:26:40 -04:00
|
|
|
if config == nil {
|
2015-12-29 15:49:17 -05:00
|
|
|
config = new(types.ImageBuildOptions)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
if config.BuildArgs == nil {
|
|
|
|
config.BuildArgs = make(map[string]string)
|
|
|
|
}
|
2016-03-22 13:49:03 -04:00
|
|
|
ctx, cancel := context.WithCancel(clientCtx)
|
2015-09-06 13:26:40 -04:00
|
|
|
b = &Builder{
|
2016-03-22 13:49:03 -04:00
|
|
|
clientCtx: ctx,
|
|
|
|
cancel: cancel,
|
2015-12-29 15:49:17 -05:00
|
|
|
options: config,
|
2015-09-06 13:26:40 -04:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
2015-12-29 15:49:17 -05:00
|
|
|
docker: backend,
|
2016-03-22 13:49:03 -04:00
|
|
|
context: buildContext,
|
2015-12-18 13:36:17 -05:00
|
|
|
runConfig: new(container.Config),
|
2015-09-06 13:26:40 -04:00
|
|
|
tmpContainers: map[string]struct{}{},
|
|
|
|
id: stringid.GenerateNonCryptoID(),
|
|
|
|
allowedBuildArgs: make(map[string]bool),
|
2016-06-27 16:20:47 -04:00
|
|
|
directive: parser.Directive{
|
|
|
|
EscapeSeen: false,
|
|
|
|
LookingForDirectives: true,
|
|
|
|
},
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2016-06-27 16:20:47 -04:00
|
|
|
parser.SetEscapeToken(parser.DefaultEscapeToken, &b.directive) // Assume the default token for escape
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
if dockerfile != nil {
|
2016-06-27 16:20:47 -04:00
|
|
|
b.dockerfile, err = parser.Parse(dockerfile, &b.directive)
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 18:32:02 -05: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
|
|
|
|
}
|
|
|
|
|
|
|
|
ref, err := reference.ParseNamed(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ref = reference.WithDefaultTag(ref)
|
|
|
|
|
|
|
|
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
|
|
|
return nil, errors.New("build tag cannot contain a digest")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, isTagged := ref.(reference.NamedTagged); !isTagged {
|
|
|
|
ref, err = reference.WithTag(ref, reference.DefaultTag)
|
2016-03-16 22:43:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-20 18:32:02 -05: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 13:26:40 -04: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 18:32:02 -05:00
|
|
|
// * Tag image, if applicable.
|
2015-09-06 13:26:40 -04:00
|
|
|
// * Print a happy message and return the image ID.
|
|
|
|
//
|
2016-04-13 13:21:00 -04:00
|
|
|
func (b *Builder) build(stdout io.Writer, stderr io.Writer, out io.Writer) (string, error) {
|
2016-01-20 18:32:02 -05:00
|
|
|
b.Stdout = stdout
|
|
|
|
b.Stderr = stderr
|
|
|
|
b.Output = out
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// If Dockerfile was not parsed yet, extract it from the Context
|
|
|
|
if b.dockerfile == nil {
|
|
|
|
if err := b.readDockerfile(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:21:00 -04:00
|
|
|
repoAndTags, err := sanitizeRepoAndTags(b.options.Tags)
|
2016-01-20 18:32:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-04-23 15:59:17 -04:00
|
|
|
if len(b.options.Labels) > 0 {
|
|
|
|
line := "LABEL "
|
|
|
|
for k, v := range b.options.Labels {
|
|
|
|
line += fmt.Sprintf("%q=%q ", k, v)
|
|
|
|
}
|
2016-06-27 16:20:47 -04:00
|
|
|
_, node, err := parser.ParseLine(line, &b.directive)
|
2016-04-23 15:59:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
b.dockerfile.Children = append(b.dockerfile.Children, node)
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
var shortImgID string
|
|
|
|
for i, n := range b.dockerfile.Children {
|
|
|
|
select {
|
2016-03-22 13:49:03 -04:00
|
|
|
case <-b.clientCtx.Done():
|
2015-09-06 13:26:40 -04:00
|
|
|
logrus.Debug("Builder: build cancelled!")
|
|
|
|
fmt.Fprintf(b.Stdout, "Build cancelled")
|
|
|
|
return "", fmt.Errorf("Build cancelled")
|
|
|
|
default:
|
|
|
|
// Not cancelled yet, keep going...
|
|
|
|
}
|
|
|
|
if err := b.dispatch(i, n); err != nil {
|
2015-12-29 15:49:17 -05:00
|
|
|
if b.options.ForceRemove {
|
2015-09-06 13:26:40 -04:00
|
|
|
b.clearTmp()
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
2016-03-30 17:26:02 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
shortImgID = stringid.TruncateID(b.image)
|
|
|
|
fmt.Fprintf(b.Stdout, " ---> %s\n", shortImgID)
|
2015-12-29 15:49:17 -05:00
|
|
|
if b.options.Remove {
|
2015-09-06 13:26:40 -04:00
|
|
|
b.clearTmp()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if there are any leftover build-args that were passed but not
|
|
|
|
// consumed during build. Return an error, if there are any.
|
|
|
|
leftoverArgs := []string{}
|
2015-12-29 15:49:17 -05:00
|
|
|
for arg := range b.options.BuildArgs {
|
2015-09-06 13:26:40 -04:00
|
|
|
if !b.isBuildArgAllowed(arg) {
|
|
|
|
leftoverArgs = append(leftoverArgs, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(leftoverArgs) > 0 {
|
|
|
|
return "", fmt.Errorf("One or more build-args %v were not consumed, failing build.", leftoverArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.image == "" {
|
|
|
|
return "", fmt.Errorf("No image was generated. Is your Dockerfile empty?")
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:29:18 -04:00
|
|
|
imageID := image.ID(b.image)
|
2016-01-20 18:32:02 -05:00
|
|
|
for _, rt := range repoAndTags {
|
2016-04-07 17:29:18 -04:00
|
|
|
if err := b.docker.TagImageWithReference(imageID, rt); err != nil {
|
2016-01-20 18:32:02 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
fmt.Fprintf(b.Stdout, "Successfully built %s\n", shortImgID)
|
|
|
|
return b.image, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel cancels an ongoing Dockerfile build.
|
|
|
|
func (b *Builder) Cancel() {
|
2016-03-22 13:49:03 -04:00
|
|
|
b.cancel()
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2016-01-29 14:28:30 -05:00
|
|
|
// BuildFromConfig builds directly from `changes`, treating it as if it were the contents of a Dockerfile
|
|
|
|
// It will:
|
2016-01-05 08:33:20 -05: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 13:36:17 -05:00
|
|
|
func BuildFromConfig(config *container.Config, changes []string) (*container.Config, error) {
|
2016-06-27 16:20:47 -04:00
|
|
|
b, err := NewBuilder(context.Background(), nil, nil, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ast, err := parser.Parse(bytes.NewBufferString(strings.Join(changes, "\n")), &b.directive)
|
2015-09-06 13:26:40 -04: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
|
|
|
|
|
|
|
|
for i, n := range ast.Children {
|
|
|
|
if err := b.dispatch(i, n); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.runConfig, nil
|
|
|
|
}
|