mirror of
https://github.com/clearlinux/docker.git
synced 2026-08-20 20:56:42 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c97a1aada6 | |||
| 803a8d86e5 | |||
| 5fd1ff014a | |||
| 3c9ed5cdd6 | |||
| 59b6a93504 | |||
| 72d7c3847a | |||
| 6e7b8efa92 | |||
| fa401da0ff | |||
| 26ec7b2e77 | |||
| 03a9e41245 | |||
| 55869531f5 | |||
| 9193585d66 |
+146
-52
@@ -4,45 +4,108 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
runtime *Runtime
|
runtime *Runtime
|
||||||
|
repositories *TagStore
|
||||||
|
graph *Graph
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBuilder(runtime *Runtime) *Builder {
|
func NewBuilder(runtime *Runtime) *Builder {
|
||||||
return &Builder{
|
return &Builder{
|
||||||
runtime: runtime,
|
runtime: runtime,
|
||||||
|
graph: runtime.graph,
|
||||||
|
repositories: runtime.repositories,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (builder *Builder) Run(image *Image, cmd ...string) (*Container, error) {
|
func (builder *Builder) Create(config *Config) (*Container, error) {
|
||||||
// FIXME: pass a NopWriter instead of nil
|
// Lookup image
|
||||||
config, err := ParseRun(append([]string{"-d", image.Id}, cmd...), nil, builder.runtime.capabilities)
|
img, err := builder.repositories.LookupImage(config.Image)
|
||||||
if config.Image == "" {
|
|
||||||
return nil, fmt.Errorf("Image not specified")
|
|
||||||
}
|
|
||||||
if len(config.Cmd) == 0 {
|
|
||||||
return nil, fmt.Errorf("Command not specified")
|
|
||||||
}
|
|
||||||
if config.Tty {
|
|
||||||
return nil, fmt.Errorf("The tty mode is not supported within the builder")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create new container
|
|
||||||
container, err := builder.runtime.Create(config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := container.Start(); err != nil {
|
// 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]
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Step 3: register the container
|
||||||
|
if err := builder.runtime.Register(container); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return container, nil
|
return container, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) (*Image, error) {
|
func (builder *Builder) Commit(container *Container, repository, tag, comment, author string) (*Image, error) {
|
||||||
return builder.runtime.Commit(container.Id, repository, tag, comment, author)
|
// 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
|
||||||
|
img, err := builder.graph.Create(rwTar, container, comment, author)
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (builder *Builder) clearTmp(containers, images map[string]struct{}) {
|
func (builder *Builder) clearTmp(containers, images map[string]struct{}) {
|
||||||
@@ -57,7 +120,7 @@ func (builder *Builder) clearTmp(containers, images map[string]struct{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (builder *Builder) Build(dockerfile io.Reader, stdout io.Writer) error {
|
func (builder *Builder) Build(dockerfile io.Reader, stdout io.Writer) (*Image, error) {
|
||||||
var (
|
var (
|
||||||
image, base *Image
|
image, base *Image
|
||||||
tmpContainers map[string]struct{} = make(map[string]struct{})
|
tmpContainers map[string]struct{} = make(map[string]struct{})
|
||||||
@@ -72,85 +135,116 @@ func (builder *Builder) Build(dockerfile io.Reader, stdout io.Writer) error {
|
|||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
// Skip comments and empty line
|
// Skip comments and empty line
|
||||||
if len(line) == 0 || line[0] == '#' {
|
if len(line) == 0 || line[0] == '#' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tmp := strings.SplitN(line, " ", 2)
|
tmp := strings.SplitN(line, " ", 2)
|
||||||
if len(tmp) != 2 {
|
if len(tmp) != 2 {
|
||||||
return fmt.Errorf("Invalid Dockerfile format")
|
return nil, fmt.Errorf("Invalid Dockerfile format")
|
||||||
}
|
}
|
||||||
switch tmp[0] {
|
instruction := tmp[0]
|
||||||
|
arguments := tmp[1]
|
||||||
|
switch strings.ToLower(instruction) {
|
||||||
case "from":
|
case "from":
|
||||||
fmt.Fprintf(stdout, "FROM %s\n", tmp[1])
|
fmt.Fprintf(stdout, "FROM %s\n", arguments)
|
||||||
image, err = builder.runtime.repositories.LookupImage(tmp[1])
|
image, err = builder.runtime.repositories.LookupImage(arguments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case "run":
|
case "run":
|
||||||
fmt.Fprintf(stdout, "RUN %s\n", tmp[1])
|
fmt.Fprintf(stdout, "RUN %s\n", arguments)
|
||||||
if image == nil {
|
if image == nil {
|
||||||
return fmt.Errorf("Please provide a source image with `from` prior to run")
|
return nil, fmt.Errorf("Please provide a source image with `from` prior to run")
|
||||||
|
}
|
||||||
|
config, err := ParseRun([]string{image.Id, "/bin/sh", "-c", arguments}, nil, builder.runtime.capabilities)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the container and start it
|
// Create the container and start it
|
||||||
c, err := builder.Run(image, "/bin/sh", "-c", tmp[1])
|
c, err := builder.Create(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := c.Start(); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
tmpContainers[c.Id] = struct{}{}
|
tmpContainers[c.Id] = struct{}{}
|
||||||
|
|
||||||
// Wait for it to finish
|
// Wait for it to finish
|
||||||
if result := c.Wait(); result != 0 {
|
if result := c.Wait(); result != 0 {
|
||||||
return fmt.Errorf("!!! '%s' return non-zero exit code '%d'. Aborting.", tmp[1], result)
|
return nil, fmt.Errorf("!!! '%s' return non-zero exit code '%d'. Aborting.", arguments, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit the container
|
// Commit the container
|
||||||
base, err = builder.Commit(c, "", "", "", "")
|
base, err = builder.Commit(c, "", "", "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
tmpImages[base.Id] = struct{}{}
|
tmpImages[base.Id] = struct{}{}
|
||||||
|
|
||||||
fmt.Fprintf(stdout, "===> %s\n", base.ShortId())
|
fmt.Fprintf(stdout, "===> %s\n", base.ShortId())
|
||||||
break
|
|
||||||
case "copy":
|
|
||||||
if image == nil {
|
|
||||||
return fmt.Errorf("Please provide a source image with `from` prior to copy")
|
|
||||||
}
|
|
||||||
tmp2 := strings.SplitN(tmp[1], " ", 2)
|
|
||||||
if len(tmp) != 2 {
|
|
||||||
return fmt.Errorf("Invalid COPY format")
|
|
||||||
}
|
|
||||||
fmt.Fprintf(stdout, "COPY %s to %s in %s\n", tmp2[0], tmp2[1], base.ShortId())
|
|
||||||
|
|
||||||
file, err := Download(tmp2[0], stdout)
|
// use the base as the new image
|
||||||
|
image = base
|
||||||
|
|
||||||
|
break
|
||||||
|
case "insert":
|
||||||
|
if image == nil {
|
||||||
|
return nil, fmt.Errorf("Please provide a source image with `from` prior to copy")
|
||||||
|
}
|
||||||
|
tmp = strings.SplitN(arguments, " ", 2)
|
||||||
|
if len(tmp) != 2 {
|
||||||
|
return nil, fmt.Errorf("Invalid INSERT format")
|
||||||
|
}
|
||||||
|
sourceUrl := tmp[0]
|
||||||
|
destPath := tmp[1]
|
||||||
|
fmt.Fprintf(stdout, "COPY %s to %s in %s\n", sourceUrl, destPath, base.ShortId())
|
||||||
|
|
||||||
|
file, err := Download(sourceUrl, stdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer file.Body.Close()
|
defer file.Body.Close()
|
||||||
|
|
||||||
c, err := builder.Run(base, "echo", "insert", tmp2[0], tmp2[1])
|
config, err := ParseRun([]string{base.Id, "echo", "insert", sourceUrl, destPath}, nil, builder.runtime.capabilities)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
|
}
|
||||||
|
c, err := builder.Create(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.Inject(file.Body, tmp2[1]); err != nil {
|
if err := c.Start(); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for echo to finish
|
||||||
|
if result := c.Wait(); result != 0 {
|
||||||
|
return nil, fmt.Errorf("!!! '%s' return non-zero exit code '%d'. Aborting.", arguments, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.Inject(file.Body, destPath); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
base, err = builder.Commit(c, "", "", "", "")
|
base, err = builder.Commit(c, "", "", "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
fmt.Fprintf(stdout, "===> %s\n", base.ShortId())
|
fmt.Fprintf(stdout, "===> %s\n", base.ShortId())
|
||||||
|
|
||||||
|
image = base
|
||||||
|
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(stdout, "Skipping unknown op %s\n", tmp[0])
|
fmt.Fprintf(stdout, "Skipping unknown instruction %s\n", instruction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if base != nil {
|
if base != nil {
|
||||||
@@ -165,5 +259,5 @@ func (builder *Builder) Build(dockerfile io.Reader, stdout io.Writer) error {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(stdout, "An error occured during the build\n")
|
fmt.Fprintf(stdout, "An error occured during the build\n")
|
||||||
}
|
}
|
||||||
return nil
|
return base, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package docker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const Dockerfile = `
|
||||||
|
# VERSION 0.1
|
||||||
|
# DOCKER-VERSION 0.1.6
|
||||||
|
|
||||||
|
from docker-ut
|
||||||
|
run sh -c 'echo root:testpass > /tmp/passwd'
|
||||||
|
run mkdir -p /var/run/sshd
|
||||||
|
copy https://raw.github.com/dotcloud/docker/master/CHANGELOG.md /tmp/CHANGELOG.md
|
||||||
|
`
|
||||||
|
|
||||||
|
func TestBuild(t *testing.T) {
|
||||||
|
runtime, err := newTestRuntime()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer nuke(runtime)
|
||||||
|
|
||||||
|
builder := NewBuilder(runtime)
|
||||||
|
|
||||||
|
img, err := builder.Build(strings.NewReader(Dockerfile), &nopWriter{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
container, err := builder.Create(
|
||||||
|
&Config{
|
||||||
|
Image: img.Id,
|
||||||
|
Cmd: []string{"cat", "/tmp/passwd"},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer runtime.Destroy(container)
|
||||||
|
|
||||||
|
output, err := container.Output()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(output) != "root:testpass\n" {
|
||||||
|
t.Fatalf("Unexpected output. Read '%s', expected '%s'", output, "root:testpass\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
container2, err := builder.Create(
|
||||||
|
&Config{
|
||||||
|
Image: img.Id,
|
||||||
|
Cmd: []string{"ls", "-d", "/var/run/sshd"},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer runtime.Destroy(container2)
|
||||||
|
|
||||||
|
output, err = container2.Output()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(output) != "/var/run/sshd\n" {
|
||||||
|
t.Fatal("/var/run/sshd has not been created")
|
||||||
|
}
|
||||||
|
|
||||||
|
container3, err := builder.Create(
|
||||||
|
&Config{
|
||||||
|
Image: img.Id,
|
||||||
|
Cmd: []string{"cat", "/tmp/CHANGELOG.md"},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer runtime.Destroy(container3)
|
||||||
|
|
||||||
|
output, err = container3.Output()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(output) == 0 {
|
||||||
|
t.Fatal("/tmp/CHANGELOG.md has not been copied")
|
||||||
|
}
|
||||||
|
}
|
||||||
+24
-24
@@ -10,7 +10,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -34,7 +33,7 @@ func (srv *Server) Help() string {
|
|||||||
help := "Usage: docker COMMAND [arg...]\n\nA self-sufficient runtime for linux containers.\n\nCommands:\n"
|
help := "Usage: docker COMMAND [arg...]\n\nA self-sufficient runtime for linux containers.\n\nCommands:\n"
|
||||||
for _, cmd := range [][]string{
|
for _, cmd := range [][]string{
|
||||||
{"attach", "Attach to a running container"},
|
{"attach", "Attach to a running container"},
|
||||||
{"build", "Build a container from Dockerfile"},
|
{"build", "Build a container from Dockerfile via stdin"},
|
||||||
{"commit", "Create a new image from a container's changes"},
|
{"commit", "Create a new image from a container's changes"},
|
||||||
{"diff", "Inspect changes on a container's filesystem"},
|
{"diff", "Inspect changes on a container's filesystem"},
|
||||||
{"export", "Stream the contents of a container as a tar archive"},
|
{"export", "Stream the contents of a container as a tar archive"},
|
||||||
@@ -90,8 +89,13 @@ func (srv *Server) CmdInsert(stdin io.ReadCloser, stdout rcli.DockerConn, args .
|
|||||||
}
|
}
|
||||||
defer file.Body.Close()
|
defer file.Body.Close()
|
||||||
|
|
||||||
|
config, err := ParseRun([]string{img.Id, "echo", "insert", url, path}, nil, srv.runtime.capabilities)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
b := NewBuilder(srv.runtime)
|
b := NewBuilder(srv.runtime)
|
||||||
c, err := b.Run(img, "echo", "insert", url, path)
|
c, err := b.Create(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -110,28 +114,16 @@ func (srv *Server) CmdInsert(stdin io.ReadCloser, stdout rcli.DockerConn, args .
|
|||||||
|
|
||||||
func (srv *Server) CmdBuild(stdin io.ReadCloser, stdout rcli.DockerConn, args ...string) error {
|
func (srv *Server) CmdBuild(stdin io.ReadCloser, stdout rcli.DockerConn, args ...string) error {
|
||||||
stdout.Flush()
|
stdout.Flush()
|
||||||
cmd := rcli.Subcmd(stdout, "build", "[Dockerfile|-]", "Build a container from Dockerfile")
|
cmd := rcli.Subcmd(stdout, "build", "-", "Build a container from Dockerfile via stdin")
|
||||||
if err := cmd.Parse(args); err != nil {
|
if err := cmd.Parse(args); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
dockerfile := cmd.Arg(0)
|
img, err := NewBuilder(srv.runtime).Build(stdin, stdout)
|
||||||
if dockerfile == "" {
|
if err != nil {
|
||||||
dockerfile = "Dockerfile"
|
return err
|
||||||
}
|
}
|
||||||
|
fmt.Fprintf(stdout, "%s\n", img.ShortId())
|
||||||
var file io.Reader
|
return nil
|
||||||
|
|
||||||
if dockerfile != "-" {
|
|
||||||
f, err := os.Open(dockerfile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
file = f
|
|
||||||
} else {
|
|
||||||
file = stdin
|
|
||||||
}
|
|
||||||
return NewBuilder(srv.runtime).Build(file, stdout)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 'docker login': login / register a user to registry service.
|
// 'docker login': login / register a user to registry service.
|
||||||
@@ -805,7 +797,13 @@ func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
|||||||
cmd.Usage()
|
cmd.Usage()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
img, err := srv.runtime.Commit(containerName, repository, tag, *flComment, *flAuthor)
|
|
||||||
|
container := srv.runtime.Get(containerName)
|
||||||
|
if container == nil {
|
||||||
|
return fmt.Errorf("No such container: %s", containerName)
|
||||||
|
}
|
||||||
|
|
||||||
|
img, err := NewBuilder(srv.runtime).Commit(container, repository, tag, *flComment, *flAuthor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1008,8 +1006,10 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout rcli.DockerConn, args ...s
|
|||||||
// or tell the client there is no options
|
// or tell the client there is no options
|
||||||
stdout.Flush()
|
stdout.Flush()
|
||||||
|
|
||||||
|
b := NewBuilder(srv.runtime)
|
||||||
|
|
||||||
// Create new container
|
// Create new container
|
||||||
container, err := srv.runtime.Create(config)
|
container, err := b.Create(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If container not found, try to pull it
|
// If container not found, try to pull it
|
||||||
if srv.runtime.graph.IsNotExist(err) {
|
if srv.runtime.graph.IsNotExist(err) {
|
||||||
@@ -1017,7 +1017,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout rcli.DockerConn, args ...s
|
|||||||
if err = srv.CmdPull(stdin, stdout, config.Image); err != nil {
|
if err = srv.CmdPull(stdin, stdout, config.Image); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if container, err = srv.runtime.Create(config); err != nil {
|
if container, err = b.Create(config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+1
-1
@@ -339,7 +339,7 @@ func TestAttachDisconnect(t *testing.T) {
|
|||||||
|
|
||||||
srv := &Server{runtime: runtime}
|
srv := &Server{runtime: runtime}
|
||||||
|
|
||||||
container, err := runtime.Create(
|
container, err := NewBuilder(runtime).Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Memory: 33554432,
|
Memory: 33554432,
|
||||||
|
|||||||
+39
-28
@@ -20,7 +20,7 @@ func TestIdFormat(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container1, err := runtime.Create(
|
container1, err := NewBuilder(runtime).Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"/bin/sh", "-c", "echo hello world"},
|
Cmd: []string{"/bin/sh", "-c", "echo hello world"},
|
||||||
@@ -45,7 +45,7 @@ func TestMultipleAttachRestart(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(
|
container, err := NewBuilder(runtime).Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"/bin/sh", "-c",
|
Cmd: []string{"/bin/sh", "-c",
|
||||||
@@ -157,8 +157,10 @@ func TestDiff(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
|
builder := NewBuilder(runtime)
|
||||||
|
|
||||||
// Create a container and remove a file
|
// Create a container and remove a file
|
||||||
container1, err := runtime.Create(
|
container1, err := builder.Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"/bin/rm", "/etc/passwd"},
|
Cmd: []string{"/bin/rm", "/etc/passwd"},
|
||||||
@@ -199,7 +201,7 @@ func TestDiff(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new container from the commited image
|
// Create a new container from the commited image
|
||||||
container2, err := runtime.Create(
|
container2, err := builder.Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: img.Id,
|
Image: img.Id,
|
||||||
Cmd: []string{"cat", "/etc/passwd"},
|
Cmd: []string{"cat", "/etc/passwd"},
|
||||||
@@ -232,7 +234,10 @@ func TestCommitRun(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container1, err := runtime.Create(
|
|
||||||
|
builder := NewBuilder(runtime)
|
||||||
|
|
||||||
|
container1, err := builder.Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"/bin/sh", "-c", "echo hello > /world"},
|
Cmd: []string{"/bin/sh", "-c", "echo hello > /world"},
|
||||||
@@ -265,7 +270,7 @@ func TestCommitRun(t *testing.T) {
|
|||||||
|
|
||||||
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
||||||
|
|
||||||
container2, err := runtime.Create(
|
container2, err := builder.Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: img.Id,
|
Image: img.Id,
|
||||||
Memory: 33554432,
|
Memory: 33554432,
|
||||||
@@ -313,7 +318,7 @@ func TestStart(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(
|
container, err := NewBuilder(runtime).Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Memory: 33554432,
|
Memory: 33554432,
|
||||||
@@ -352,7 +357,7 @@ func TestRun(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(
|
container, err := NewBuilder(runtime).Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Memory: 33554432,
|
Memory: 33554432,
|
||||||
@@ -381,7 +386,7 @@ func TestOutput(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(
|
container, err := NewBuilder(runtime).Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"echo", "-n", "foobar"},
|
Cmd: []string{"echo", "-n", "foobar"},
|
||||||
@@ -406,7 +411,7 @@ func TestKillDifferentUser(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"tail", "-f", "/etc/resolv.conf"},
|
Cmd: []string{"tail", "-f", "/etc/resolv.conf"},
|
||||||
User: "daemon",
|
User: "daemon",
|
||||||
@@ -454,7 +459,7 @@ func TestKill(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"cat", "/dev/zero"},
|
Cmd: []string{"cat", "/dev/zero"},
|
||||||
},
|
},
|
||||||
@@ -500,7 +505,9 @@ func TestExitCode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
trueContainer, err := runtime.Create(&Config{
|
builder := NewBuilder(runtime)
|
||||||
|
|
||||||
|
trueContainer, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"/bin/true", ""},
|
Cmd: []string{"/bin/true", ""},
|
||||||
})
|
})
|
||||||
@@ -515,7 +522,7 @@ func TestExitCode(t *testing.T) {
|
|||||||
t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
|
t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
falseContainer, err := runtime.Create(&Config{
|
falseContainer, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"/bin/false", ""},
|
Cmd: []string{"/bin/false", ""},
|
||||||
})
|
})
|
||||||
@@ -537,7 +544,7 @@ func TestRestart(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"echo", "-n", "foobar"},
|
Cmd: []string{"echo", "-n", "foobar"},
|
||||||
},
|
},
|
||||||
@@ -570,7 +577,7 @@ func TestRestartStdin(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"cat"},
|
Cmd: []string{"cat"},
|
||||||
|
|
||||||
@@ -649,8 +656,10 @@ func TestUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
|
builder := NewBuilder(runtime)
|
||||||
|
|
||||||
// Default user must be root
|
// Default user must be root
|
||||||
container, err := runtime.Create(&Config{
|
container, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"id"},
|
Cmd: []string{"id"},
|
||||||
},
|
},
|
||||||
@@ -668,7 +677,7 @@ func TestUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set a username
|
// Set a username
|
||||||
container, err = runtime.Create(&Config{
|
container, err = builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"id"},
|
Cmd: []string{"id"},
|
||||||
|
|
||||||
@@ -688,7 +697,7 @@ func TestUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set a UID
|
// Set a UID
|
||||||
container, err = runtime.Create(&Config{
|
container, err = builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"id"},
|
Cmd: []string{"id"},
|
||||||
|
|
||||||
@@ -708,7 +717,7 @@ func TestUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set a different user by uid
|
// Set a different user by uid
|
||||||
container, err = runtime.Create(&Config{
|
container, err = builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"id"},
|
Cmd: []string{"id"},
|
||||||
|
|
||||||
@@ -730,7 +739,7 @@ func TestUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set a different user by username
|
// Set a different user by username
|
||||||
container, err = runtime.Create(&Config{
|
container, err = builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"id"},
|
Cmd: []string{"id"},
|
||||||
|
|
||||||
@@ -757,7 +766,9 @@ func TestMultipleContainers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
container1, err := runtime.Create(&Config{
|
builder := NewBuilder(runtime)
|
||||||
|
|
||||||
|
container1, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"cat", "/dev/zero"},
|
Cmd: []string{"cat", "/dev/zero"},
|
||||||
},
|
},
|
||||||
@@ -767,7 +778,7 @@ func TestMultipleContainers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer runtime.Destroy(container1)
|
defer runtime.Destroy(container1)
|
||||||
|
|
||||||
container2, err := runtime.Create(&Config{
|
container2, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"cat", "/dev/zero"},
|
Cmd: []string{"cat", "/dev/zero"},
|
||||||
},
|
},
|
||||||
@@ -813,7 +824,7 @@ func TestStdin(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"cat"},
|
Cmd: []string{"cat"},
|
||||||
|
|
||||||
@@ -860,7 +871,7 @@ func TestTty(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"cat"},
|
Cmd: []string{"cat"},
|
||||||
|
|
||||||
@@ -907,7 +918,7 @@ func TestEnv(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"/usr/bin/env"},
|
Cmd: []string{"/usr/bin/env"},
|
||||||
},
|
},
|
||||||
@@ -981,7 +992,7 @@ func TestLXCConfig(t *testing.T) {
|
|||||||
memMin := 33554432
|
memMin := 33554432
|
||||||
memMax := 536870912
|
memMax := 536870912
|
||||||
mem := memMin + rand.Intn(memMax-memMin)
|
mem := memMin + rand.Intn(memMax-memMin)
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"/bin/true"},
|
Cmd: []string{"/bin/true"},
|
||||||
|
|
||||||
@@ -1008,7 +1019,7 @@ func BenchmarkRunSequencial(b *testing.B) {
|
|||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"echo", "-n", "foo"},
|
Cmd: []string{"echo", "-n", "foo"},
|
||||||
},
|
},
|
||||||
@@ -1043,7 +1054,7 @@ func BenchmarkRunParallel(b *testing.B) {
|
|||||||
complete := make(chan error)
|
complete := make(chan error)
|
||||||
tasks = append(tasks, complete)
|
tasks = append(tasks, complete)
|
||||||
go func(i int, complete chan error) {
|
go func(i int, complete chan error) {
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"echo", "-n", "foo"},
|
Cmd: []string{"echo", "-n", "foo"},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
==============
|
||||||
|
Docker Builder
|
||||||
|
==============
|
||||||
|
|
||||||
|
.. contents:: Table of Contents
|
||||||
|
|
||||||
|
1. Format
|
||||||
|
=========
|
||||||
|
|
||||||
|
The Docker builder format is quite simple:
|
||||||
|
|
||||||
|
``instruction arguments``
|
||||||
|
|
||||||
|
The first instruction must be `FROM`
|
||||||
|
|
||||||
|
All instruction are to be placed in a file named `Dockerfile`
|
||||||
|
|
||||||
|
In order to place comments within a Dockerfile, simply prefix the line with "`#`"
|
||||||
|
|
||||||
|
2. Instructions
|
||||||
|
===============
|
||||||
|
|
||||||
|
Docker builder comes with a set of instructions:
|
||||||
|
|
||||||
|
1. FROM: Set from what image to build
|
||||||
|
2. RUN: Execute a command
|
||||||
|
3. INSERT: Insert a remote file (http) into the image
|
||||||
|
|
||||||
|
2.1 FROM
|
||||||
|
--------
|
||||||
|
``FROM <image>``
|
||||||
|
|
||||||
|
The `FROM` instruction must be the first one in order for Builder to know from where to run commands.
|
||||||
|
|
||||||
|
`FROM` can also be used in order to build multiple images within a single Dockerfile
|
||||||
|
|
||||||
|
2.2 RUN
|
||||||
|
-------
|
||||||
|
``RUN <command>``
|
||||||
|
|
||||||
|
The `RUN` instruction is the main one, it allows you to execute any commands on the `FROM` image and to save the results.
|
||||||
|
You can use as many `RUN` as you want within a Dockerfile, the commands will be executed on the result of the previous command.
|
||||||
|
|
||||||
|
2.3 INSERT
|
||||||
|
----------
|
||||||
|
|
||||||
|
``INSERT <file url> <path>``
|
||||||
|
|
||||||
|
The `INSERT` instruction will download the file at the given url and place it within the image at the given path.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
The path must include the file name.
|
||||||
|
|
||||||
|
3. Dockerfile Examples
|
||||||
|
======================
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
# Nginx
|
||||||
|
#
|
||||||
|
# VERSION 0.0.1
|
||||||
|
# DOCKER-VERSION 0.2
|
||||||
|
|
||||||
|
from ubuntu
|
||||||
|
|
||||||
|
# make sure the package repository is up to date
|
||||||
|
run echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
|
||||||
|
run apt-get update
|
||||||
|
|
||||||
|
run apt-get install -y inotify-tools nginx apache openssh-server
|
||||||
|
insert https://raw.github.com/creack/docker-vps/master/nginx-wrapper.sh /usr/sbin/nginx-wrapper
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
# Firefox over VNC
|
||||||
|
#
|
||||||
|
# VERSION 0.3
|
||||||
|
# DOCKER-VERSION 0.2
|
||||||
|
|
||||||
|
from ubuntu
|
||||||
|
# make sure the package repository is up to date
|
||||||
|
run echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
|
||||||
|
run apt-get update
|
||||||
|
|
||||||
|
# Install vnc, xvfb in order to create a 'fake' display and firefox
|
||||||
|
run apt-get install -y x11vnc xvfb firefox
|
||||||
|
run mkdir /.vnc
|
||||||
|
# Setup a password
|
||||||
|
run x11vnc -storepasswd 1234 ~/.vnc/passwd
|
||||||
|
# Autostart firefox (might not be the best way to do it, but it does the trick)
|
||||||
|
run bash -c 'echo "firefox" >> /.bashrc'
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
:title: docker documentation
|
||||||
|
:description: Documentation for docker builder
|
||||||
|
:keywords: docker, builder, dockerfile
|
||||||
|
|
||||||
|
|
||||||
|
Builder
|
||||||
|
=======
|
||||||
|
|
||||||
|
Contents:
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 2
|
||||||
|
|
||||||
|
basics
|
||||||
@@ -27,6 +27,7 @@ Available Commands
|
|||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
|
|
||||||
command/attach
|
command/attach
|
||||||
|
command/build
|
||||||
command/commit
|
command/commit
|
||||||
command/diff
|
command/diff
|
||||||
command/export
|
command/export
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
===========================================
|
||||||
|
``build`` -- Build a container from Dockerfile via stdin
|
||||||
|
===========================================
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
Usage: docker build -
|
||||||
|
Example: cat Dockerfile | docker build -
|
||||||
|
Build a new image from the Dockerfile passed via stdin
|
||||||
@@ -15,7 +15,8 @@ This documentation has the following resources:
|
|||||||
examples/index
|
examples/index
|
||||||
contributing/index
|
contributing/index
|
||||||
commandline/index
|
commandline/index
|
||||||
|
builder/index
|
||||||
faq
|
faq
|
||||||
|
|
||||||
|
|
||||||
.. image:: http://www.docker.io/_static/lego_docker.jpg
|
.. image:: http://www.docker.io/_static/lego_docker.jpg
|
||||||
|
|||||||
+2
-91
@@ -12,7 +12,6 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Capabilities struct {
|
type Capabilities struct {
|
||||||
@@ -77,67 +76,6 @@ func (runtime *Runtime) containerRoot(id string) string {
|
|||||||
return path.Join(runtime.repository, id)
|
return path.Join(runtime.repository, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (runtime *Runtime) Create(config *Config) (*Container, error) {
|
|
||||||
// Lookup image
|
|
||||||
img, err := runtime.repositories.LookupImage(config.Image)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// 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]
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// Step 3: register the container
|
|
||||||
if err := runtime.Register(container); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return container, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (runtime *Runtime) Load(id string) (*Container, error) {
|
func (runtime *Runtime) Load(id string) (*Container, error) {
|
||||||
container := &Container{root: runtime.containerRoot(id)}
|
container := &Container{root: runtime.containerRoot(id)}
|
||||||
if err := container.FromDisk(); err != nil {
|
if err := container.FromDisk(); err != nil {
|
||||||
@@ -247,33 +185,6 @@ func (runtime *Runtime) Destroy(container *Container) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit creates a new filesystem image from the current state of a container.
|
|
||||||
// The image can optionally be tagged into a repository
|
|
||||||
func (runtime *Runtime) Commit(id, repository, tag, comment, author string) (*Image, error) {
|
|
||||||
container := runtime.Get(id)
|
|
||||||
if container == nil {
|
|
||||||
return nil, fmt.Errorf("No such container: %s", id)
|
|
||||||
}
|
|
||||||
// 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
|
|
||||||
img, err := runtime.graph.Create(rwTar, container, comment, author)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// Register the image if needed
|
|
||||||
if repository != "" {
|
|
||||||
if err := runtime.repositories.Set(repository, tag, img.Id, true); err != nil {
|
|
||||||
return img, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return img, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (runtime *Runtime) restore() error {
|
func (runtime *Runtime) restore() error {
|
||||||
dir, err := ioutil.ReadDir(runtime.repository)
|
dir, err := ioutil.ReadDir(runtime.repository)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -314,13 +225,13 @@ func NewRuntime() (*Runtime, error) {
|
|||||||
_, err2 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.soft_limit_in_bytes"))
|
_, err2 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.soft_limit_in_bytes"))
|
||||||
runtime.capabilities.MemoryLimit = err1 == nil && err2 == nil
|
runtime.capabilities.MemoryLimit = err1 == nil && err2 == nil
|
||||||
if !runtime.capabilities.MemoryLimit {
|
if !runtime.capabilities.MemoryLimit {
|
||||||
log.Printf("WARNING: Your kernel does not support cgroup memory limit.")
|
log.Printf("WARNING: Your kernel does not support cgroup memory limit.")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.memsw.limit_in_bytes"))
|
_, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.memsw.limit_in_bytes"))
|
||||||
runtime.capabilities.SwapLimit = err == nil
|
runtime.capabilities.SwapLimit = err == nil
|
||||||
if !runtime.capabilities.SwapLimit {
|
if !runtime.capabilities.SwapLimit {
|
||||||
log.Printf("WARNING: Your kernel does not support cgroup swap limit.")
|
log.Printf("WARNING: Your kernel does not support cgroup swap limit.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return runtime, nil
|
return runtime, nil
|
||||||
|
|||||||
+13
-8
@@ -116,7 +116,7 @@ func TestRuntimeCreate(t *testing.T) {
|
|||||||
if len(runtime.List()) != 0 {
|
if len(runtime.List()) != 0 {
|
||||||
t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
|
t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
|
||||||
}
|
}
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"ls", "-al"},
|
Cmd: []string{"ls", "-al"},
|
||||||
},
|
},
|
||||||
@@ -163,7 +163,7 @@ func TestDestroy(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"ls", "-al"},
|
Cmd: []string{"ls", "-al"},
|
||||||
},
|
},
|
||||||
@@ -210,7 +210,10 @@ func TestGet(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container1, err := runtime.Create(&Config{
|
|
||||||
|
builder := NewBuilder(runtime)
|
||||||
|
|
||||||
|
container1, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"ls", "-al"},
|
Cmd: []string{"ls", "-al"},
|
||||||
},
|
},
|
||||||
@@ -220,7 +223,7 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer runtime.Destroy(container1)
|
defer runtime.Destroy(container1)
|
||||||
|
|
||||||
container2, err := runtime.Create(&Config{
|
container2, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"ls", "-al"},
|
Cmd: []string{"ls", "-al"},
|
||||||
},
|
},
|
||||||
@@ -230,7 +233,7 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer runtime.Destroy(container2)
|
defer runtime.Destroy(container2)
|
||||||
|
|
||||||
container3, err := runtime.Create(&Config{
|
container3, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"ls", "-al"},
|
Cmd: []string{"ls", "-al"},
|
||||||
},
|
},
|
||||||
@@ -260,7 +263,7 @@ func TestAllocatePortLocalhost(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
container, err := runtime.Create(&Config{
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
Image: GetTestImage(runtime).Id,
|
Image: GetTestImage(runtime).Id,
|
||||||
Cmd: []string{"sh", "-c", "echo well hello there | nc -l -p 5555"},
|
Cmd: []string{"sh", "-c", "echo well hello there | nc -l -p 5555"},
|
||||||
PortSpecs: []string{"5555"},
|
PortSpecs: []string{"5555"},
|
||||||
@@ -313,8 +316,10 @@ func TestRestore(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
builder := NewBuilder(runtime1)
|
||||||
|
|
||||||
// Create a container with one instance of docker
|
// Create a container with one instance of docker
|
||||||
container1, err := runtime1.Create(&Config{
|
container1, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime1).Id,
|
Image: GetTestImage(runtime1).Id,
|
||||||
Cmd: []string{"ls", "-al"},
|
Cmd: []string{"ls", "-al"},
|
||||||
},
|
},
|
||||||
@@ -325,7 +330,7 @@ func TestRestore(t *testing.T) {
|
|||||||
defer runtime1.Destroy(container1)
|
defer runtime1.Destroy(container1)
|
||||||
|
|
||||||
// Create a second container meant to be killed
|
// Create a second container meant to be killed
|
||||||
container2, err := runtime1.Create(&Config{
|
container2, err := builder.Create(&Config{
|
||||||
Image: GetTestImage(runtime1).Id,
|
Image: GetTestImage(runtime1).Id,
|
||||||
Cmd: []string{"/bin/cat"},
|
Cmd: []string{"/bin/cat"},
|
||||||
OpenStdin: true,
|
OpenStdin: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user