mirror of https://github.com/docker/docs.git
Create a layer for each operation (expose, cmd, etc)
This commit is contained in:
parent
ae0d555022
commit
b6165daa77
42
buildfile.go
42
buildfile.go
|
@ -32,8 +32,6 @@ type buildFile struct {
|
||||||
tmpContainers map[string]struct{}
|
tmpContainers map[string]struct{}
|
||||||
tmpImages map[string]struct{}
|
tmpImages map[string]struct{}
|
||||||
|
|
||||||
needCommit bool
|
|
||||||
|
|
||||||
out io.Writer
|
out io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,9 +79,8 @@ func (b *buildFile) CmdFrom(name string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildFile) CmdMaintainer(name string) error {
|
func (b *buildFile) CmdMaintainer(name string) error {
|
||||||
b.needCommit = true
|
|
||||||
b.maintainer = name
|
b.maintainer = name
|
||||||
return nil
|
return b.commit("", b.config.Cmd, fmt.Sprintf("MAINTAINER %s", name))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildFile) CmdRun(args string) error {
|
func (b *buildFile) CmdRun(args string) error {
|
||||||
|
@ -113,7 +110,7 @@ func (b *buildFile) CmdRun(args string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := b.commit(cid, cmd); err != nil {
|
if err := b.commit(cid, cmd, "run"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b.config.Cmd = cmd
|
b.config.Cmd = cmd
|
||||||
|
@ -121,7 +118,6 @@ func (b *buildFile) CmdRun(args string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildFile) CmdEnv(args string) error {
|
func (b *buildFile) CmdEnv(args string) error {
|
||||||
b.needCommit = true
|
|
||||||
tmp := strings.SplitN(args, " ", 2)
|
tmp := strings.SplitN(args, " ", 2)
|
||||||
if len(tmp) != 2 {
|
if len(tmp) != 2 {
|
||||||
return fmt.Errorf("Invalid ENV format")
|
return fmt.Errorf("Invalid ENV format")
|
||||||
|
@ -136,29 +132,25 @@ func (b *buildFile) CmdEnv(args string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.config.Env = append(b.config.Env, key+"="+value)
|
b.config.Env = append(b.config.Env, key+"="+value)
|
||||||
return nil
|
return b.commit("", b.config.Cmd, fmt.Sprintf("ENV %s=%s", key, value))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildFile) CmdCmd(args string) error {
|
func (b *buildFile) CmdCmd(args string) error {
|
||||||
b.needCommit = true
|
|
||||||
var cmd []string
|
var cmd []string
|
||||||
if err := json.Unmarshal([]byte(args), &cmd); err != nil {
|
if err := json.Unmarshal([]byte(args), &cmd); err != nil {
|
||||||
utils.Debugf("Error unmarshalling: %s, using /bin/sh -c", err)
|
utils.Debugf("Error unmarshalling: %s, using /bin/sh -c", err)
|
||||||
b.config.Cmd = []string{"/bin/sh", "-c", args}
|
cmd = []string{"/bin/sh", "-c", args}
|
||||||
} else {
|
|
||||||
b.config.Cmd = cmd
|
|
||||||
}
|
}
|
||||||
return nil
|
return b.commit("", cmd, fmt.Sprintf("CMD %v", cmd))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildFile) CmdExpose(args string) error {
|
func (b *buildFile) CmdExpose(args string) error {
|
||||||
ports := strings.Split(args, " ")
|
ports := strings.Split(args, " ")
|
||||||
b.config.PortSpecs = append(ports, b.config.PortSpecs...)
|
b.config.PortSpecs = append(ports, b.config.PortSpecs...)
|
||||||
return nil
|
return b.commit("", b.config.Cmd, fmt.Sprintf("EXPOSE %v", ports))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildFile) CmdInsert(args string) error {
|
func (b *buildFile) CmdInsert(args string) error {
|
||||||
|
|
||||||
if b.image == "" {
|
if b.image == "" {
|
||||||
return fmt.Errorf("Please provide a source image with `from` prior to insert")
|
return fmt.Errorf("Please provide a source image with `from` prior to insert")
|
||||||
}
|
}
|
||||||
|
@ -176,7 +168,7 @@ func (b *buildFile) CmdInsert(args string) error {
|
||||||
defer file.Body.Close()
|
defer file.Body.Close()
|
||||||
|
|
||||||
cmd := b.config.Cmd
|
cmd := b.config.Cmd
|
||||||
b.config.Cmd = []string{"echo", "INSERT", sourceUrl, "in", destPath}
|
b.config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) INSERT %s in %s", sourceUrl, destPath)}
|
||||||
cid, err := b.run()
|
cid, err := b.run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -190,8 +182,7 @@ func (b *buildFile) CmdInsert(args string) error {
|
||||||
if err := container.Inject(file.Body, destPath); err != nil {
|
if err := container.Inject(file.Body, destPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return b.commit(cid, cmd, fmt.Sprintf("INSERT %s in %s", sourceUrl, destPath))
|
||||||
return b.commit(cid, cmd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildFile) CmdAdd(args string) error {
|
func (b *buildFile) CmdAdd(args string) error {
|
||||||
|
@ -206,7 +197,7 @@ func (b *buildFile) CmdAdd(args string) error {
|
||||||
dest := strings.Trim(tmp[1], " ")
|
dest := strings.Trim(tmp[1], " ")
|
||||||
|
|
||||||
cmd := b.config.Cmd
|
cmd := b.config.Cmd
|
||||||
b.config.Cmd = []string{"echo", "PUSH", orig, "in", dest}
|
b.config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) ADD %s in %s", orig, dest)}
|
||||||
cid, err := b.run()
|
cid, err := b.run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -243,8 +234,7 @@ func (b *buildFile) CmdAdd(args string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return b.commit(cid, cmd, fmt.Sprintf("ADD %s in %s", orig, dest))
|
||||||
return b.commit(cid, cmd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildFile) run() (string, error) {
|
func (b *buildFile) run() (string, error) {
|
||||||
|
@ -274,20 +264,18 @@ func (b *buildFile) run() (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit the container <id> with the autorun command <autoCmd>
|
// Commit the container <id> with the autorun command <autoCmd>
|
||||||
func (b *buildFile) commit(id string, autoCmd []string) error {
|
func (b *buildFile) commit(id string, autoCmd []string, comment string) error {
|
||||||
if b.image == "" {
|
if b.image == "" {
|
||||||
return fmt.Errorf("Please provide a source image with `from` prior to commit")
|
return fmt.Errorf("Please provide a source image with `from` prior to commit")
|
||||||
}
|
}
|
||||||
b.config.Image = b.image
|
b.config.Image = b.image
|
||||||
if id == "" {
|
if id == "" {
|
||||||
cmd := b.config.Cmd
|
b.config.Cmd = []string{"/bin/sh", "-c", "#(nop) " + comment}
|
||||||
b.config.Cmd = []string{"true"}
|
|
||||||
if cid, err := b.run(); err != nil {
|
if cid, err := b.run(); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
id = cid
|
id = cid
|
||||||
}
|
}
|
||||||
b.config.Cmd = cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
container := b.runtime.Get(id)
|
container := b.runtime.Get(id)
|
||||||
|
@ -305,7 +293,6 @@ func (b *buildFile) commit(id string, autoCmd []string) error {
|
||||||
}
|
}
|
||||||
b.tmpImages[image.Id] = struct{}{}
|
b.tmpImages[image.Id] = struct{}{}
|
||||||
b.image = image.Id
|
b.image = image.Id
|
||||||
b.needCommit = false
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -358,11 +345,6 @@ func (b *buildFile) Build(dockerfile, context io.Reader) (string, error) {
|
||||||
|
|
||||||
fmt.Fprintf(b.out, "===> %v\n", b.image)
|
fmt.Fprintf(b.out, "===> %v\n", b.image)
|
||||||
}
|
}
|
||||||
if b.needCommit {
|
|
||||||
if err := b.commit("", nil); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if b.image != "" {
|
if b.image != "" {
|
||||||
// The build is successful, keep the temporary containers and images
|
// The build is successful, keep the temporary containers and images
|
||||||
for i := range b.tmpImages {
|
for i := range b.tmpImages {
|
||||||
|
|
Loading…
Reference in New Issue