builder: remove container package dependency

Signed-off-by: Tibor Vass <tibor@docker.com>
Upstream-commit: c70f8b3c9c7a6dc6a219354acaa2e650d1403ecf
Component: engine
This commit is contained in:
Tibor Vass
2015-12-10 15:35:53 +01:00
parent 637e991bae
commit bf2423fe8a
10 changed files with 107 additions and 163 deletions

View File

@ -77,7 +77,7 @@ type Builder struct {
Stdout io.Writer
Stderr io.Writer
docker builder.Docker
docker builder.Backend
context builder.Context
dockerfile *parser.Node
@ -102,7 +102,7 @@ type Builder struct {
// NewBuilder creates a new Dockerfile builder from an optional dockerfile and a Config.
// If dockerfile is nil, the Dockerfile specified by Config.DockerfileName,
// will be read from the Context passed to Build().
func NewBuilder(config *Config, docker builder.Docker, context builder.Context, dockerfile io.ReadCloser) (b *Builder, err error) {
func NewBuilder(config *Config, docker builder.Backend, context builder.Context, dockerfile io.ReadCloser) (b *Builder, err error) {
if config == nil {
config = new(Config)
}

View File

@ -215,7 +215,7 @@ func from(b *Builder, args []string, attributes map[string]bool, original string
)
// TODO: don't use `name`, instead resolve it to a digest
if !b.Pull {
image, err = b.docker.LookupImage(name)
image, err = b.docker.GetImage(name)
// TODO: shouldn't we error out if error is different from "not found" ?
}
if image == nil {
@ -394,18 +394,12 @@ func run(b *Builder, args []string, attributes map[string]bool, original string)
logrus.Debugf("[BUILDER] Command to be executed: %v", b.runConfig.Cmd)
c, err := b.create()
cID, err := b.create()
if err != nil {
return err
}
// Ensure that we keep the container mounted until the commit
// to avoid unmounting and then mounting directly again
b.docker.Mount(c)
defer b.docker.Unmount(c)
err = b.run(c)
if err != nil {
if err := b.run(cID); err != nil {
return err
}
@ -414,11 +408,7 @@ func run(b *Builder, args []string, attributes map[string]bool, original string)
// properly match it.
b.runConfig.Env = env
b.runConfig.Cmd = saveCmd
if err := b.commit(c.ID, cmd, "run"); err != nil {
return err
}
return nil
return b.commit(cID, cmd, "run")
}
// CMD foo

View File

@ -23,7 +23,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/builder"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/container"
"github.com/docker/docker/daemon"
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/httputils"
@ -56,21 +56,16 @@ func (b *Builder) commit(id string, autoCmd *stringutils.StrSlice, comment strin
}
defer func(cmd *stringutils.StrSlice) { b.runConfig.Cmd = cmd }(cmd)
if hit, err := b.probeCache(); err != nil {
hit, err := b.probeCache()
if err != nil {
return err
} else if hit {
return nil
}
container, err := b.create()
id, err = b.create()
if err != nil {
return err
}
id = container.ID
if err := b.docker.Mount(container); err != nil {
return err
}
defer b.docker.Unmount(container)
}
// Note: Actually copy the struct
@ -192,11 +187,10 @@ func (b *Builder) runContextCommand(args []string, allowRemote bool, allowLocalD
return nil
}
container, _, err := b.docker.Create(b.runConfig, nil)
container, err := b.docker.ContainerCreate(&daemon.ContainerCreateConfig{Config: b.runConfig})
if err != nil {
return err
}
defer b.docker.Unmount(container)
b.tmpContainers[container.ID] = struct{}{}
comment := fmt.Sprintf("%s %s in %s", cmdName, origPaths, dest)
@ -214,15 +208,12 @@ func (b *Builder) runContextCommand(args []string, allowRemote bool, allowLocalD
}
for _, info := range infos {
if err := b.docker.Copy(container, dest, info.FileInfo, info.decompress); err != nil {
if err := b.docker.BuilderCopy(container.ID, dest, info.FileInfo, info.decompress); err != nil {
return err
}
}
if err := b.commit(container.ID, cmd, comment); err != nil {
return err
}
return nil
return b.commit(container.ID, cmd, comment)
}
func (b *Builder) download(srcURL string) (fi builder.FileInfo, err error) {
@ -414,8 +405,8 @@ func (b *Builder) processImageFrom(img *image.Image) error {
}
// The default path will be blank on Windows (set by HCS)
if len(b.runConfig.Env) == 0 && container.DefaultPathEnv != "" {
b.runConfig.Env = append(b.runConfig.Env, "PATH="+container.DefaultPathEnv)
if len(b.runConfig.Env) == 0 && system.DefaultPathEnv != "" {
b.runConfig.Env = append(b.runConfig.Env, "PATH="+system.DefaultPathEnv)
}
// Process ONBUILD triggers if they exist
@ -487,9 +478,9 @@ func (b *Builder) probeCache() (bool, error) {
return true, nil
}
func (b *Builder) create() (*container.Container, error) {
func (b *Builder) create() (string, error) {
if b.image == "" && !b.noBaseImage {
return nil, fmt.Errorf("Please provide a source image with `from` prior to run")
return "", fmt.Errorf("Please provide a source image with `from` prior to run")
}
b.runConfig.Image = b.image
@ -515,12 +506,14 @@ func (b *Builder) create() (*container.Container, error) {
config := *b.runConfig
// Create the container
c, warnings, err := b.docker.Create(b.runConfig, hostConfig)
c, err := b.docker.ContainerCreate(&daemon.ContainerCreateConfig{
Config: b.runConfig,
HostConfig: hostConfig,
})
if err != nil {
return nil, err
return "", err
}
defer b.docker.Unmount(c)
for _, warning := range warnings {
for _, warning := range c.Warnings {
fmt.Fprintf(b.Stdout, " ---> [Warning] %s\n", warning)
}
@ -529,23 +522,24 @@ func (b *Builder) create() (*container.Container, error) {
if config.Cmd.Len() > 0 {
// override the entry point that may have been picked up from the base image
s := config.Cmd.Slice()
c.Path = s[0]
c.Args = s[1:]
if err := b.docker.ContainerUpdateCmd(c.ID, config.Cmd.Slice()); err != nil {
return "", err
}
}
return c, nil
return c.ID, nil
}
func (b *Builder) run(c *container.Container) error {
var errCh chan error
func (b *Builder) run(cID string) (err error) {
errCh := make(chan error)
if b.Verbose {
errCh = c.Attach(nil, b.Stdout, b.Stderr)
}
//start the container
if err := b.docker.Start(c); err != nil {
return err
go func() {
errCh <- b.docker.ContainerWsAttachWithLogs(cID, &daemon.ContainerWsAttachWithLogsConfig{
OutStream: b.Stdout,
ErrStream: b.Stderr,
Stream: true,
})
}()
}
finished := make(chan struct{})
@ -553,13 +547,17 @@ func (b *Builder) run(c *container.Container) error {
go func() {
select {
case <-b.cancelled:
logrus.Debugln("Build cancelled, killing and removing container:", c.ID)
b.docker.Kill(c)
b.removeContainer(c.ID)
logrus.Debugln("Build cancelled, killing and removing container:", cID)
b.docker.ContainerKill(cID, 0)
b.removeContainer(cID)
case <-finished:
}
}()
if err := b.docker.ContainerStart(cID, nil); err != nil {
return err
}
if b.Verbose {
// Block on reading output from container, stop on err or chan closed
if err := <-errCh; err != nil {
@ -567,8 +565,7 @@ func (b *Builder) run(c *container.Container) error {
}
}
// Wait for it to finish
if ret, _ := c.WaitStop(-1 * time.Second); ret != 0 {
if ret, _ := b.docker.ContainerWait(cID, -1); ret != 0 {
// TODO: change error type, because jsonmessage.JSONError assumes HTTP
return &jsonmessage.JSONError{
Message: fmt.Sprintf("The command '%s' returned a non-zero code: %d", b.runConfig.Cmd.ToString(), ret),
@ -584,7 +581,7 @@ func (b *Builder) removeContainer(c string) error {
ForceRemove: true,
RemoveVolume: true,
}
if err := b.docker.Remove(c, rmConfig); err != nil {
if err := b.docker.ContainerRm(c, rmConfig); err != nil {
fmt.Fprintf(b.Stdout, "Error removing intermediate container %s: %v\n", stringid.TruncateID(c), err)
return err
}