2015-09-05 15:49:06 -04:00
|
|
|
package dockerfile
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// internals for handling commands. Covers many areas and a lot of
|
|
|
|
// non-contiguous functionality. Please read the comments.
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2017-08-03 20:22:00 -04:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
2017-07-26 12:05:55 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-08-05 18:41:09 -04:00
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-03-16 19:07:41 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-05-14 14:18:48 -04:00
|
|
|
"github.com/docker/docker/image"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2017-07-26 12:05:55 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-03-24 07:25:26 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-07-26 12:05:55 -04:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-07-26 12:05:55 -04:00
|
|
|
lcUser "github.com/opencontainers/runc/libcontainer/user"
|
2017-03-15 18:28:06 -04:00
|
|
|
"github.com/pkg/errors"
|
2014-08-05 18:41:09 -04:00
|
|
|
)
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
// For Windows only
|
|
|
|
var pathBlacklist = map[string]bool{
|
|
|
|
"c:\\": true,
|
|
|
|
"c:\\windows": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Archiver defines an interface for copying files from one destination to
|
|
|
|
// another using Tar/Untar.
|
|
|
|
type Archiver interface {
|
|
|
|
TarUntar(src, dst string) error
|
|
|
|
UntarPath(src, dst string) error
|
|
|
|
CopyWithTar(src, dst string) error
|
|
|
|
CopyFileWithTar(src, dst string) error
|
|
|
|
IDMappings() *idtools.IDMappings
|
|
|
|
}
|
|
|
|
|
|
|
|
// The builder will use the following interfaces if the container fs implements
|
|
|
|
// these for optimized copies to and from the container.
|
|
|
|
type extractor interface {
|
|
|
|
ExtractArchive(src io.Reader, dst string, opts *archive.TarOptions) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type archiver interface {
|
|
|
|
ArchivePath(src string, opts *archive.TarOptions) (io.ReadCloser, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper functions to get tar/untar func
|
|
|
|
func untarFunc(i interface{}) containerfs.UntarFunc {
|
|
|
|
if ea, ok := i.(extractor); ok {
|
|
|
|
return ea.ExtractArchive
|
|
|
|
}
|
|
|
|
return chrootarchive.Untar
|
|
|
|
}
|
|
|
|
|
|
|
|
func tarFunc(i interface{}) containerfs.TarFunc {
|
|
|
|
if ap, ok := i.(archiver); ok {
|
|
|
|
return ap.ArchivePath
|
|
|
|
}
|
|
|
|
return archive.TarWithOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) getArchiver(src, dst containerfs.Driver) Archiver {
|
|
|
|
t, u := tarFunc(src), untarFunc(dst)
|
|
|
|
return &containerfs.Archiver{
|
|
|
|
SrcDriver: src,
|
|
|
|
DstDriver: dst,
|
|
|
|
Tar: t,
|
|
|
|
Untar: u,
|
|
|
|
IDMappingsVar: b.idMappings,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
func (b *Builder) commit(dispatchState *dispatchState, comment string) error {
|
2015-01-09 16:07:00 -05:00
|
|
|
if b.disableCommit {
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
if !dispatchState.hasFromImage() {
|
2016-12-25 01:37:31 -05:00
|
|
|
return errors.New("Please provide a source image with `from` prior to commit")
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2016-03-16 17:52:34 -04:00
|
|
|
|
2017-06-20 18:08:58 -04:00
|
|
|
runConfigWithCommentCmd := copyRunConfig(dispatchState.runConfig, withCmdComment(comment, b.platform))
|
2017-04-26 17:45:16 -04:00
|
|
|
hit, err := b.probeCache(dispatchState, runConfigWithCommentCmd)
|
2017-04-21 14:11:21 -04:00
|
|
|
if err != nil || hit {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
id, err := b.create(runConfigWithCommentCmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
return b.commitContainer(dispatchState, id, runConfigWithCommentCmd)
|
2017-04-21 14:11:21 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
func (b *Builder) commitContainer(dispatchState *dispatchState, id string, containerConfig *container.Config) error {
|
2017-04-21 14:11:21 -04:00
|
|
|
if b.disableCommit {
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-30 07:34:09 -04:00
|
|
|
|
2016-03-16 19:07:41 -04:00
|
|
|
commitCfg := &backend.ContainerCommitConfig{
|
|
|
|
ContainerCommitConfig: types.ContainerCommitConfig{
|
2017-04-26 17:45:16 -04:00
|
|
|
Author: dispatchState.maintainer,
|
2016-03-16 19:07:41 -04:00
|
|
|
Pause: true,
|
2017-04-25 12:21:43 -04:00
|
|
|
// TODO: this should be done by Commit()
|
2017-04-26 17:45:16 -04:00
|
|
|
Config: copyRunConfig(dispatchState.runConfig),
|
2016-03-16 19:07:41 -04:00
|
|
|
},
|
2017-04-21 15:08:11 -04:00
|
|
|
ContainerConfig: containerConfig,
|
2015-06-20 06:40:37 -04:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
// Commit the container
|
2015-11-18 17:20:54 -05:00
|
|
|
imageID, err := b.docker.Commit(id, commitCfg)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-16 17:52:34 -04:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
dispatchState.imageID = imageID
|
2017-05-14 14:18:48 -04:00
|
|
|
b.buildStages.update(imageID)
|
2014-08-05 16:17:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:03:29 -04:00
|
|
|
func (b *Builder) exportImage(state *dispatchState, imageMount *imageMount, runConfig *container.Config) error {
|
2017-06-20 13:34:55 -04:00
|
|
|
newLayer, err := imageMount.Layer().Commit(b.platform)
|
2017-05-25 17:03:29 -04:00
|
|
|
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 15:01:43 -04:00
|
|
|
}, parentImage.OS)
|
2017-05-25 17:03:29 -04:00
|
|
|
|
|
|
|
// TODO: it seems strange to marshal this here instead of just passing in the
|
|
|
|
// image struct
|
|
|
|
config, err := newImage.MarshalJSON()
|
2017-05-14 14:18:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to encode image config")
|
|
|
|
}
|
|
|
|
|
2017-06-20 13:34:55 -04:00
|
|
|
exportedImage, err := b.docker.CreateImage(config, state.imageID, parentImage.OS)
|
2017-05-25 17:03:29 -04:00
|
|
|
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 14:18:48 -04:00
|
|
|
}
|
|
|
|
|
2017-05-07 14:37:46 -04:00
|
|
|
func (b *Builder) performCopy(state *dispatchState, inst copyInstruction) error {
|
|
|
|
srcHash := getSourceHashFromInfos(inst.infos)
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2017-07-26 12:05:55 -04:00
|
|
|
var chownComment string
|
|
|
|
if inst.chownStr != "" {
|
|
|
|
chownComment = fmt.Sprintf("--chown=%s", inst.chownStr)
|
|
|
|
}
|
|
|
|
commentStr := fmt.Sprintf("%s %s%s in %s ", inst.cmdName, chownComment, srcHash, inst.dest)
|
|
|
|
|
2017-04-21 14:11:21 -04:00
|
|
|
// TODO: should this have been using origPaths instead of srcHash in the comment?
|
2017-04-21 15:08:11 -04:00
|
|
|
runConfigWithCommentCmd := copyRunConfig(
|
2017-05-07 14:37:46 -04:00
|
|
|
state.runConfig,
|
2017-07-26 12:05:55 -04:00
|
|
|
withCmdCommentString(commentStr, b.platform))
|
2017-05-25 17:03:29 -04:00
|
|
|
hit, err := b.probeCache(state, runConfigWithCommentCmd)
|
|
|
|
if err != nil || hit {
|
2016-05-02 21:33:59 -04:00
|
|
|
return err
|
2014-09-16 12:58:20 -04:00
|
|
|
}
|
|
|
|
|
2017-06-19 20:15:23 -04:00
|
|
|
imageMount, err := b.imageSources.Get(state.imageID, true)
|
2017-05-14 14:18:48 -04:00
|
|
|
if err != nil {
|
2017-05-25 17:03:29 -04:00
|
|
|
return errors.Wrapf(err, "failed to get destination image %q", state.imageID)
|
2017-05-14 14:18:48 -04:00
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
|
|
|
|
destInfo, err := createDestInfo(state.runConfig.WorkingDir, inst, imageMount, b.platform)
|
2017-05-14 14:18:48 -04:00
|
|
|
if err != nil {
|
2017-05-25 17:03:29 -04:00
|
|
|
return err
|
2017-05-14 14:18:48 -04:00
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
chownPair := b.idMappings.RootPair()
|
2017-07-26 12:05:55 -04:00
|
|
|
// if a chown was requested, perform the steps to get the uid, gid
|
|
|
|
// translated (if necessary because of user namespaces), and replace
|
|
|
|
// the root pair with the chown pair for copy operations
|
|
|
|
if inst.chownStr != "" {
|
2017-08-03 20:22:00 -04:00
|
|
|
chownPair, err = parseChownFlag(inst.chownStr, destInfo.root.Path(), b.idMappings)
|
2017-07-26 12:05:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "unable to convert uid/gid chown string to host mapping")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:37:46 -04:00
|
|
|
for _, info := range inst.infos {
|
2017-08-03 20:22:00 -04:00
|
|
|
opts := copyFileOptions{
|
|
|
|
decompress: inst.allowLocalDecompression,
|
|
|
|
archiver: b.getArchiver(info.root, destInfo.root),
|
|
|
|
chownPair: chownPair,
|
|
|
|
}
|
2017-05-25 17:03:29 -04:00
|
|
|
if err := performCopyForInfo(destInfo, info, opts); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to copy files")
|
2014-09-16 12:58:20 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 17:03:29 -04:00
|
|
|
return b.exportImage(state, imageMount, runConfigWithCommentCmd)
|
|
|
|
}
|
2017-05-14 14:18:48 -04:00
|
|
|
|
2017-07-26 12:05:55 -04:00
|
|
|
func parseChownFlag(chown, ctrRootPath string, idMappings *idtools.IDMappings) (idtools.IDPair, error) {
|
|
|
|
var userStr, grpStr string
|
|
|
|
parts := strings.Split(chown, ":")
|
|
|
|
if len(parts) > 2 {
|
|
|
|
return idtools.IDPair{}, errors.New("invalid chown string format: " + chown)
|
|
|
|
}
|
|
|
|
if len(parts) == 1 {
|
|
|
|
// if no group specified, use the user spec as group as well
|
|
|
|
userStr, grpStr = parts[0], parts[0]
|
|
|
|
} else {
|
|
|
|
userStr, grpStr = parts[0], parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
passwdPath, err := symlink.FollowSymlinkInScope(filepath.Join(ctrRootPath, "etc", "passwd"), ctrRootPath)
|
|
|
|
if err != nil {
|
|
|
|
return idtools.IDPair{}, errors.Wrapf(err, "can't resolve /etc/passwd path in container rootfs")
|
|
|
|
}
|
|
|
|
groupPath, err := symlink.FollowSymlinkInScope(filepath.Join(ctrRootPath, "etc", "group"), ctrRootPath)
|
|
|
|
if err != nil {
|
|
|
|
return idtools.IDPair{}, errors.Wrapf(err, "can't resolve /etc/group path in container rootfs")
|
|
|
|
}
|
|
|
|
uid, err := lookupUser(userStr, passwdPath)
|
|
|
|
if err != nil {
|
|
|
|
return idtools.IDPair{}, errors.Wrapf(err, "can't find uid for user "+userStr)
|
|
|
|
}
|
|
|
|
gid, err := lookupGroup(grpStr, groupPath)
|
|
|
|
if err != nil {
|
|
|
|
return idtools.IDPair{}, errors.Wrapf(err, "can't find gid for group "+grpStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert as necessary because of user namespaces
|
|
|
|
chownPair, err := idMappings.ToHost(idtools.IDPair{UID: uid, GID: gid})
|
|
|
|
if err != nil {
|
|
|
|
return idtools.IDPair{}, errors.Wrapf(err, "unable to convert uid/gid to host mapping")
|
|
|
|
}
|
|
|
|
return chownPair, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupUser(userStr, filepath string) (int, error) {
|
|
|
|
// if the string is actually a uid integer, parse to int and return
|
|
|
|
// as we don't need to translate with the help of files
|
|
|
|
uid, err := strconv.Atoi(userStr)
|
|
|
|
if err == nil {
|
|
|
|
return uid, nil
|
|
|
|
}
|
|
|
|
users, err := lcUser.ParsePasswdFileFilter(filepath, func(u lcUser.User) bool {
|
2017-09-11 14:55:05 -04:00
|
|
|
return u.Name == userStr
|
2017-07-26 12:05:55 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if len(users) == 0 {
|
|
|
|
return 0, errors.New("no such user: " + userStr)
|
|
|
|
}
|
|
|
|
return users[0].Uid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupGroup(groupStr, filepath string) (int, error) {
|
|
|
|
// if the string is actually a gid integer, parse to int and return
|
|
|
|
// as we don't need to translate with the help of files
|
|
|
|
gid, err := strconv.Atoi(groupStr)
|
|
|
|
if err == nil {
|
|
|
|
return gid, nil
|
|
|
|
}
|
|
|
|
groups, err := lcUser.ParseGroupFileFilter(filepath, func(g lcUser.Group) bool {
|
2017-09-11 14:55:05 -04:00
|
|
|
return g.Name == groupStr
|
2017-07-26 12:05:55 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if len(groups) == 0 {
|
|
|
|
return 0, errors.New("no such group: " + groupStr)
|
|
|
|
}
|
|
|
|
return groups[0].Gid, nil
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
func createDestInfo(workingDir string, inst copyInstruction, imageMount *imageMount, platform string) (copyInfo, error) {
|
2017-05-25 17:03:29 -04:00
|
|
|
// Twiddle the destination when it's a relative path - meaning, make it
|
|
|
|
// relative to the WORKINGDIR
|
2017-08-03 20:22:00 -04:00
|
|
|
dest, err := normalizeDest(workingDir, inst.dest, platform)
|
2017-05-25 17:03:29 -04:00
|
|
|
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 14:37:46 -04:00
|
|
|
}
|
2014-09-16 12:58:20 -04:00
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
// normalizeDest normalises the destination of a COPY/ADD command in a
|
|
|
|
// platform semantically consistent way.
|
|
|
|
func normalizeDest(workingDir, requested string, platform string) (string, error) {
|
|
|
|
dest := fromSlash(requested, platform)
|
|
|
|
endsInSlash := strings.HasSuffix(dest, string(separator(platform)))
|
|
|
|
|
|
|
|
if platform != "windows" {
|
|
|
|
if !path.IsAbs(requested) {
|
|
|
|
dest = path.Join("/", filepath.ToSlash(workingDir), dest)
|
|
|
|
// Make sure we preserve any trailing slash
|
|
|
|
if endsInSlash {
|
|
|
|
dest += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are guaranteed that the working directory is already consistent,
|
|
|
|
// However, Windows also has, for now, the limitation that ADD/COPY can
|
|
|
|
// only be done to the system drive, not any drives that might be present
|
|
|
|
// as a result of a bind mount.
|
|
|
|
//
|
|
|
|
// So... if the path requested is Linux-style absolute (/foo or \\foo),
|
|
|
|
// we assume it is the system drive. If it is a Windows-style absolute
|
|
|
|
// (DRIVE:\\foo), error if DRIVE is not C. And finally, ensure we
|
|
|
|
// strip any configured working directories drive letter so that it
|
|
|
|
// can be subsequently legitimately converted to a Windows volume-style
|
|
|
|
// pathname.
|
|
|
|
|
|
|
|
// Not a typo - filepath.IsAbs, not system.IsAbs on this next check as
|
|
|
|
// we only want to validate where the DriveColon part has been supplied.
|
|
|
|
if filepath.IsAbs(dest) {
|
|
|
|
if strings.ToUpper(string(dest[0])) != "C" {
|
|
|
|
return "", fmt.Errorf("Windows does not support destinations not on the system drive (C:)")
|
|
|
|
}
|
|
|
|
dest = dest[2:] // Strip the drive letter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cannot handle relative where WorkingDir is not the system drive.
|
|
|
|
if len(workingDir) > 0 {
|
|
|
|
if ((len(workingDir) > 1) && !system.IsAbs(workingDir[2:])) || (len(workingDir) == 1) {
|
|
|
|
return "", fmt.Errorf("Current WorkingDir %s is not platform consistent", workingDir)
|
|
|
|
}
|
|
|
|
if !system.IsAbs(dest) {
|
|
|
|
if string(workingDir[0]) != "C" {
|
|
|
|
return "", fmt.Errorf("Windows does not support relative paths when WORKDIR is not the system drive")
|
|
|
|
}
|
|
|
|
dest = filepath.Join(string(os.PathSeparator), workingDir[2:], dest)
|
|
|
|
// Make sure we preserve any trailing slash
|
|
|
|
if endsInSlash {
|
|
|
|
dest += string(os.PathSeparator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dest, nil
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:37:46 -04: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 14:11:21 -04: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 15:08:11 -04:00
|
|
|
// withCmdComment sets Cmd to a nop comment string. See withCmdCommentString for
|
|
|
|
// why there are two almost identical versions of this.
|
2017-06-20 18:08:58 -04:00
|
|
|
func withCmdComment(comment string, platform string) runConfigModifier {
|
2017-04-21 14:11:21 -04:00
|
|
|
return func(runConfig *container.Config) {
|
2017-06-20 18:08:58 -04:00
|
|
|
runConfig.Cmd = append(getShell(runConfig, platform), "#(nop) ", comment)
|
2017-04-21 14:11:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:08:11 -04: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.
|
2017-06-20 18:08:58 -04:00
|
|
|
func withCmdCommentString(comment string, platform string) runConfigModifier {
|
2017-04-21 15:08:11 -04:00
|
|
|
return func(runConfig *container.Config) {
|
2017-06-20 18:08:58 -04:00
|
|
|
runConfig.Cmd = append(getShell(runConfig, platform), "#(nop) "+comment)
|
2017-04-21 15:08:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 14:11:21 -04:00
|
|
|
func withEnv(env []string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Env = env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:21:43 -04: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 14:11:21 -04:00
|
|
|
// getShell is a helper function which gets the right shell for prefixing the
|
|
|
|
// shell-form of RUN, ENTRYPOINT and CMD instructions
|
2017-06-20 18:08:58 -04:00
|
|
|
func getShell(c *container.Config, platform string) []string {
|
2017-04-21 14:11:21 -04:00
|
|
|
if 0 == len(c.Shell) {
|
2017-06-20 18:08:58 -04:00
|
|
|
return append([]string{}, defaultShellForPlatform(platform)[:]...)
|
2017-04-21 14:11:21 -04:00
|
|
|
}
|
|
|
|
return append([]string{}, c.Shell[:]...)
|
2014-09-16 12:58:20 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
func (b *Builder) probeCache(dispatchState *dispatchState, runConfig *container.Config) (bool, error) {
|
2017-04-13 18:44:36 -04:00
|
|
|
cachedID, err := b.imageProber.Probe(dispatchState.imageID, runConfig)
|
|
|
|
if cachedID == "" || err != nil {
|
2015-02-25 13:27:32 -05:00
|
|
|
return false, err
|
|
|
|
}
|
2016-12-25 01:37:31 -05:00
|
|
|
fmt.Fprint(b.Stdout, " ---> Using cache\n")
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2017-08-24 13:11:44 -04:00
|
|
|
dispatchState.imageID = cachedID
|
2017-05-14 14:18:48 -04:00
|
|
|
b.buildStages.update(dispatchState.imageID)
|
2015-02-25 13:27:32 -05:00
|
|
|
return true, nil
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-04-13 18:44:36 -04:00
|
|
|
var defaultLogConfig = container.LogConfig{Type: "none"}
|
2015-11-18 14:03:08 -05:00
|
|
|
|
2017-04-13 18:44:36 -04: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 09:35:53 -05:00
|
|
|
return "", err
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2017-04-13 18:44:36 -04:00
|
|
|
// Set a log config to override any default value set on the daemon
|
|
|
|
hostConfig := &container.HostConfig{LogConfig: defaultLogConfig}
|
2017-05-17 20:08:01 -04:00
|
|
|
container, err := b.containerManager.Create(runConfig, hostConfig, b.platform)
|
2017-04-13 18:44:36 -04:00
|
|
|
return container.ID, err
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-04-13 18:44:36 -04:00
|
|
|
func (b *Builder) create(runConfig *container.Config) (string, error) {
|
|
|
|
hostConfig := hostConfigFromOptions(b.options)
|
2017-05-17 20:08:01 -04:00
|
|
|
container, err := b.containerManager.Create(runConfig, hostConfig, b.platform)
|
2017-03-30 16:52:40 -04:00
|
|
|
if err != nil {
|
2017-04-13 18:44:36 -04:00
|
|
|
return "", err
|
2015-10-16 05:18:10 -04:00
|
|
|
}
|
2017-04-13 18:44:36 -04: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 05:18:10 -04:00
|
|
|
}
|
2017-04-13 18:44:36 -04:00
|
|
|
fmt.Fprintf(b.Stdout, " ---> Running in %s\n", stringid.TruncateID(container.ID))
|
|
|
|
return container.ID, nil
|
2015-10-16 05:18:10 -04:00
|
|
|
}
|
|
|
|
|
2017-04-13 18:44:36 -04: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 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
|
|
|
|
// fromSlash works like filepath.FromSlash but with a given OS platform field
|
|
|
|
func fromSlash(path, platform string) string {
|
|
|
|
if platform == "windows" {
|
|
|
|
return strings.Replace(path, "/", "\\", -1)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
// separator returns a OS path separator for the given OS platform
|
|
|
|
func separator(platform string) byte {
|
|
|
|
if platform == "windows" {
|
|
|
|
return '\\'
|
|
|
|
}
|
|
|
|
return '/'
|
|
|
|
}
|