2018-02-05 21:05:59 +00:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/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 (
|
2022-10-26 16:13:17 +00:00
|
|
|
"context"
|
2014-08-05 22:41:09 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
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"
|
2018-02-16 21:50:57 +00:00
|
|
|
"github.com/docker/docker/builder"
|
2017-05-14 18:18:48 +00:00
|
|
|
"github.com/docker/docker/image"
|
2017-08-04 00:22:00 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2015-03-24 11:25:26 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-11-09 21:20:51 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2023-05-08 09:57:52 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-03-15 22:28:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2014-08-05 22:41:09 +00:00
|
|
|
)
|
|
|
|
|
2022-09-23 00:32:37 +00:00
|
|
|
func (b *Builder) getArchiver() *archive.Archiver {
|
|
|
|
return chrootarchive.NewArchiver(b.idMapping)
|
2017-08-04 00:22:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:13:17 +00:00
|
|
|
func (b *Builder) commit(ctx context.Context, 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
|
|
|
|
2018-02-05 22:41:45 +00:00
|
|
|
runConfigWithCommentCmd := copyRunConfig(dispatchState.runConfig, withCmdComment(comment, dispatchState.operatingSystem))
|
2022-10-26 16:13:17 +00:00
|
|
|
id, err := b.probeAndCreate(ctx, dispatchState, runConfigWithCommentCmd)
|
2018-06-01 07:21:29 +00:00
|
|
|
if err != nil || id == "" {
|
2017-04-21 18:11:21 +00:00
|
|
|
return err
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
|
2023-01-12 14:14:12 +00:00
|
|
|
return b.commitContainer(ctx, dispatchState, id, runConfigWithCommentCmd)
|
2017-04-21 18:11:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:14:12 +00:00
|
|
|
func (b *Builder) commitContainer(ctx context.Context, 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
|
|
|
|
2018-02-06 18:27:55 +00:00
|
|
|
commitCfg := backend.CommitConfig{
|
|
|
|
Author: dispatchState.maintainer,
|
|
|
|
// TODO: this copy should be done by Commit()
|
|
|
|
Config: copyRunConfig(dispatchState.runConfig),
|
2017-04-21 19:08:11 +00:00
|
|
|
ContainerConfig: containerConfig,
|
2018-02-06 18:27:55 +00:00
|
|
|
ContainerID: id,
|
2015-06-20 10:40:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:14:12 +00:00
|
|
|
imageID, err := b.docker.CommitBuildStep(ctx, commitCfg)
|
2018-02-06 18:27:55 +00:00
|
|
|
dispatchState.imageID = string(imageID)
|
|
|
|
return err
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 12:19:51 +00:00
|
|
|
func (b *Builder) exportImage(ctx context.Context, state *dispatchState, layer builder.RWLayer, parent builder.Image, runConfig *container.Config) error {
|
2018-02-16 21:50:57 +00:00
|
|
|
newLayer, err := layer.Commit()
|
2017-05-25 21:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-16 21:50:57 +00:00
|
|
|
parentImage, ok := parent.(*image.Image)
|
2017-05-25 21:03:29 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("unexpected image type")
|
|
|
|
}
|
|
|
|
|
2023-05-08 09:57:52 +00:00
|
|
|
platform := &ocispec.Platform{
|
2019-04-26 22:12:43 +00:00
|
|
|
OS: parentImage.OS,
|
|
|
|
Architecture: parentImage.Architecture,
|
|
|
|
Variant: parentImage.Variant,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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), platform)
|
|
|
|
|
2017-05-25 21:03:29 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-04-13 12:19:51 +00:00
|
|
|
// when writing the new image's manifest, we now need to pass in the new layer's digest.
|
|
|
|
// before the containerd store work this was unnecessary since we get the layer id
|
|
|
|
// from the image's RootFS ChainID -- see:
|
|
|
|
// https://github.com/moby/moby/blob/8cf66ed7322fa885ef99c4c044fa23e1727301dc/image/store.go#L162
|
|
|
|
// however, with the containerd store we can't do this. An alternative implementation here
|
|
|
|
// without changing the signature would be to get the layer digest by walking the content store
|
|
|
|
// and filtering the objects to find the layer with the DiffID we want, but that has performance
|
|
|
|
// implications that should be called out/investigated
|
|
|
|
exportedImage, err := b.docker.CreateImage(ctx, config, state.imageID, newLayer.ContentStoreDigest())
|
2017-05-25 21:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to export image")
|
|
|
|
}
|
|
|
|
|
|
|
|
state.imageID = exportedImage.ImageID()
|
2019-04-26 22:12:43 +00:00
|
|
|
b.imageSources.Add(newImageMount(exportedImage, newLayer), platform)
|
2017-05-25 21:03:29 +00:00
|
|
|
return nil
|
2017-05-14 18:18:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:13:17 +00:00
|
|
|
func (b *Builder) performCopy(ctx context.Context, req dispatchRequest, inst copyInstruction) error {
|
2018-06-26 21:49:33 +00:00
|
|
|
state := req.state
|
2017-05-07 18:37:46 +00:00
|
|
|
srcHash := getSourceHashFromInfos(inst.infos)
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2017-07-26 16:05:55 +00:00
|
|
|
var chownComment string
|
|
|
|
if inst.chownStr != "" {
|
2023-05-05 10:18:10 +00:00
|
|
|
chownComment = fmt.Sprintf("--chown=%s ", inst.chownStr)
|
2017-07-26 16:05:55 +00:00
|
|
|
}
|
|
|
|
commentStr := fmt.Sprintf("%s %s%s in %s ", inst.cmdName, chownComment, srcHash, inst.dest)
|
|
|
|
|
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,
|
2018-02-05 22:41:45 +00:00
|
|
|
withCmdCommentString(commentStr, state.operatingSystem))
|
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
|
|
|
}
|
|
|
|
|
2022-10-28 11:40:06 +00:00
|
|
|
imageMount, err := b.imageSources.Get(ctx, state.imageID, true, req.builder.platform)
|
2017-05-14 18:18:48 +00:00
|
|
|
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-08-04 00:22:00 +00:00
|
|
|
|
2018-02-16 21:50:57 +00:00
|
|
|
rwLayer, err := imageMount.NewRWLayer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rwLayer.Release()
|
|
|
|
|
2018-02-05 22:41:45 +00:00
|
|
|
destInfo, err := createDestInfo(state.runConfig.WorkingDir, inst, rwLayer, state.operatingSystem)
|
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
|
|
|
}
|
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
identity := b.idMapping.RootPair()
|
2017-07-26 16:05:55 +00: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 != "" {
|
2022-10-26 16:13:17 +00:00
|
|
|
identity, err = parseChownFlag(ctx, b, state, inst.chownStr, destInfo.root, b.idMapping)
|
2017-07-26 16:05:55 +00:00
|
|
|
if err != nil {
|
2017-11-16 06:20:33 +00:00
|
|
|
if b.options.Platform != "windows" {
|
|
|
|
return errors.Wrapf(err, "unable to convert uid/gid chown string to host mapping")
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrapf(err, "unable to map container user account name to SID")
|
2017-07-26 16:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 18:37:46 +00:00
|
|
|
for _, info := range inst.infos {
|
2017-08-04 00:22:00 +00:00
|
|
|
opts := copyFileOptions{
|
|
|
|
decompress: inst.allowLocalDecompression,
|
2022-09-23 00:32:37 +00:00
|
|
|
archiver: b.getArchiver(),
|
2018-12-18 21:30:08 +00:00
|
|
|
}
|
|
|
|
if !inst.preserveOwnership {
|
|
|
|
opts.identity = &identity
|
2017-08-04 00:22:00 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2023-04-13 12:19:51 +00:00
|
|
|
return b.exportImage(ctx, state, rwLayer, imageMount.Image(), runConfigWithCommentCmd)
|
2017-05-25 21:03:29 +00:00
|
|
|
}
|
2017-05-14 18:18:48 +00:00
|
|
|
|
2018-02-16 21:50:57 +00:00
|
|
|
func createDestInfo(workingDir string, inst copyInstruction, rwLayer builder.RWLayer, platform string) (copyInfo, error) {
|
2017-05-25 21:03:29 +00:00
|
|
|
// Twiddle the destination when it's a relative path - meaning, make it
|
|
|
|
// relative to the WORKINGDIR
|
2021-07-26 19:08:06 +00:00
|
|
|
dest, err := normalizeDest(workingDir, inst.dest)
|
2017-05-25 21:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return copyInfo{}, errors.Wrapf(err, "invalid %s", inst.cmdName)
|
|
|
|
}
|
|
|
|
|
2018-02-16 21:50:57 +00:00
|
|
|
return copyInfo{root: rwLayer.Root(), path: 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 withCmd(cmd []string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Cmd = cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Windows: (WCOW) Generate OCI spec that remote runtime can escape
Signed-off-by: John Howard <jhoward@microsoft.com>
Also fixes https://github.com/moby/moby/issues/22874
This commit is a pre-requisite to moving moby/moby on Windows to using
Containerd for its runtime.
The reason for this is that the interface between moby and containerd
for the runtime is an OCI spec which must be unambigious.
It is the responsibility of the runtime (runhcs in the case of
containerd on Windows) to ensure that arguments are escaped prior
to calling into HCS and onwards to the Win32 CreateProcess call.
Previously, the builder was always escaping arguments which has
led to several bugs in moby. Because the local runtime in
libcontainerd had context of whether or not arguments were escaped,
it was possible to hack around in daemon/oci_windows.go with
knowledge of the context of the call (from builder or not).
With a remote runtime, this is not possible as there's rightly
no context of the caller passed across in the OCI spec. Put another
way, as I put above, the OCI spec must be unambigious.
The other previous limitation (which leads to various subtle bugs)
is that moby is coded entirely from a Linux-centric point of view.
Unfortunately, Windows != Linux. Windows CreateProcess uses a
command line, not an array of arguments. And it has very specific
rules about how to escape a command line. Some interesting reading
links about this are:
https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
https://stackoverflow.com/questions/31838469/how-do-i-convert-argv-to-lpcommandline-parameter-of-createprocess
https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017
For this reason, the OCI spec has recently been updated to cater
for more natural syntax by including a CommandLine option in
Process.
What does this commit do?
Primary objective is to ensure that the built OCI spec is unambigious.
It changes the builder so that `ArgsEscaped` as commited in a
layer is only controlled by the use of CMD or ENTRYPOINT.
Subsequently, when calling in to create a container from the builder,
if follows a different path to both `docker run` and `docker create`
using the added `ContainerCreateIgnoreImagesArgsEscaped`. This allows
a RUN from the builder to control how to escape in the OCI spec.
It changes the builder so that when shell form is used for RUN,
CMD or ENTRYPOINT, it builds (for WCOW) a more natural command line
using the original as put by the user in the dockerfile, not
the parsed version as a set of args which loses fidelity.
This command line is put into args[0] and `ArgsEscaped` is set
to true for CMD or ENTRYPOINT. A RUN statement does not commit
`ArgsEscaped` to the commited layer regardless or whether shell
or exec form were used.
2019-01-18 00:03:29 +00:00
|
|
|
func withArgsEscaped(argsEscaped bool) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.ArgsEscaped = argsEscaped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-20 22:08:58 +00:00
|
|
|
func withCmdComment(comment string, platform string) runConfigModifier {
|
2017-04-21 18:11:21 +00:00
|
|
|
return func(runConfig *container.Config) {
|
2017-06-20 22:08:58 +00:00
|
|
|
runConfig.Cmd = append(getShell(runConfig, platform), "#(nop) ", comment)
|
2017-04-21 18:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2017-06-20 22:08:58 +00:00
|
|
|
func withCmdCommentString(comment string, platform string) runConfigModifier {
|
2017-04-21 19:08:11 +00:00
|
|
|
return func(runConfig *container.Config) {
|
2017-06-20 22:08:58 +00:00
|
|
|
runConfig.Cmd = append(getShell(runConfig, platform), "#(nop) "+comment)
|
2017-04-21 19:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 00:36:50 +00:00
|
|
|
// withoutHealthcheck disables healthcheck.
|
|
|
|
//
|
|
|
|
// The dockerfile RUN instruction expect to run without healthcheck
|
|
|
|
// so the runConfig Healthcheck needs to be disabled.
|
|
|
|
func withoutHealthcheck() runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Healthcheck = &container.HealthConfig{
|
|
|
|
Test: []string{"NONE"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 21:20:51 +00:00
|
|
|
func copyRunConfig(runConfig *container.Config, modifiers ...runConfigModifier) *container.Config {
|
|
|
|
copy := *runConfig
|
|
|
|
copy.Cmd = copyStringSlice(runConfig.Cmd)
|
|
|
|
copy.Env = copyStringSlice(runConfig.Env)
|
|
|
|
copy.Entrypoint = copyStringSlice(runConfig.Entrypoint)
|
|
|
|
copy.OnBuild = copyStringSlice(runConfig.OnBuild)
|
|
|
|
copy.Shell = copyStringSlice(runConfig.Shell)
|
|
|
|
|
|
|
|
if copy.Volumes != nil {
|
|
|
|
copy.Volumes = make(map[string]struct{}, len(runConfig.Volumes))
|
|
|
|
for k, v := range runConfig.Volumes {
|
|
|
|
copy.Volumes[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if copy.ExposedPorts != nil {
|
|
|
|
copy.ExposedPorts = make(nat.PortSet, len(runConfig.ExposedPorts))
|
|
|
|
for k, v := range runConfig.ExposedPorts {
|
|
|
|
copy.ExposedPorts[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if copy.Labels != nil {
|
|
|
|
copy.Labels = make(map[string]string, len(runConfig.Labels))
|
|
|
|
for k, v := range runConfig.Labels {
|
|
|
|
copy.Labels[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, modifier := range modifiers {
|
|
|
|
modifier(©)
|
|
|
|
}
|
|
|
|
return ©
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyStringSlice(orig []string) []string {
|
|
|
|
if orig == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return append([]string{}, orig...)
|
|
|
|
}
|
|
|
|
|
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
|
2017-08-08 19:43:48 +00:00
|
|
|
func getShell(c *container.Config, os string) []string {
|
2017-04-21 18:11:21 +00:00
|
|
|
if 0 == len(c.Shell) {
|
2017-08-08 19:43:48 +00:00
|
|
|
return append([]string{}, defaultShellForOS(os)[:]...)
|
2017-04-21 18:11:21 +00:00
|
|
|
}
|
|
|
|
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-08-24 17:11:44 +00:00
|
|
|
dispatchState.imageID = cachedID
|
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
|
|
|
|
2022-10-26 16:13:17 +00:00
|
|
|
func (b *Builder) probeAndCreate(ctx context.Context, dispatchState *dispatchState, runConfig *container.Config) (string, error) {
|
2017-04-13 22:44:36 +00:00
|
|
|
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
|
|
|
}
|
2022-10-26 16:13:17 +00:00
|
|
|
return b.create(ctx, runConfig)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:13:17 +00:00
|
|
|
func (b *Builder) create(ctx context.Context, runConfig *container.Config) (string, error) {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).Debugf("[BUILDER] Command to be executed: %v", runConfig.Cmd)
|
2018-07-03 02:31:05 +00:00
|
|
|
|
2020-11-06 08:51:44 +00:00
|
|
|
hostConfig := hostConfigFromOptions(b.options)
|
2022-10-26 16:13:17 +00:00
|
|
|
container, err := b.containerManager.Create(ctx, 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
|
|
|
}
|
|
|
|
|
2020-11-06 08:51:44 +00:00
|
|
|
func hostConfigFromOptions(options *types.ImageBuildOptions) *container.HostConfig {
|
2017-04-13 22:44:36 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2018-01-03 23:59:06 +00:00
|
|
|
hc := &container.HostConfig{
|
2017-04-13 22:44:36 +00:00
|
|
|
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
|
|
|
}
|
2018-01-03 23:59:06 +00:00
|
|
|
return hc
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|