2013-04-24 18:03:01 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-04-24 22:14:10 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2013-04-24 18:03:01 +00:00
|
|
|
"strings"
|
2013-04-24 22:14:10 +00:00
|
|
|
"time"
|
2013-04-24 18:03:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Builder struct {
|
2013-04-24 22:14:10 +00:00
|
|
|
runtime *Runtime
|
|
|
|
repositories *TagStore
|
2013-04-24 22:24:14 +00:00
|
|
|
graph *Graph
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBuilder(runtime *Runtime) *Builder {
|
|
|
|
return &Builder{
|
2013-04-24 22:14:10 +00:00
|
|
|
runtime: runtime,
|
2013-04-24 22:24:14 +00:00
|
|
|
graph: runtime.graph,
|
2013-04-24 22:14:10 +00:00
|
|
|
repositories: runtime.repositories,
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 21:36:45 +00:00
|
|
|
func (builder *Builder) mergeConfig(userConf, imageConf *Config) {
|
|
|
|
if userConf.Hostname != "" {
|
|
|
|
userConf.Hostname = imageConf.Hostname
|
|
|
|
}
|
|
|
|
if userConf.User != "" {
|
|
|
|
userConf.User = imageConf.User
|
|
|
|
}
|
|
|
|
if userConf.Memory == 0 {
|
|
|
|
userConf.Memory = imageConf.Memory
|
|
|
|
}
|
|
|
|
if userConf.MemorySwap == 0 {
|
|
|
|
userConf.MemorySwap = imageConf.MemorySwap
|
|
|
|
}
|
|
|
|
if userConf.PortSpecs == nil || len(userConf.PortSpecs) == 0 {
|
|
|
|
userConf.PortSpecs = imageConf.PortSpecs
|
|
|
|
}
|
|
|
|
if !userConf.Tty {
|
|
|
|
userConf.Tty = userConf.Tty
|
|
|
|
}
|
|
|
|
if !userConf.OpenStdin {
|
|
|
|
userConf.OpenStdin = imageConf.OpenStdin
|
|
|
|
}
|
|
|
|
if !userConf.StdinOnce {
|
|
|
|
userConf.StdinOnce = imageConf.StdinOnce
|
|
|
|
}
|
|
|
|
if userConf.Env == nil || len(userConf.Env) == 0 {
|
|
|
|
userConf.Env = imageConf.Env
|
|
|
|
}
|
|
|
|
if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
|
|
|
|
userConf.Cmd = imageConf.Cmd
|
|
|
|
}
|
|
|
|
if userConf.Dns == nil || len(userConf.Dns) == 0 {
|
|
|
|
userConf.Dns = imageConf.Dns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:14:10 +00:00
|
|
|
func (builder *Builder) Create(config *Config) (*Container, error) {
|
|
|
|
// Lookup image
|
|
|
|
img, err := builder.repositories.LookupImage(config.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
2013-05-01 21:36:45 +00:00
|
|
|
|
|
|
|
if img.Config != nil {
|
|
|
|
builder.mergeConfig(config, img.Config)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Cmd == nil {
|
|
|
|
return nil, fmt.Errorf("No command specified")
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:14:10 +00:00
|
|
|
// Generate id
|
|
|
|
id := GenerateId()
|
|
|
|
// Generate default hostname
|
|
|
|
// FIXME: the lxc template no longer needs to set a default hostname
|
|
|
|
if config.Hostname == "" {
|
|
|
|
config.Hostname = id[:12]
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
2013-04-24 22:14:10 +00:00
|
|
|
|
|
|
|
container := &Container{
|
|
|
|
// FIXME: we should generate the ID here instead of receiving it as an argument
|
|
|
|
Id: id,
|
|
|
|
Created: time.Now(),
|
|
|
|
Path: config.Cmd[0],
|
|
|
|
Args: config.Cmd[1:], //FIXME: de-duplicate from config
|
|
|
|
Config: config,
|
|
|
|
Image: img.Id, // Always use the resolved image id
|
|
|
|
NetworkSettings: &NetworkSettings{},
|
|
|
|
// FIXME: do we need to store this in the container?
|
|
|
|
SysInitPath: sysInitPath,
|
|
|
|
}
|
|
|
|
container.root = builder.runtime.containerRoot(container.Id)
|
|
|
|
// Step 1: create the container directory.
|
|
|
|
// This doubles as a barrier to avoid race conditions.
|
|
|
|
if err := os.Mkdir(container.root, 0700); err != nil {
|
|
|
|
return nil, err
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
|
|
|
|
2013-04-24 22:14:10 +00:00
|
|
|
// If custom dns exists, then create a resolv.conf for the container
|
|
|
|
if len(config.Dns) > 0 {
|
|
|
|
container.ResolvConfPath = path.Join(container.root, "resolv.conf")
|
|
|
|
f, err := os.Create(container.ResolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
for _, dns := range config.Dns {
|
|
|
|
if _, err := f.Write([]byte("nameserver " + dns + "\n")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
container.ResolvConfPath = "/etc/resolv.conf"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: save the container json
|
|
|
|
if err := container.ToDisk(); err != nil {
|
2013-04-24 18:03:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-04-24 22:14:10 +00:00
|
|
|
// Step 3: register the container
|
|
|
|
if err := builder.runtime.Register(container); err != nil {
|
2013-04-24 18:03:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return container, nil
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:24:14 +00:00
|
|
|
// Commit creates a new filesystem image from the current state of a container.
|
|
|
|
// The image can optionally be tagged into a repository
|
2013-05-01 21:36:45 +00:00
|
|
|
func (builder *Builder) Commit(container *Container, repository, tag, comment, author string, config *Config) (*Image, error) {
|
2013-04-24 22:24:14 +00:00
|
|
|
// FIXME: freeze the container before copying it to avoid data corruption?
|
|
|
|
// FIXME: this shouldn't be in commands.
|
|
|
|
rwTar, err := container.ExportRw()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Create a new image from the container's base layers + a new layer from container changes
|
2013-05-01 21:36:45 +00:00
|
|
|
img, err := builder.graph.Create(rwTar, container, comment, author, config)
|
2013-04-24 22:24:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Register the image if needed
|
|
|
|
if repository != "" {
|
|
|
|
if err := builder.repositories.Set(repository, tag, img.Id, true); err != nil {
|
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return img, nil
|
2013-04-24 20:35:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-24 19:02:00 +00:00
|
|
|
func (builder *Builder) clearTmp(containers, images map[string]struct{}) {
|
|
|
|
for c := range containers {
|
|
|
|
tmp := builder.runtime.Get(c)
|
|
|
|
builder.runtime.Destroy(tmp)
|
|
|
|
Debugf("Removing container %s", c)
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
2013-04-24 19:02:00 +00:00
|
|
|
for i := range images {
|
|
|
|
builder.runtime.graph.Delete(i)
|
|
|
|
Debugf("Removing image %s", i)
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:20:45 +00:00
|
|
|
func (builder *Builder) Build(dockerfile io.Reader, stdout io.Writer) (*Image, error) {
|
2013-04-24 19:02:00 +00:00
|
|
|
var (
|
|
|
|
image, base *Image
|
2013-05-01 23:26:26 +00:00
|
|
|
maintainer string
|
2013-04-24 19:02:00 +00:00
|
|
|
tmpContainers map[string]struct{} = make(map[string]struct{})
|
|
|
|
tmpImages map[string]struct{} = make(map[string]struct{})
|
|
|
|
)
|
|
|
|
defer builder.clearTmp(tmpContainers, tmpImages)
|
2013-04-24 18:03:01 +00:00
|
|
|
|
|
|
|
file := bufio.NewReader(dockerfile)
|
|
|
|
for {
|
|
|
|
line, err := file.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
// Skip comments and empty line
|
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
2013-05-01 20:45:35 +00:00
|
|
|
tmp := strings.SplitN(line, " ", 2)
|
2013-04-24 18:03:01 +00:00
|
|
|
if len(tmp) != 2 {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, fmt.Errorf("Invalid Dockerfile format")
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
2013-05-01 22:05:52 +00:00
|
|
|
instruction := strings.Trim(tmp[0], " ")
|
|
|
|
arguments := strings.Trim(tmp[1], " ")
|
2013-05-01 20:45:50 +00:00
|
|
|
switch strings.ToLower(instruction) {
|
2013-04-24 18:03:01 +00:00
|
|
|
case "from":
|
2013-05-01 20:45:50 +00:00
|
|
|
fmt.Fprintf(stdout, "FROM %s\n", arguments)
|
|
|
|
image, err = builder.runtime.repositories.LookupImage(arguments)
|
2013-04-24 18:03:01 +00:00
|
|
|
if err != nil {
|
2013-05-01 22:06:23 +00:00
|
|
|
if builder.runtime.graph.IsNotExist(err) {
|
|
|
|
if builder.runtime.graph.LookupRemoteImage(arguments, builder.runtime.authConfig) {
|
|
|
|
if err := builder.runtime.graph.PullImage(stdout, arguments, builder.runtime.authConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// FIXME: Allow pull repo:tag
|
|
|
|
if err := builder.runtime.graph.PullRepository(stdout, arguments, "", builder.runtime.repositories, builder.runtime.authConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
image, err = builder.runtime.repositories.LookupImage(arguments)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
2013-05-01 22:06:23 +00:00
|
|
|
|
2013-05-01 23:26:26 +00:00
|
|
|
break
|
|
|
|
case "mainainer":
|
|
|
|
fmt.Fprintf(stdout, "MAINTAINER %s\n", arguments)
|
|
|
|
maintainer = arguments
|
2013-04-24 18:03:01 +00:00
|
|
|
break
|
|
|
|
case "run":
|
2013-05-01 20:45:50 +00:00
|
|
|
fmt.Fprintf(stdout, "RUN %s\n", arguments)
|
2013-04-24 18:03:01 +00:00
|
|
|
if image == nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, fmt.Errorf("Please provide a source image with `from` prior to run")
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
2013-05-01 20:45:50 +00:00
|
|
|
config, err := ParseRun([]string{image.Id, "/bin/sh", "-c", arguments}, nil, builder.runtime.capabilities)
|
2013-04-24 22:14:10 +00:00
|
|
|
if err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 22:14:10 +00:00
|
|
|
}
|
2013-04-24 19:02:00 +00:00
|
|
|
|
|
|
|
// Create the container and start it
|
2013-04-24 22:14:10 +00:00
|
|
|
c, err := builder.Create(config)
|
2013-04-24 18:03:01 +00:00
|
|
|
if err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
2013-04-24 22:14:10 +00:00
|
|
|
if err := c.Start(); err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 22:14:10 +00:00
|
|
|
}
|
2013-04-24 19:02:00 +00:00
|
|
|
tmpContainers[c.Id] = struct{}{}
|
|
|
|
|
|
|
|
// Wait for it to finish
|
|
|
|
if result := c.Wait(); result != 0 {
|
2013-05-01 20:45:50 +00:00
|
|
|
return nil, fmt.Errorf("!!! '%s' return non-zero exit code '%d'. Aborting.", arguments, result)
|
2013-04-24 19:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the container
|
2013-05-01 23:26:26 +00:00
|
|
|
base, err = builder.Commit(c, "", "", "", maintainer, nil)
|
2013-04-24 19:02:00 +00:00
|
|
|
if err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 19:02:00 +00:00
|
|
|
}
|
|
|
|
tmpImages[base.Id] = struct{}{}
|
|
|
|
|
2013-04-24 21:28:51 +00:00
|
|
|
fmt.Fprintf(stdout, "===> %s\n", base.ShortId())
|
2013-04-25 15:08:05 +00:00
|
|
|
|
|
|
|
// use the base as the new image
|
|
|
|
image = base
|
|
|
|
|
2013-05-01 23:26:46 +00:00
|
|
|
break
|
|
|
|
case "expose":
|
|
|
|
ports := strings.Split(arguments, " ")
|
|
|
|
|
|
|
|
fmt.Fprintf(stdout, "EXPOSE %v\n", ports)
|
|
|
|
if image == nil {
|
|
|
|
return nil, fmt.Errorf("Please provide a source image with `from` prior to copy")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the container and start it
|
|
|
|
c, err := builder.Create(&Config{Image: image.Id, Cmd: []string{"", ""}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := c.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tmpContainers[c.Id] = struct{}{}
|
|
|
|
|
|
|
|
// Commit the container
|
|
|
|
base, err = builder.Commit(c, "", "", "", maintainer, &Config{PortSpecs: ports})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tmpImages[base.Id] = struct{}{}
|
|
|
|
|
|
|
|
fmt.Fprintf(stdout, "===> %s\n", base.ShortId())
|
|
|
|
|
|
|
|
image = base
|
2013-04-24 18:03:01 +00:00
|
|
|
break
|
2013-05-01 20:45:35 +00:00
|
|
|
case "insert":
|
2013-04-24 21:28:51 +00:00
|
|
|
if image == nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, fmt.Errorf("Please provide a source image with `from` prior to copy")
|
2013-04-24 21:28:51 +00:00
|
|
|
}
|
2013-05-01 20:45:50 +00:00
|
|
|
tmp = strings.SplitN(arguments, " ", 2)
|
2013-04-24 21:28:51 +00:00
|
|
|
if len(tmp) != 2 {
|
2013-05-01 20:45:35 +00:00
|
|
|
return nil, fmt.Errorf("Invalid INSERT format")
|
2013-04-24 21:28:51 +00:00
|
|
|
}
|
2013-05-01 23:26:26 +00:00
|
|
|
sourceUrl := strings.Trim(tmp[0], " ")
|
2013-05-01 22:05:52 +00:00
|
|
|
destPath := strings.Trim(tmp[1], " ")
|
2013-05-01 20:45:50 +00:00
|
|
|
fmt.Fprintf(stdout, "COPY %s to %s in %s\n", sourceUrl, destPath, base.ShortId())
|
2013-04-24 21:28:51 +00:00
|
|
|
|
2013-05-01 20:45:50 +00:00
|
|
|
file, err := Download(sourceUrl, stdout)
|
2013-04-24 21:28:51 +00:00
|
|
|
if err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 21:28:51 +00:00
|
|
|
}
|
|
|
|
defer file.Body.Close()
|
|
|
|
|
2013-05-01 20:45:50 +00:00
|
|
|
config, err := ParseRun([]string{base.Id, "echo", "insert", sourceUrl, destPath}, nil, builder.runtime.capabilities)
|
2013-04-24 22:14:10 +00:00
|
|
|
if err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 22:14:10 +00:00
|
|
|
}
|
|
|
|
c, err := builder.Create(config)
|
2013-04-24 21:28:51 +00:00
|
|
|
if err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 21:28:51 +00:00
|
|
|
}
|
|
|
|
|
2013-04-24 22:14:10 +00:00
|
|
|
if err := c.Start(); err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 22:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for echo to finish
|
|
|
|
if result := c.Wait(); result != 0 {
|
2013-05-01 20:45:50 +00:00
|
|
|
return nil, fmt.Errorf("!!! '%s' return non-zero exit code '%d'. Aborting.", arguments, result)
|
2013-04-24 22:14:10 +00:00
|
|
|
}
|
|
|
|
|
2013-05-01 20:45:50 +00:00
|
|
|
if err := c.Inject(file.Body, destPath); err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 21:28:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-01 23:26:26 +00:00
|
|
|
base, err = builder.Commit(c, "", "", "", maintainer, nil)
|
2013-04-24 21:28:51 +00:00
|
|
|
if err != nil {
|
2013-04-25 18:20:45 +00:00
|
|
|
return nil, err
|
2013-04-24 21:28:51 +00:00
|
|
|
}
|
|
|
|
fmt.Fprintf(stdout, "===> %s\n", base.ShortId())
|
2013-04-28 04:45:05 +00:00
|
|
|
|
|
|
|
image = base
|
|
|
|
|
2013-04-24 21:28:51 +00:00
|
|
|
break
|
2013-04-24 18:03:01 +00:00
|
|
|
default:
|
2013-05-01 20:45:35 +00:00
|
|
|
fmt.Fprintf(stdout, "Skipping unknown instruction %s\n", instruction)
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if base != nil {
|
2013-04-24 19:02:00 +00:00
|
|
|
// The build is successful, keep the temporary containers and images
|
|
|
|
for i := range tmpImages {
|
|
|
|
delete(tmpImages, i)
|
|
|
|
}
|
|
|
|
for i := range tmpContainers {
|
|
|
|
delete(tmpContainers, i)
|
|
|
|
}
|
2013-04-24 21:28:51 +00:00
|
|
|
fmt.Fprintf(stdout, "Build finished. image id: %s\n", base.ShortId())
|
2013-04-24 18:03:01 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(stdout, "An error occured during the build\n")
|
|
|
|
}
|
2013-04-25 18:20:45 +00:00
|
|
|
return base, nil
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|