2014-08-11 15:44:31 +00:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
2015-01-09 21:07:00 +00:00
|
|
|
"bytes"
|
2015-04-04 14:54:43 +00:00
|
|
|
"errors"
|
2015-03-25 07:44:12 +00:00
|
|
|
"fmt"
|
2014-08-11 15:44:31 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-08-13 18:34:34 +00:00
|
|
|
"runtime"
|
2015-02-05 11:26:05 +00:00
|
|
|
"strings"
|
2015-04-16 18:11:26 +00:00
|
|
|
"sync"
|
2014-08-11 15:44:31 +00:00
|
|
|
|
2014-09-11 14:42:17 +00:00
|
|
|
"github.com/docker/docker/api"
|
2015-01-09 21:07:00 +00:00
|
|
|
"github.com/docker/docker/builder/parser"
|
2015-04-22 12:06:58 +00:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2014-08-11 15:44:31 +00:00
|
|
|
"github.com/docker/docker/daemon"
|
2015-05-11 17:23:32 +00:00
|
|
|
"github.com/docker/docker/graph/tags"
|
2014-09-30 06:23:36 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-03-29 21:17:23 +00:00
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2014-08-11 15:44:31 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2015-04-04 14:54:43 +00:00
|
|
|
"github.com/docker/docker/pkg/progressreader"
|
2015-03-18 02:18:41 +00:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2015-04-08 02:29:29 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-07-23 02:26:06 +00:00
|
|
|
"github.com/docker/docker/pkg/ulimit"
|
2014-11-24 23:47:42 +00:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2014-08-11 15:44:31 +00:00
|
|
|
"github.com/docker/docker/registry"
|
2015-01-09 21:07:00 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-04-23 17:19:34 +00:00
|
|
|
"github.com/docker/docker/utils"
|
2014-08-11 15:44:31 +00:00
|
|
|
)
|
|
|
|
|
2015-04-04 14:54:43 +00:00
|
|
|
// When downloading remote contexts, limit the amount (in bytes)
|
|
|
|
// to be read from the response body in order to detect its Content-Type
|
|
|
|
const maxPreambleLength = 100
|
|
|
|
|
2015-02-05 11:26:05 +00:00
|
|
|
// whitelist of commands allowed for a commit/import
|
2014-10-24 21:11:00 +00:00
|
|
|
var validCommitCommands = map[string]bool{
|
|
|
|
"cmd": true,
|
2015-07-09 16:57:42 +00:00
|
|
|
"entrypoint": true,
|
2014-10-24 21:11:00 +00:00
|
|
|
"env": true,
|
|
|
|
"expose": true,
|
2015-06-04 13:47:55 +00:00
|
|
|
"label": true,
|
2015-07-09 16:57:42 +00:00
|
|
|
"onbuild": true,
|
|
|
|
"user": true,
|
|
|
|
"volume": true,
|
|
|
|
"workdir": true,
|
2014-10-24 21:11:00 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00: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,
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
// Config contains all configs for a build job
|
2015-04-16 18:11:26 +00:00
|
|
|
type Config struct {
|
|
|
|
DockerfileName string
|
|
|
|
RemoteURL string
|
|
|
|
RepoName string
|
|
|
|
SuppressOutput bool
|
|
|
|
NoCache bool
|
|
|
|
Remove bool
|
|
|
|
ForceRemove bool
|
|
|
|
Pull bool
|
|
|
|
Memory int64
|
|
|
|
MemorySwap int64
|
2015-07-22 05:29:03 +00:00
|
|
|
CPUShares int64
|
|
|
|
CPUPeriod int64
|
|
|
|
CPUQuota int64
|
|
|
|
CPUSetCpus string
|
|
|
|
CPUSetMems string
|
2015-03-26 23:14:31 +00:00
|
|
|
CgroupParent string
|
2015-07-23 02:26:06 +00:00
|
|
|
Ulimits []*ulimit.Ulimit
|
2015-06-23 19:58:17 +00:00
|
|
|
AuthConfigs map[string]cliconfig.AuthConfig
|
2014-11-14 18:59:14 +00:00
|
|
|
BuildArgs map[string]string
|
2015-04-16 18:11:26 +00:00
|
|
|
|
2015-04-16 21:26:33 +00:00
|
|
|
Stdout io.Writer
|
|
|
|
Context io.ReadCloser
|
2015-04-16 18:11:26 +00:00
|
|
|
// When closed, the job has been cancelled.
|
|
|
|
// Note: not all jobs implement cancellation.
|
|
|
|
// See Job.Cancel() and Job.WaitCancelled()
|
|
|
|
cancelled chan struct{}
|
|
|
|
cancelOnce sync.Once
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
// Cancel signals the build job to cancel
|
2015-04-16 18:11:26 +00:00
|
|
|
func (b *Config) Cancel() {
|
|
|
|
b.cancelOnce.Do(func() {
|
|
|
|
close(b.cancelled)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
// WaitCancelled returns a channel which is closed ("never blocks") when
|
|
|
|
// the job is cancelled.
|
2015-04-16 18:11:26 +00:00
|
|
|
func (b *Config) WaitCancelled() <-chan struct{} {
|
|
|
|
return b.cancelled
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
// NewBuildConfig returns a new Config struct
|
2015-04-16 21:26:33 +00:00
|
|
|
func NewBuildConfig() *Config {
|
|
|
|
return &Config{
|
2015-06-23 19:58:17 +00:00
|
|
|
AuthConfigs: map[string]cliconfig.AuthConfig{},
|
|
|
|
cancelled: make(chan struct{}),
|
2015-04-16 18:11:26 +00:00
|
|
|
}
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
// Build is the main interface of the package, it gathers the Builder
|
|
|
|
// struct and calls builder.Run() to do all the real build job.
|
2015-09-29 17:51:40 +00:00
|
|
|
func Build(d *daemon.Daemon, buildConfig *Config) error {
|
2014-08-11 15:44:31 +00:00
|
|
|
var (
|
2015-04-16 18:11:26 +00:00
|
|
|
repoName string
|
|
|
|
tag string
|
|
|
|
context io.ReadCloser
|
2014-08-11 15:44:31 +00:00
|
|
|
)
|
2015-04-04 14:54:43 +00:00
|
|
|
sf := streamformatter.NewJSONStreamFormatter()
|
2014-09-11 14:42:17 +00:00
|
|
|
|
2015-04-16 18:11:26 +00:00
|
|
|
repoName, tag = parsers.ParseRepositoryTag(buildConfig.RepoName)
|
2014-10-02 15:37:13 +00:00
|
|
|
if repoName != "" {
|
2015-04-16 21:26:33 +00:00
|
|
|
if err := registry.ValidateRepositoryName(repoName); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-10-02 15:37:13 +00:00
|
|
|
}
|
|
|
|
if len(tag) > 0 {
|
2015-05-11 17:23:32 +00:00
|
|
|
if err := tags.ValidateTagName(tag); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-10-02 15:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 15:44:31 +00:00
|
|
|
|
2015-04-16 18:11:26 +00:00
|
|
|
if buildConfig.RemoteURL == "" {
|
2015-04-16 21:26:33 +00:00
|
|
|
context = ioutil.NopCloser(buildConfig.Context)
|
2015-04-16 18:11:26 +00:00
|
|
|
} else if urlutil.IsGitURL(buildConfig.RemoteURL) {
|
2015-04-23 17:19:34 +00:00
|
|
|
root, err := utils.GitClone(buildConfig.RemoteURL)
|
2014-08-11 15:44:31 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
|
|
|
c, err := archive.Tar(root, archive.Uncompressed)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
context = c
|
2015-04-16 18:11:26 +00:00
|
|
|
} else if urlutil.IsURL(buildConfig.RemoteURL) {
|
|
|
|
f, err := httputils.Download(buildConfig.RemoteURL)
|
2014-08-11 15:44:31 +00:00
|
|
|
if err != nil {
|
2015-04-04 14:54:43 +00:00
|
|
|
return fmt.Errorf("Error downloading remote context %s: %v", buildConfig.RemoteURL, err)
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
defer f.Body.Close()
|
2015-04-04 14:54:43 +00:00
|
|
|
ct := f.Header.Get("Content-Type")
|
2015-07-23 21:19:58 +00:00
|
|
|
clen := f.ContentLength
|
2015-04-04 14:54:43 +00:00
|
|
|
contentType, bodyReader, err := inspectResponse(ct, f.Body, clen)
|
|
|
|
|
|
|
|
defer bodyReader.Close()
|
|
|
|
|
2014-08-11 15:44:31 +00:00
|
|
|
if err != nil {
|
2015-04-04 14:54:43 +00:00
|
|
|
return fmt.Errorf("Error detecting content type for remote %s: %v", buildConfig.RemoteURL, err)
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
2015-04-04 14:54:43 +00:00
|
|
|
if contentType == httputils.MimeTypes.TextPlain {
|
|
|
|
dockerFile, err := ioutil.ReadAll(bodyReader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-17 18:25:36 +00:00
|
|
|
|
2015-04-04 14:54:43 +00:00
|
|
|
// When we're downloading just a Dockerfile put it in
|
|
|
|
// the default name - don't allow the client to move/specify it
|
|
|
|
buildConfig.DockerfileName = api.DefaultDockerfileName
|
2015-02-17 18:25:36 +00:00
|
|
|
|
2015-04-04 14:54:43 +00:00
|
|
|
c, err := archive.Generate(buildConfig.DockerfileName, string(dockerFile))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
context = c
|
|
|
|
} else {
|
|
|
|
// Pass through - this is a pre-packaged context, presumably
|
|
|
|
// with a Dockerfile with the right name inside it.
|
|
|
|
prCfg := progressreader.Config{
|
|
|
|
In: bodyReader,
|
|
|
|
Out: buildConfig.Stdout,
|
|
|
|
Formatter: sf,
|
|
|
|
Size: clen,
|
|
|
|
NewLines: true,
|
|
|
|
ID: "Downloading context",
|
|
|
|
Action: buildConfig.RemoteURL,
|
|
|
|
}
|
|
|
|
context = progressreader.New(prCfg)
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 14:54:43 +00:00
|
|
|
defer context.Close()
|
2014-08-11 15:44:31 +00:00
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
builder := &builder{
|
2015-04-16 21:26:33 +00:00
|
|
|
Daemon: d,
|
2015-07-25 08:35:07 +00:00
|
|
|
OutStream: &streamformatter.StdoutFormatter{
|
2015-04-16 18:11:26 +00:00
|
|
|
Writer: buildConfig.Stdout,
|
2014-08-11 15:44:31 +00:00
|
|
|
StreamFormatter: sf,
|
|
|
|
},
|
2015-07-25 08:35:07 +00:00
|
|
|
ErrStream: &streamformatter.StderrFormatter{
|
2015-04-16 18:11:26 +00:00
|
|
|
Writer: buildConfig.Stdout,
|
2014-08-11 15:44:31 +00:00
|
|
|
StreamFormatter: sf,
|
|
|
|
},
|
2014-11-14 18:59:14 +00:00
|
|
|
Verbose: !buildConfig.SuppressOutput,
|
|
|
|
UtilizeCache: !buildConfig.NoCache,
|
|
|
|
Remove: buildConfig.Remove,
|
|
|
|
ForceRemove: buildConfig.ForceRemove,
|
|
|
|
Pull: buildConfig.Pull,
|
|
|
|
OutOld: buildConfig.Stdout,
|
|
|
|
StreamFormatter: sf,
|
|
|
|
AuthConfigs: buildConfig.AuthConfigs,
|
|
|
|
dockerfileName: buildConfig.DockerfileName,
|
|
|
|
cpuShares: buildConfig.CPUShares,
|
|
|
|
cpuPeriod: buildConfig.CPUPeriod,
|
|
|
|
cpuQuota: buildConfig.CPUQuota,
|
|
|
|
cpuSetCpus: buildConfig.CPUSetCpus,
|
|
|
|
cpuSetMems: buildConfig.CPUSetMems,
|
|
|
|
cgroupParent: buildConfig.CgroupParent,
|
|
|
|
memory: buildConfig.Memory,
|
|
|
|
memorySwap: buildConfig.MemorySwap,
|
|
|
|
ulimits: buildConfig.Ulimits,
|
|
|
|
cancelled: buildConfig.WaitCancelled(),
|
|
|
|
id: stringid.GenerateRandomID(),
|
|
|
|
buildArgs: buildConfig.BuildArgs,
|
|
|
|
allowedBuildArgs: make(map[string]bool),
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 02:29:29 +00:00
|
|
|
defer func() {
|
2015-09-29 17:51:40 +00:00
|
|
|
builder.Daemon.Graph().Release(builder.id, builder.activeImages...)
|
2015-04-08 02:29:29 +00:00
|
|
|
}()
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
id, err := builder.Run(context)
|
2014-08-11 15:44:31 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
if repoName != "" {
|
2015-09-29 17:51:40 +00:00
|
|
|
return d.Repositories().Tag(repoName, tag, id, true)
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
2015-01-09 21:07:00 +00:00
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
// BuildFromConfig will do build directly from parameter 'changes', which comes
|
|
|
|
// from Dockerfile entries, it will:
|
|
|
|
//
|
|
|
|
// - call parse.Parse() to get AST root from Dockerfile entries
|
|
|
|
// - do build by calling builder.dispatch() to call all entries' handling routines
|
2015-09-29 17:51:40 +00:00
|
|
|
func BuildFromConfig(d *daemon.Daemon, c *runconfig.Config, changes []string) (*runconfig.Config, error) {
|
2015-02-05 11:26:05 +00:00
|
|
|
ast, err := parser.Parse(bytes.NewBufferString(strings.Join(changes, "\n")))
|
2015-01-09 21:07:00 +00:00
|
|
|
if err != nil {
|
2015-04-16 21:26:33 +00:00
|
|
|
return nil, err
|
2015-01-09 21:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 21:11:00 +00:00
|
|
|
// ensure that the commands are valid
|
|
|
|
for _, n := range ast.Children {
|
|
|
|
if !validCommitCommands[n.Value] {
|
2015-04-16 21:26:33 +00:00
|
|
|
return nil, fmt.Errorf("%s is not a valid change command", n.Value)
|
2014-10-24 21:11:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
builder := &builder{
|
2015-04-16 21:26:33 +00:00
|
|
|
Daemon: d,
|
|
|
|
Config: c,
|
2015-01-09 21:07:00 +00:00
|
|
|
OutStream: ioutil.Discard,
|
|
|
|
ErrStream: ioutil.Discard,
|
|
|
|
disableCommit: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, n := range ast.Children {
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := builder.dispatch(i, n); err != nil {
|
2015-04-16 21:26:33 +00:00
|
|
|
return nil, err
|
2015-01-09 21:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 21:26:33 +00:00
|
|
|
return builder.Config, nil
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
// CommitConfig contains build configs for commit operation
|
|
|
|
type CommitConfig struct {
|
2015-06-20 10:40:37 +00:00
|
|
|
Pause bool
|
|
|
|
Repo string
|
|
|
|
Tag string
|
|
|
|
Author string
|
|
|
|
Comment string
|
|
|
|
Changes []string
|
|
|
|
Config *runconfig.Config
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
// Commit will create a new image from a container's changes
|
2015-09-29 17:51:40 +00:00
|
|
|
func Commit(name string, d *daemon.Daemon, c *CommitConfig) (string, error) {
|
|
|
|
container, err := d.Get(name)
|
2015-04-16 21:26:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-01-09 21:07:00 +00:00
|
|
|
}
|
2015-04-16 21:26:33 +00:00
|
|
|
|
2015-08-13 18:34:34 +00:00
|
|
|
// It is not possible to commit a running container on Windows
|
|
|
|
if runtime.GOOS == "windows" && container.IsRunning() {
|
|
|
|
return "", fmt.Errorf("Windows does not support commit of a running container")
|
|
|
|
}
|
|
|
|
|
2015-06-06 16:41:42 +00:00
|
|
|
if c.Config == nil {
|
|
|
|
c.Config = &runconfig.Config{}
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
newConfig, err := BuildFromConfig(d, c.Config, c.Changes)
|
2015-04-16 21:26:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := runconfig.Merge(newConfig, container.Config); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-06-20 10:40:37 +00:00
|
|
|
commitCfg := &daemon.ContainerCommitConfig{
|
|
|
|
Pause: c.Pause,
|
|
|
|
Repo: c.Repo,
|
|
|
|
Tag: c.Tag,
|
|
|
|
Author: c.Author,
|
|
|
|
Comment: c.Comment,
|
|
|
|
Config: newConfig,
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
img, err := d.Commit(container, commitCfg)
|
2015-04-16 21:26:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return img.ID, nil
|
2015-01-09 21:07:00 +00:00
|
|
|
}
|
2015-04-04 14:54:43 +00:00
|
|
|
|
|
|
|
// inspectResponse looks into the http response data at r to determine whether its
|
|
|
|
// content-type is on the list of acceptable content types for remote build contexts.
|
|
|
|
// This function returns:
|
|
|
|
// - a string representation of the detected content-type
|
|
|
|
// - an io.Reader for the response body
|
|
|
|
// - an error value which will be non-nil either when something goes wrong while
|
|
|
|
// reading bytes from r or when the detected content-type is not acceptable.
|
2015-07-23 21:19:58 +00:00
|
|
|
func inspectResponse(ct string, r io.ReadCloser, clen int64) (string, io.ReadCloser, error) {
|
2015-04-04 14:54:43 +00:00
|
|
|
plen := clen
|
|
|
|
if plen <= 0 || plen > maxPreambleLength {
|
|
|
|
plen = maxPreambleLength
|
|
|
|
}
|
|
|
|
|
|
|
|
preamble := make([]byte, plen, plen)
|
|
|
|
rlen, err := r.Read(preamble)
|
|
|
|
if rlen == 0 {
|
|
|
|
return ct, r, errors.New("Empty response")
|
|
|
|
}
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return ct, r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
preambleR := bytes.NewReader(preamble)
|
|
|
|
bodyReader := ioutil.NopCloser(io.MultiReader(preambleR, r))
|
|
|
|
// Some web servers will use application/octet-stream as the default
|
|
|
|
// content type for files without an extension (e.g. 'Dockerfile')
|
|
|
|
// so if we receive this value we better check for text content
|
|
|
|
contentType := ct
|
|
|
|
if len(ct) == 0 || ct == httputils.MimeTypes.OctetStream {
|
|
|
|
contentType, _, err = httputils.DetectContentType(preamble)
|
|
|
|
if err != nil {
|
|
|
|
return contentType, bodyReader, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType = selectAcceptableMIME(contentType)
|
|
|
|
var cterr error
|
|
|
|
if len(contentType) == 0 {
|
|
|
|
cterr = fmt.Errorf("unsupported Content-Type %q", ct)
|
|
|
|
contentType = ct
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentType, bodyReader, cterr
|
|
|
|
}
|