github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/builder/dockerfile/internals.go (about)

     1  package dockerfile // import "github.com/docker/docker/builder/dockerfile"
     2  
     3  // internals for handling commands. Covers many areas and a lot of
     4  // non-contiguous functionality. Please read the comments.
     5  
     6  import (
     7  	"context"
     8  	"crypto/sha256"
     9  	"encoding/hex"
    10  	"fmt"
    11  	"strings"
    12  
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/api/types/backend"
    15  	"github.com/docker/docker/api/types/container"
    16  	"github.com/docker/docker/builder"
    17  	"github.com/docker/docker/image"
    18  	"github.com/docker/docker/pkg/archive"
    19  	"github.com/docker/docker/pkg/chrootarchive"
    20  	"github.com/docker/docker/pkg/stringid"
    21  	"github.com/docker/go-connections/nat"
    22  	specs "github.com/opencontainers/image-spec/specs-go/v1"
    23  	"github.com/pkg/errors"
    24  	"github.com/sirupsen/logrus"
    25  )
    26  
    27  func (b *Builder) getArchiver() *archive.Archiver {
    28  	return chrootarchive.NewArchiver(b.idMapping)
    29  }
    30  
    31  func (b *Builder) commit(ctx context.Context, dispatchState *dispatchState, comment string) error {
    32  	if b.disableCommit {
    33  		return nil
    34  	}
    35  	if !dispatchState.hasFromImage() {
    36  		return errors.New("Please provide a source image with `from` prior to commit")
    37  	}
    38  
    39  	runConfigWithCommentCmd := copyRunConfig(dispatchState.runConfig, withCmdComment(comment, dispatchState.operatingSystem))
    40  	id, err := b.probeAndCreate(ctx, dispatchState, runConfigWithCommentCmd)
    41  	if err != nil || id == "" {
    42  		return err
    43  	}
    44  
    45  	return b.commitContainer(dispatchState, id, runConfigWithCommentCmd)
    46  }
    47  
    48  func (b *Builder) commitContainer(dispatchState *dispatchState, id string, containerConfig *container.Config) error {
    49  	if b.disableCommit {
    50  		return nil
    51  	}
    52  
    53  	commitCfg := backend.CommitConfig{
    54  		Author: dispatchState.maintainer,
    55  		// TODO: this copy should be done by Commit()
    56  		Config:          copyRunConfig(dispatchState.runConfig),
    57  		ContainerConfig: containerConfig,
    58  		ContainerID:     id,
    59  	}
    60  
    61  	imageID, err := b.docker.CommitBuildStep(commitCfg)
    62  	dispatchState.imageID = string(imageID)
    63  	return err
    64  }
    65  
    66  func (b *Builder) exportImage(state *dispatchState, layer builder.RWLayer, parent builder.Image, runConfig *container.Config) error {
    67  	newLayer, err := layer.Commit()
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	parentImage, ok := parent.(*image.Image)
    73  	if !ok {
    74  		return errors.Errorf("unexpected image type")
    75  	}
    76  
    77  	platform := &specs.Platform{
    78  		OS:           parentImage.OS,
    79  		Architecture: parentImage.Architecture,
    80  		Variant:      parentImage.Variant,
    81  	}
    82  
    83  	// add an image mount without an image so the layer is properly unmounted
    84  	// if there is an error before we can add the full mount with image
    85  	b.imageSources.Add(newImageMount(nil, newLayer), platform)
    86  
    87  	newImage := image.NewChildImage(parentImage, image.ChildConfig{
    88  		Author:          state.maintainer,
    89  		ContainerConfig: runConfig,
    90  		DiffID:          newLayer.DiffID(),
    91  		Config:          copyRunConfig(state.runConfig),
    92  	}, parentImage.OS)
    93  
    94  	// TODO: it seems strange to marshal this here instead of just passing in the
    95  	// image struct
    96  	config, err := newImage.MarshalJSON()
    97  	if err != nil {
    98  		return errors.Wrap(err, "failed to encode image config")
    99  	}
   100  
   101  	exportedImage, err := b.docker.CreateImage(config, state.imageID)
   102  	if err != nil {
   103  		return errors.Wrapf(err, "failed to export image")
   104  	}
   105  
   106  	state.imageID = exportedImage.ImageID()
   107  	b.imageSources.Add(newImageMount(exportedImage, newLayer), platform)
   108  	return nil
   109  }
   110  
   111  func (b *Builder) performCopy(ctx context.Context, req dispatchRequest, inst copyInstruction) error {
   112  	state := req.state
   113  	srcHash := getSourceHashFromInfos(inst.infos)
   114  
   115  	var chownComment string
   116  	if inst.chownStr != "" {
   117  		chownComment = fmt.Sprintf("--chown=%s", inst.chownStr)
   118  	}
   119  	commentStr := fmt.Sprintf("%s %s%s in %s ", inst.cmdName, chownComment, srcHash, inst.dest)
   120  
   121  	// TODO: should this have been using origPaths instead of srcHash in the comment?
   122  	runConfigWithCommentCmd := copyRunConfig(
   123  		state.runConfig,
   124  		withCmdCommentString(commentStr, state.operatingSystem))
   125  	hit, err := b.probeCache(state, runConfigWithCommentCmd)
   126  	if err != nil || hit {
   127  		return err
   128  	}
   129  
   130  	imageMount, err := b.imageSources.Get(ctx, state.imageID, true, req.builder.platform)
   131  	if err != nil {
   132  		return errors.Wrapf(err, "failed to get destination image %q", state.imageID)
   133  	}
   134  
   135  	rwLayer, err := imageMount.NewRWLayer()
   136  	if err != nil {
   137  		return err
   138  	}
   139  	defer rwLayer.Release()
   140  
   141  	destInfo, err := createDestInfo(state.runConfig.WorkingDir, inst, rwLayer, state.operatingSystem)
   142  	if err != nil {
   143  		return err
   144  	}
   145  
   146  	identity := b.idMapping.RootPair()
   147  	// if a chown was requested, perform the steps to get the uid, gid
   148  	// translated (if necessary because of user namespaces), and replace
   149  	// the root pair with the chown pair for copy operations
   150  	if inst.chownStr != "" {
   151  		identity, err = parseChownFlag(ctx, b, state, inst.chownStr, destInfo.root, b.idMapping)
   152  		if err != nil {
   153  			if b.options.Platform != "windows" {
   154  				return errors.Wrapf(err, "unable to convert uid/gid chown string to host mapping")
   155  			}
   156  
   157  			return errors.Wrapf(err, "unable to map container user account name to SID")
   158  		}
   159  	}
   160  
   161  	for _, info := range inst.infos {
   162  		opts := copyFileOptions{
   163  			decompress: inst.allowLocalDecompression,
   164  			archiver:   b.getArchiver(),
   165  		}
   166  		if !inst.preserveOwnership {
   167  			opts.identity = &identity
   168  		}
   169  		if err := performCopyForInfo(destInfo, info, opts); err != nil {
   170  			return errors.Wrapf(err, "failed to copy files")
   171  		}
   172  	}
   173  	return b.exportImage(state, rwLayer, imageMount.Image(), runConfigWithCommentCmd)
   174  }
   175  
   176  func createDestInfo(workingDir string, inst copyInstruction, rwLayer builder.RWLayer, platform string) (copyInfo, error) {
   177  	// Twiddle the destination when it's a relative path - meaning, make it
   178  	// relative to the WORKINGDIR
   179  	dest, err := normalizeDest(workingDir, inst.dest)
   180  	if err != nil {
   181  		return copyInfo{}, errors.Wrapf(err, "invalid %s", inst.cmdName)
   182  	}
   183  
   184  	return copyInfo{root: rwLayer.Root(), path: dest}, nil
   185  }
   186  
   187  // For backwards compat, if there's just one info then use it as the
   188  // cache look-up string, otherwise hash 'em all into one
   189  func getSourceHashFromInfos(infos []copyInfo) string {
   190  	if len(infos) == 1 {
   191  		return infos[0].hash
   192  	}
   193  	var hashs []string
   194  	for _, info := range infos {
   195  		hashs = append(hashs, info.hash)
   196  	}
   197  	return hashStringSlice("multi", hashs)
   198  }
   199  
   200  func hashStringSlice(prefix string, slice []string) string {
   201  	hasher := sha256.New()
   202  	hasher.Write([]byte(strings.Join(slice, ",")))
   203  	return prefix + ":" + hex.EncodeToString(hasher.Sum(nil))
   204  }
   205  
   206  type runConfigModifier func(*container.Config)
   207  
   208  func withCmd(cmd []string) runConfigModifier {
   209  	return func(runConfig *container.Config) {
   210  		runConfig.Cmd = cmd
   211  	}
   212  }
   213  
   214  func withArgsEscaped(argsEscaped bool) runConfigModifier {
   215  	return func(runConfig *container.Config) {
   216  		runConfig.ArgsEscaped = argsEscaped
   217  	}
   218  }
   219  
   220  // withCmdComment sets Cmd to a nop comment string. See withCmdCommentString for
   221  // why there are two almost identical versions of this.
   222  func withCmdComment(comment string, platform string) runConfigModifier {
   223  	return func(runConfig *container.Config) {
   224  		runConfig.Cmd = append(getShell(runConfig, platform), "#(nop) ", comment)
   225  	}
   226  }
   227  
   228  // withCmdCommentString exists to maintain compatibility with older versions.
   229  // A few instructions (workdir, copy, add) used a nop comment that is a single arg
   230  // where as all the other instructions used a two arg comment string. This
   231  // function implements the single arg version.
   232  func withCmdCommentString(comment string, platform string) runConfigModifier {
   233  	return func(runConfig *container.Config) {
   234  		runConfig.Cmd = append(getShell(runConfig, platform), "#(nop) "+comment)
   235  	}
   236  }
   237  
   238  func withEnv(env []string) runConfigModifier {
   239  	return func(runConfig *container.Config) {
   240  		runConfig.Env = env
   241  	}
   242  }
   243  
   244  // withEntrypointOverride sets an entrypoint on runConfig if the command is
   245  // not empty. The entrypoint is left unmodified if command is empty.
   246  //
   247  // The dockerfile RUN instruction expect to run without an entrypoint
   248  // so the runConfig entrypoint needs to be modified accordingly. ContainerCreate
   249  // will change a []string{""} entrypoint to nil, so we probe the cache with the
   250  // nil entrypoint.
   251  func withEntrypointOverride(cmd []string, entrypoint []string) runConfigModifier {
   252  	return func(runConfig *container.Config) {
   253  		if len(cmd) > 0 {
   254  			runConfig.Entrypoint = entrypoint
   255  		}
   256  	}
   257  }
   258  
   259  // withoutHealthcheck disables healthcheck.
   260  //
   261  // The dockerfile RUN instruction expect to run without healthcheck
   262  // so the runConfig Healthcheck needs to be disabled.
   263  func withoutHealthcheck() runConfigModifier {
   264  	return func(runConfig *container.Config) {
   265  		runConfig.Healthcheck = &container.HealthConfig{
   266  			Test: []string{"NONE"},
   267  		}
   268  	}
   269  }
   270  
   271  func copyRunConfig(runConfig *container.Config, modifiers ...runConfigModifier) *container.Config {
   272  	copy := *runConfig
   273  	copy.Cmd = copyStringSlice(runConfig.Cmd)
   274  	copy.Env = copyStringSlice(runConfig.Env)
   275  	copy.Entrypoint = copyStringSlice(runConfig.Entrypoint)
   276  	copy.OnBuild = copyStringSlice(runConfig.OnBuild)
   277  	copy.Shell = copyStringSlice(runConfig.Shell)
   278  
   279  	if copy.Volumes != nil {
   280  		copy.Volumes = make(map[string]struct{}, len(runConfig.Volumes))
   281  		for k, v := range runConfig.Volumes {
   282  			copy.Volumes[k] = v
   283  		}
   284  	}
   285  
   286  	if copy.ExposedPorts != nil {
   287  		copy.ExposedPorts = make(nat.PortSet, len(runConfig.ExposedPorts))
   288  		for k, v := range runConfig.ExposedPorts {
   289  			copy.ExposedPorts[k] = v
   290  		}
   291  	}
   292  
   293  	if copy.Labels != nil {
   294  		copy.Labels = make(map[string]string, len(runConfig.Labels))
   295  		for k, v := range runConfig.Labels {
   296  			copy.Labels[k] = v
   297  		}
   298  	}
   299  
   300  	for _, modifier := range modifiers {
   301  		modifier(&copy)
   302  	}
   303  	return &copy
   304  }
   305  
   306  func copyStringSlice(orig []string) []string {
   307  	if orig == nil {
   308  		return nil
   309  	}
   310  	return append([]string{}, orig...)
   311  }
   312  
   313  // getShell is a helper function which gets the right shell for prefixing the
   314  // shell-form of RUN, ENTRYPOINT and CMD instructions
   315  func getShell(c *container.Config, os string) []string {
   316  	if 0 == len(c.Shell) {
   317  		return append([]string{}, defaultShellForOS(os)[:]...)
   318  	}
   319  	return append([]string{}, c.Shell[:]...)
   320  }
   321  
   322  func (b *Builder) probeCache(dispatchState *dispatchState, runConfig *container.Config) (bool, error) {
   323  	cachedID, err := b.imageProber.Probe(dispatchState.imageID, runConfig)
   324  	if cachedID == "" || err != nil {
   325  		return false, err
   326  	}
   327  	fmt.Fprint(b.Stdout, " ---> Using cache\n")
   328  
   329  	dispatchState.imageID = cachedID
   330  	return true, nil
   331  }
   332  
   333  var defaultLogConfig = container.LogConfig{Type: "none"}
   334  
   335  func (b *Builder) probeAndCreate(ctx context.Context, dispatchState *dispatchState, runConfig *container.Config) (string, error) {
   336  	if hit, err := b.probeCache(dispatchState, runConfig); err != nil || hit {
   337  		return "", err
   338  	}
   339  	return b.create(ctx, runConfig)
   340  }
   341  
   342  func (b *Builder) create(ctx context.Context, runConfig *container.Config) (string, error) {
   343  	logrus.Debugf("[BUILDER] Command to be executed: %v", runConfig.Cmd)
   344  
   345  	hostConfig := hostConfigFromOptions(b.options)
   346  	container, err := b.containerManager.Create(ctx, runConfig, hostConfig)
   347  	if err != nil {
   348  		return "", err
   349  	}
   350  	// TODO: could this be moved into containerManager.Create() ?
   351  	for _, warning := range container.Warnings {
   352  		fmt.Fprintf(b.Stdout, " ---> [Warning] %s\n", warning)
   353  	}
   354  	fmt.Fprintf(b.Stdout, " ---> Running in %s\n", stringid.TruncateID(container.ID))
   355  	return container.ID, nil
   356  }
   357  
   358  func hostConfigFromOptions(options *types.ImageBuildOptions) *container.HostConfig {
   359  	resources := container.Resources{
   360  		CgroupParent: options.CgroupParent,
   361  		CPUShares:    options.CPUShares,
   362  		CPUPeriod:    options.CPUPeriod,
   363  		CPUQuota:     options.CPUQuota,
   364  		CpusetCpus:   options.CPUSetCPUs,
   365  		CpusetMems:   options.CPUSetMems,
   366  		Memory:       options.Memory,
   367  		MemorySwap:   options.MemorySwap,
   368  		Ulimits:      options.Ulimits,
   369  	}
   370  
   371  	hc := &container.HostConfig{
   372  		SecurityOpt: options.SecurityOpt,
   373  		Isolation:   options.Isolation,
   374  		ShmSize:     options.ShmSize,
   375  		Resources:   resources,
   376  		NetworkMode: container.NetworkMode(options.NetworkMode),
   377  		// Set a log config to override any default value set on the daemon
   378  		LogConfig:  defaultLogConfig,
   379  		ExtraHosts: options.ExtraHosts,
   380  	}
   381  	return hc
   382  }