2013-04-24 18:03:01 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-06-06 18:01:09 +00:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-04-24 22:14:10 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
2013-04-24 18:03:01 +00:00
|
|
|
)
|
|
|
|
|
2013-06-05 21:20:19 +00:00
|
|
|
var defaultDns = []string{"8.8.8.8", "8.8.4.4"}
|
|
|
|
|
2013-04-24 18:03:01 +00:00
|
|
|
type Builder struct {
|
2013-04-24 22:14:10 +00:00
|
|
|
runtime *Runtime
|
|
|
|
repositories *TagStore
|
2013-05-06 23:58:09 +00:00
|
|
|
graph *Graph
|
2013-05-19 17:46:24 +00:00
|
|
|
|
|
|
|
config *Config
|
|
|
|
image *Image
|
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-05-06 23:58:09 +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-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-05-01 21:36:45 +00:00
|
|
|
}
|
2013-05-01 21:36:45 +00:00
|
|
|
|
|
|
|
if img.Config != nil {
|
2013-05-19 17:46:24 +00:00
|
|
|
MergeConfig(config, img.Config)
|
2013-05-01 21:36:45 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 18:18:13 +00:00
|
|
|
if config.Cmd == nil || len(config.Cmd) == 0 {
|
2013-05-01 21:36:45 +00:00
|
|
|
return nil, fmt.Errorf("No command specified")
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:14:10 +00:00
|
|
|
// Generate id
|
2013-06-04 18:00:22 +00:00
|
|
|
id := GenerateID()
|
2013-04-24 22:14:10 +00:00
|
|
|
// Generate default hostname
|
|
|
|
// FIXME: the lxc template no longer needs to set a default hostname
|
|
|
|
if config.Hostname == "" {
|
|
|
|
config.Hostname = id[:12]
|
2013-05-01 21:36:45 +00:00
|
|
|
}
|
2013-04-24 22:14:10 +00:00
|
|
|
|
2013-06-25 02:20:05 +00:00
|
|
|
var args []string
|
|
|
|
var entrypoint string
|
|
|
|
|
|
|
|
if len(config.Entrypoint) != 0 {
|
|
|
|
entrypoint = config.Entrypoint[0]
|
|
|
|
args = append(config.Entrypoint[1:], config.Cmd...)
|
|
|
|
} else {
|
|
|
|
entrypoint = config.Cmd[0]
|
|
|
|
args = config.Cmd[1:]
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:14:10 +00:00
|
|
|
container := &Container{
|
|
|
|
// FIXME: we should generate the ID here instead of receiving it as an argument
|
2013-06-04 18:00:22 +00:00
|
|
|
ID: id,
|
2013-04-24 22:14:10 +00:00
|
|
|
Created: time.Now(),
|
2013-06-25 02:20:05 +00:00
|
|
|
Path: entrypoint,
|
|
|
|
Args: args, //FIXME: de-duplicate from config
|
2013-04-24 22:14:10 +00:00
|
|
|
Config: config,
|
2013-06-04 18:00:22 +00:00
|
|
|
Image: img.ID, // Always use the resolved image id
|
2013-04-24 22:14:10 +00:00
|
|
|
NetworkSettings: &NetworkSettings{},
|
|
|
|
// FIXME: do we need to store this in the container?
|
|
|
|
SysInitPath: sysInitPath,
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
container.root = builder.runtime.containerRoot(container.ID)
|
2013-04-24 22:14:10 +00:00
|
|
|
// 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-05-01 21:36:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 18:01:09 +00:00
|
|
|
if len(config.Dns) == 0 && len(builder.runtime.Dns) == 0 && utils.CheckLocalDns() {
|
|
|
|
//"WARNING: Docker detected local DNS server on resolv.conf. Using default external servers: %v", defaultDns
|
|
|
|
builder.runtime.Dns = defaultDns
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:14:10 +00:00
|
|
|
// If custom dns exists, then create a resolv.conf for the container
|
2013-06-05 21:20:19 +00:00
|
|
|
if len(config.Dns) > 0 || len(builder.runtime.Dns) > 0 {
|
|
|
|
var dns []string
|
|
|
|
if len(config.Dns) > 0 {
|
|
|
|
dns = config.Dns
|
|
|
|
} else {
|
|
|
|
dns = builder.runtime.Dns
|
|
|
|
}
|
2013-04-24 22:14:10 +00:00
|
|
|
container.ResolvConfPath = path.Join(container.root, "resolv.conf")
|
|
|
|
f, err := os.Create(container.ResolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2013-06-05 21:20:19 +00:00
|
|
|
for _, dns := range dns {
|
2013-04-24 22:14:10 +00:00
|
|
|
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 22:14:10 +00:00
|
|
|
return nil, err
|
2013-04-24 18:03:01 +00:00
|
|
|
}
|
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-05-01 21:36:45 +00:00
|
|
|
// Commit creates a new filesystem image from the current state of a container.
|
|
|
|
// The image can optionally be tagged into a repository
|
|
|
|
func (builder *Builder) Commit(container *Container, repository, tag, comment, author string, config *Config) (*Image, error) {
|
|
|
|
// FIXME: freeze the container before copying it to avoid data corruption?
|
|
|
|
// FIXME: this shouldn't be in commands.
|
2013-07-25 15:18:34 +00:00
|
|
|
if err := container.EnsureMounted(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-05-01 21:36:45 +00:00
|
|
|
rwTar, err := container.ExportRw()
|
2013-04-24 22:24:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-05-01 21:36:45 +00:00
|
|
|
// Create a new image from the container's base layers + a new layer from container changes
|
|
|
|
img, err := builder.graph.Create(rwTar, container, comment, author, config)
|
2013-04-24 22:24:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-05-06 23:58:09 +00:00
|
|
|
// Register the image if needed
|
|
|
|
if repository != "" {
|
2013-06-04 18:00:22 +00:00
|
|
|
if err := builder.repositories.Set(repository, tag, img.ID, true); err != nil {
|
2013-05-06 23:58:09 +00:00
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
}
|
2013-04-24 22:24:14 +00:00
|
|
|
return img, nil
|
2013-04-24 20:35:57 +00:00
|
|
|
}
|