2015-09-05 19:49:06 +00:00
|
|
|
package dockerfile
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// internals for handling commands. Covers many areas and a lot of
|
|
|
|
// non-contiguous functionality. Please read the comments.
|
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-03-16 23:07:41 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-05-14 18:18:48 +00:00
|
|
|
"github.com/docker/docker/image"
|
2015-03-24 11:25:26 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-03-15 22:28:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2014-08-05 22:41:09 +00:00
|
|
|
)
|
|
|
|
|
2017-04-26 21:45:16 +00:00
|
|
|
func (b *Builder) commit(dispatchState *dispatchState, comment string) error {
|
2015-01-09 21:07:00 +00:00
|
|
|
if b.disableCommit {
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-26 21:45:16 +00:00
|
|
|
if !dispatchState.hasFromImage() {
|
2016-12-25 06:37:31 +00:00
|
|
|
return errors.New("Please provide a source image with `from` prior to commit")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2016-03-16 21:52:34 +00:00
|
|
|
|
2017-04-26 21:45:16 +00:00
|
|
|
runConfigWithCommentCmd := copyRunConfig(dispatchState.runConfig, withCmdComment(comment))
|
|
|
|
hit, err := b.probeCache(dispatchState, runConfigWithCommentCmd)
|
2017-04-21 18:11:21 +00:00
|
|
|
if err != nil || hit {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
id, err := b.create(runConfigWithCommentCmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
|
2017-04-26 21:45:16 +00:00
|
|
|
return b.commitContainer(dispatchState, id, runConfigWithCommentCmd)
|
2017-04-21 18:11:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 21:45:16 +00:00
|
|
|
func (b *Builder) commitContainer(dispatchState *dispatchState, id string, containerConfig *container.Config) error {
|
2017-04-21 18:11:21 +00:00
|
|
|
if b.disableCommit {
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-30 11:34:09 +00:00
|
|
|
|
2016-03-16 23:07:41 +00:00
|
|
|
commitCfg := &backend.ContainerCommitConfig{
|
|
|
|
ContainerCommitConfig: types.ContainerCommitConfig{
|
2017-04-26 21:45:16 +00:00
|
|
|
Author: dispatchState.maintainer,
|
2016-03-16 23:07:41 +00:00
|
|
|
Pause: true,
|
2017-04-25 16:21:43 +00:00
|
|
|
// TODO: this should be done by Commit()
|
2017-04-26 21:45:16 +00:00
|
|
|
Config: copyRunConfig(dispatchState.runConfig),
|
2016-03-16 23:07:41 +00:00
|
|
|
},
|
2017-04-21 19:08:11 +00:00
|
|
|
ContainerConfig: containerConfig,
|
2015-06-20 10:40:37 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 20:17:40 +00:00
|
|
|
// Commit the container
|
2015-11-18 22:20:54 +00:00
|
|
|
imageID, err := b.docker.Commit(id, commitCfg)
|
2014-08-05 20:17:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-16 21:52:34 +00:00
|
|
|
|
2017-04-26 21:45:16 +00:00
|
|
|
dispatchState.imageID = imageID
|
2017-05-14 18:18:48 +00:00
|
|
|
b.buildStages.update(imageID)
|
2014-08-05 20:17:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-25 21:03:29 +00:00
|
|
|
func (b *Builder) exportImage(state *dispatchState, imageMount *imageMount, runConfig *container.Config) error {
|
|
|
|
newLayer, err := imageMount.Layer().Commit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add an image mount without an image so the layer is properly unmounted
|
|
|
|
// if there is an error before we can add the full mount with image
|
|
|
|
b.imageSources.Add(newImageMount(nil, newLayer))
|
|
|
|
|
|
|
|
parentImage, ok := imageMount.Image().(*image.Image)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("unexpected image type")
|
|
|
|
}
|
|
|
|
|
|
|
|
newImage := image.NewChildImage(parentImage, image.ChildConfig{
|
|
|
|
Author: state.maintainer,
|
|
|
|
ContainerConfig: runConfig,
|
|
|
|
DiffID: newLayer.DiffID(),
|
|
|
|
Config: copyRunConfig(state.runConfig),
|
2017-05-17 19:01:43 +00:00
|
|
|
}, parentImage.OS)
|
2017-05-25 21:03:29 +00:00
|
|
|
|
|
|
|
// TODO: it seems strange to marshal this here instead of just passing in the
|
|
|
|
// image struct
|
|
|
|
config, err := newImage.MarshalJSON()
|
2017-05-14 18:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to encode image config")
|
|
|
|
}
|
|
|
|
|
2017-05-25 21:03:29 +00:00
|
|
|
exportedImage, err := b.docker.CreateImage(config, state.imageID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to export image")
|
|
|
|
}
|
|
|
|
|
|
|
|
state.imageID = exportedImage.ImageID()
|
|
|
|
b.imageSources.Add(newImageMount(exportedImage, newLayer))
|
|
|
|
b.buildStages.update(state.imageID)
|
|
|
|
return nil
|
2017-05-14 18:18:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-07 18:37:46 +00:00
|
|
|
func (b *Builder) performCopy(state *dispatchState, inst copyInstruction) error {
|
|
|
|
srcHash := getSourceHashFromInfos(inst.infos)
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
// TODO: should this have been using origPaths instead of srcHash in the comment?
|
2017-04-21 19:08:11 +00:00
|
|
|
runConfigWithCommentCmd := copyRunConfig(
|
2017-05-07 18:37:46 +00:00
|
|
|
state.runConfig,
|
|
|
|
withCmdCommentString(fmt.Sprintf("%s %s in %s ", inst.cmdName, srcHash, inst.dest)))
|
2017-05-25 21:03:29 +00:00
|
|
|
hit, err := b.probeCache(state, runConfigWithCommentCmd)
|
|
|
|
if err != nil || hit {
|
2016-05-03 01:33:59 +00:00
|
|
|
return err
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 18:18:48 +00:00
|
|
|
imageMount, err := b.imageSources.Get(state.imageID)
|
|
|
|
if err != nil {
|
2017-05-25 21:03:29 +00:00
|
|
|
return errors.Wrapf(err, "failed to get destination image %q", state.imageID)
|
2017-05-14 18:18:48 +00:00
|
|
|
}
|
2017-05-25 21:03:29 +00:00
|
|
|
destInfo, err := createDestInfo(state.runConfig.WorkingDir, inst, imageMount)
|
2017-05-14 18:18:48 +00:00
|
|
|
if err != nil {
|
2017-05-25 21:03:29 +00:00
|
|
|
return err
|
2017-05-14 18:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts := copyFileOptions{
|
|
|
|
decompress: inst.allowLocalDecompression,
|
|
|
|
archiver: b.archiver,
|
|
|
|
}
|
2017-05-07 18:37:46 +00:00
|
|
|
for _, info := range inst.infos {
|
2017-05-25 21:03:29 +00:00
|
|
|
if err := performCopyForInfo(destInfo, info, opts); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to copy files")
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 21:03:29 +00:00
|
|
|
return b.exportImage(state, imageMount, runConfigWithCommentCmd)
|
|
|
|
}
|
2017-05-14 18:18:48 +00:00
|
|
|
|
2017-05-25 21:03:29 +00:00
|
|
|
func createDestInfo(workingDir string, inst copyInstruction, imageMount *imageMount) (copyInfo, error) {
|
|
|
|
// Twiddle the destination when it's a relative path - meaning, make it
|
|
|
|
// relative to the WORKINGDIR
|
|
|
|
dest, err := normaliseDest(workingDir, inst.dest)
|
|
|
|
if err != nil {
|
|
|
|
return copyInfo{}, errors.Wrapf(err, "invalid %s", inst.cmdName)
|
|
|
|
}
|
|
|
|
|
|
|
|
destMount, err := imageMount.Source()
|
|
|
|
if err != nil {
|
|
|
|
return copyInfo{}, errors.Wrapf(err, "failed to mount copy source")
|
|
|
|
}
|
|
|
|
|
|
|
|
return newCopyInfoFromSource(destMount, dest, ""), nil
|
2017-05-07 18:37:46 +00:00
|
|
|
}
|
2014-09-16 16:58:20 +00:00
|
|
|
|
2017-05-07 18:37:46 +00:00
|
|
|
// For backwards compat, if there's just one info then use it as the
|
|
|
|
// cache look-up string, otherwise hash 'em all into one
|
|
|
|
func getSourceHashFromInfos(infos []copyInfo) string {
|
|
|
|
if len(infos) == 1 {
|
|
|
|
return infos[0].hash
|
|
|
|
}
|
|
|
|
var hashs []string
|
|
|
|
for _, info := range infos {
|
|
|
|
hashs = append(hashs, info.hash)
|
|
|
|
}
|
|
|
|
return hashStringSlice("multi", hashs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashStringSlice(prefix string, slice []string) string {
|
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(strings.Join(slice, ",")))
|
|
|
|
return prefix + ":" + hex.EncodeToString(hasher.Sum(nil))
|
2017-04-21 18:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type runConfigModifier func(*container.Config)
|
|
|
|
|
|
|
|
func copyRunConfig(runConfig *container.Config, modifiers ...runConfigModifier) *container.Config {
|
|
|
|
copy := *runConfig
|
|
|
|
for _, modifier := range modifiers {
|
|
|
|
modifier(©)
|
|
|
|
}
|
|
|
|
return ©
|
|
|
|
}
|
|
|
|
|
|
|
|
func withCmd(cmd []string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Cmd = cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 19:08:11 +00:00
|
|
|
// withCmdComment sets Cmd to a nop comment string. See withCmdCommentString for
|
|
|
|
// why there are two almost identical versions of this.
|
2017-04-21 18:11:21 +00:00
|
|
|
func withCmdComment(comment string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Cmd = append(getShell(runConfig), "#(nop) ", comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 19:08:11 +00:00
|
|
|
// withCmdCommentString exists to maintain compatibility with older versions.
|
|
|
|
// A few instructions (workdir, copy, add) used a nop comment that is a single arg
|
|
|
|
// where as all the other instructions used a two arg comment string. This
|
|
|
|
// function implements the single arg version.
|
|
|
|
func withCmdCommentString(comment string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Cmd = append(getShell(runConfig), "#(nop) "+comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
func withEnv(env []string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Env = env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 16:21:43 +00:00
|
|
|
// withEntrypointOverride sets an entrypoint on runConfig if the command is
|
|
|
|
// not empty. The entrypoint is left unmodified if command is empty.
|
|
|
|
//
|
|
|
|
// The dockerfile RUN instruction expect to run without an entrypoint
|
|
|
|
// so the runConfig entrypoint needs to be modified accordingly. ContainerCreate
|
|
|
|
// will change a []string{""} entrypoint to nil, so we probe the cache with the
|
|
|
|
// nil entrypoint.
|
|
|
|
func withEntrypointOverride(cmd []string, entrypoint []string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
if len(cmd) > 0 {
|
|
|
|
runConfig.Entrypoint = entrypoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
// getShell is a helper function which gets the right shell for prefixing the
|
|
|
|
// shell-form of RUN, ENTRYPOINT and CMD instructions
|
|
|
|
func getShell(c *container.Config) []string {
|
|
|
|
if 0 == len(c.Shell) {
|
|
|
|
return append([]string{}, defaultShell[:]...)
|
|
|
|
}
|
|
|
|
return append([]string{}, c.Shell[:]...)
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 21:45:16 +00:00
|
|
|
func (b *Builder) probeCache(dispatchState *dispatchState, runConfig *container.Config) (bool, error) {
|
2017-04-13 22:44:36 +00:00
|
|
|
cachedID, err := b.imageProber.Probe(dispatchState.imageID, runConfig)
|
|
|
|
if cachedID == "" || err != nil {
|
2015-02-25 18:27:32 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2016-12-25 06:37:31 +00:00
|
|
|
fmt.Fprint(b.Stdout, " ---> Using cache\n")
|
2015-09-06 17:26:40 +00:00
|
|
|
|
2017-04-13 22:44:36 +00:00
|
|
|
dispatchState.imageID = string(cachedID)
|
2017-05-14 18:18:48 +00:00
|
|
|
b.buildStages.update(dispatchState.imageID)
|
2015-02-25 18:27:32 +00:00
|
|
|
return true, nil
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 22:44:36 +00:00
|
|
|
var defaultLogConfig = container.LogConfig{Type: "none"}
|
2015-11-18 19:03:08 +00:00
|
|
|
|
2017-04-13 22:44:36 +00:00
|
|
|
func (b *Builder) probeAndCreate(dispatchState *dispatchState, runConfig *container.Config) (string, error) {
|
|
|
|
if hit, err := b.probeCache(dispatchState, runConfig); err != nil || hit {
|
2015-12-10 14:35:53 +00:00
|
|
|
return "", err
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2017-04-13 22:44:36 +00:00
|
|
|
// Set a log config to override any default value set on the daemon
|
|
|
|
hostConfig := &container.HostConfig{LogConfig: defaultLogConfig}
|
|
|
|
container, err := b.containerManager.Create(runConfig, hostConfig)
|
|
|
|
return container.ID, err
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 22:44:36 +00:00
|
|
|
func (b *Builder) create(runConfig *container.Config) (string, error) {
|
|
|
|
hostConfig := hostConfigFromOptions(b.options)
|
|
|
|
container, err := b.containerManager.Create(runConfig, hostConfig)
|
2017-03-30 20:52:40 +00:00
|
|
|
if err != nil {
|
2017-04-13 22:44:36 +00:00
|
|
|
return "", err
|
2015-10-16 09:18:10 +00:00
|
|
|
}
|
2017-04-13 22:44:36 +00:00
|
|
|
// TODO: could this be moved into containerManager.Create() ?
|
|
|
|
for _, warning := range container.Warnings {
|
|
|
|
fmt.Fprintf(b.Stdout, " ---> [Warning] %s\n", warning)
|
2015-10-16 09:18:10 +00:00
|
|
|
}
|
2017-04-13 22:44:36 +00:00
|
|
|
fmt.Fprintf(b.Stdout, " ---> Running in %s\n", stringid.TruncateID(container.ID))
|
|
|
|
return container.ID, nil
|
2015-10-16 09:18:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 22:44:36 +00:00
|
|
|
func hostConfigFromOptions(options *types.ImageBuildOptions) *container.HostConfig {
|
|
|
|
resources := container.Resources{
|
|
|
|
CgroupParent: options.CgroupParent,
|
|
|
|
CPUShares: options.CPUShares,
|
|
|
|
CPUPeriod: options.CPUPeriod,
|
|
|
|
CPUQuota: options.CPUQuota,
|
|
|
|
CpusetCpus: options.CPUSetCPUs,
|
|
|
|
CpusetMems: options.CPUSetMems,
|
|
|
|
Memory: options.Memory,
|
|
|
|
MemorySwap: options.MemorySwap,
|
|
|
|
Ulimits: options.Ulimits,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &container.HostConfig{
|
|
|
|
SecurityOpt: options.SecurityOpt,
|
|
|
|
Isolation: options.Isolation,
|
|
|
|
ShmSize: options.ShmSize,
|
|
|
|
Resources: resources,
|
|
|
|
NetworkMode: container.NetworkMode(options.NetworkMode),
|
|
|
|
// Set a log config to override any default value set on the daemon
|
|
|
|
LogConfig: defaultLogConfig,
|
|
|
|
ExtraHosts: options.ExtraHosts,
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
}
|