mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
![Daniel Nephin](/assets/img/avatar_default.png)
Extract a common function for builder.createContainer Extract imageCache for doing cache probes Removes the cacheBuested field from Builder Create a new containerManager class which reduces the interface between the builder and managing containers to 3 functions (from 6) Signed-off-by: Daniel Nephin <dnephin@docker.com>
238 lines
7.4 KiB
Go
238 lines
7.4 KiB
Go
package dockerfile
|
|
|
|
// internals for handling commands. Covers many areas and a lot of
|
|
// non-contiguous functionality. Please read the comments.
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/backend"
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/pkg/stringid"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (b *Builder) commit(dispatchState *dispatchState, comment string) error {
|
|
if b.disableCommit {
|
|
return nil
|
|
}
|
|
if !dispatchState.hasFromImage() {
|
|
return errors.New("Please provide a source image with `from` prior to commit")
|
|
}
|
|
|
|
runConfigWithCommentCmd := copyRunConfig(dispatchState.runConfig, withCmdComment(comment))
|
|
hit, err := b.probeCache(dispatchState, runConfigWithCommentCmd)
|
|
if err != nil || hit {
|
|
return err
|
|
}
|
|
id, err := b.create(runConfigWithCommentCmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return b.commitContainer(dispatchState, id, runConfigWithCommentCmd)
|
|
}
|
|
|
|
// TODO: see if any args can be dropped
|
|
func (b *Builder) commitContainer(dispatchState *dispatchState, id string, containerConfig *container.Config) error {
|
|
if b.disableCommit {
|
|
return nil
|
|
}
|
|
|
|
commitCfg := &backend.ContainerCommitConfig{
|
|
ContainerCommitConfig: types.ContainerCommitConfig{
|
|
Author: dispatchState.maintainer,
|
|
Pause: true,
|
|
// TODO: this should be done by Commit()
|
|
Config: copyRunConfig(dispatchState.runConfig),
|
|
},
|
|
ContainerConfig: containerConfig,
|
|
}
|
|
|
|
// Commit the container
|
|
imageID, err := b.docker.Commit(id, commitCfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dispatchState.imageID = imageID
|
|
b.buildStages.update(imageID, dispatchState.runConfig)
|
|
return nil
|
|
}
|
|
|
|
func (b *Builder) performCopy(state *dispatchState, inst copyInstruction) error {
|
|
srcHash := getSourceHashFromInfos(inst.infos)
|
|
|
|
// TODO: should this have been using origPaths instead of srcHash in the comment?
|
|
runConfigWithCommentCmd := copyRunConfig(
|
|
state.runConfig,
|
|
withCmdCommentString(fmt.Sprintf("%s %s in %s ", inst.cmdName, srcHash, inst.dest)))
|
|
containerID, err := b.probeAndCreate(state, runConfigWithCommentCmd)
|
|
if err != nil || containerID == "" {
|
|
return err
|
|
}
|
|
|
|
// Twiddle the destination when it's a relative path - meaning, make it
|
|
// relative to the WORKINGDIR
|
|
dest, err := normaliseDest(inst.cmdName, state.runConfig.WorkingDir, inst.dest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, info := range inst.infos {
|
|
if err := b.docker.CopyOnBuild(containerID, dest, info.root, info.path, inst.allowLocalDecompression); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return b.commitContainer(state, containerID, runConfigWithCommentCmd)
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
// withCmdComment sets Cmd to a nop comment string. See withCmdCommentString for
|
|
// why there are two almost identical versions of this.
|
|
func withCmdComment(comment string) runConfigModifier {
|
|
return func(runConfig *container.Config) {
|
|
runConfig.Cmd = append(getShell(runConfig), "#(nop) ", comment)
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
func withEnv(env []string) runConfigModifier {
|
|
return func(runConfig *container.Config) {
|
|
runConfig.Env = env
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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[:]...)
|
|
}
|
|
|
|
func (b *Builder) probeCache(dispatchState *dispatchState, runConfig *container.Config) (bool, error) {
|
|
cachedID, err := b.imageProber.Probe(dispatchState.imageID, runConfig)
|
|
if cachedID == "" || err != nil {
|
|
return false, err
|
|
}
|
|
fmt.Fprint(b.Stdout, " ---> Using cache\n")
|
|
|
|
dispatchState.imageID = string(cachedID)
|
|
b.buildStages.update(dispatchState.imageID, runConfig)
|
|
return true, nil
|
|
}
|
|
|
|
var defaultLogConfig = container.LogConfig{Type: "none"}
|
|
|
|
func (b *Builder) probeAndCreate(dispatchState *dispatchState, runConfig *container.Config) (string, error) {
|
|
if hit, err := b.probeCache(dispatchState, runConfig); err != nil || hit {
|
|
return "", err
|
|
}
|
|
// 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
|
|
}
|
|
|
|
func (b *Builder) create(runConfig *container.Config) (string, error) {
|
|
hostConfig := hostConfigFromOptions(b.options)
|
|
container, err := b.containerManager.Create(runConfig, hostConfig)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
// TODO: could this be moved into containerManager.Create() ?
|
|
for _, warning := range container.Warnings {
|
|
fmt.Fprintf(b.Stdout, " ---> [Warning] %s\n", warning)
|
|
}
|
|
fmt.Fprintf(b.Stdout, " ---> Running in %s\n", stringid.TruncateID(container.ID))
|
|
return container.ID, nil
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|