mirror of https://github.com/docker/docs.git
Add "Cmd" prefix to builder instructions
This commit is contained in:
parent
0f312113d3
commit
c2a14bb196
4
api.go
4
api.go
|
@ -590,6 +590,10 @@ func postImagesGetCache(srv *Server, w http.ResponseWriter, r *http.Request, var
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if image == nil {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
apiId := &ApiId{Id: image.Id}
|
apiId := &ApiId{Id: image.Id}
|
||||||
b, err := json.Marshal(apiId)
|
b, err := json.Marshal(apiId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -13,7 +13,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type BuilderClient struct {
|
type BuilderClient struct {
|
||||||
builder *Builder
|
|
||||||
cli *DockerCli
|
cli *DockerCli
|
||||||
|
|
||||||
image string
|
image string
|
||||||
|
@ -28,17 +27,20 @@ type BuilderClient struct {
|
||||||
|
|
||||||
func (b *BuilderClient) clearTmp(containers, images map[string]struct{}) {
|
func (b *BuilderClient) clearTmp(containers, images map[string]struct{}) {
|
||||||
for c := range containers {
|
for c := range containers {
|
||||||
tmp := b.builder.runtime.Get(c)
|
if _, _, err := b.cli.call("DELETE", "/containers/"+c, nil); err != nil {
|
||||||
b.builder.runtime.Destroy(tmp)
|
utils.Debugf("%s", err)
|
||||||
|
}
|
||||||
utils.Debugf("Removing container %s", c)
|
utils.Debugf("Removing container %s", c)
|
||||||
}
|
}
|
||||||
for i := range images {
|
for i := range images {
|
||||||
b.builder.runtime.graph.Delete(i)
|
if _, _, err := b.cli.call("DELETE", "/images/"+i, nil); err != nil {
|
||||||
|
utils.Debugf("%s", err)
|
||||||
|
}
|
||||||
utils.Debugf("Removing image %s", i)
|
utils.Debugf("Removing image %s", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BuilderClient) From(name string) error {
|
func (b *BuilderClient) CmdFrom(name string) error {
|
||||||
obj, statusCode, err := b.cli.call("GET", "/images/"+name+"/json", nil)
|
obj, statusCode, err := b.cli.call("GET", "/images/"+name+"/json", nil)
|
||||||
if statusCode == 404 {
|
if statusCode == 404 {
|
||||||
if err := b.cli.hijack("POST", "/images/create?fromImage="+name, false); err != nil {
|
if err := b.cli.hijack("POST", "/images/create?fromImage="+name, false); err != nil {
|
||||||
|
@ -53,7 +55,7 @@ func (b *BuilderClient) From(name string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
img := &ApiImages{}
|
img := &ApiId{}
|
||||||
if err := json.Unmarshal(obj, img); err != nil {
|
if err := json.Unmarshal(obj, img); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -61,17 +63,17 @@ func (b *BuilderClient) From(name string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BuilderClient) Maintainer(name string) error {
|
func (b *BuilderClient) CmdMaintainer(name string) error {
|
||||||
b.needCommit = true
|
b.needCommit = true
|
||||||
b.maintainer = name
|
b.maintainer = name
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BuilderClient) Run(args string) error {
|
func (b *BuilderClient) CmdRun(args string) error {
|
||||||
if b.image == "" {
|
if b.image == "" {
|
||||||
return 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")
|
||||||
}
|
}
|
||||||
config, _, err := ParseRun([]string{b.image, "/bin/sh", "-c", args}, b.builder.runtime.capabilities)
|
config, _, err := ParseRun([]string{b.image, "/bin/sh", "-c", args}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -90,8 +92,49 @@ func (b *BuilderClient) Run(args string) error {
|
||||||
b.image = apiId.Id
|
b.image = apiId.Id
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
b.commit()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
body, _, err = b.cli.call("POST", "/containers/create", b.config)
|
func (b *BuilderClient) CmdEnv(args string) error {
|
||||||
|
b.needCommit = true
|
||||||
|
tmp := strings.SplitN(args, " ", 2)
|
||||||
|
if len(tmp) != 2 {
|
||||||
|
return fmt.Errorf("Invalid ENV format")
|
||||||
|
}
|
||||||
|
key := strings.Trim(tmp[0], " ")
|
||||||
|
value := strings.Trim(tmp[1], " ")
|
||||||
|
|
||||||
|
for i, elem := range b.config.Env {
|
||||||
|
if strings.HasPrefix(elem, key+"=") {
|
||||||
|
b.config.Env[i] = key + "=" + value
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.config.Env = append(b.config.Env, key+"="+value)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BuilderClient) CmdCmd(args string) error {
|
||||||
|
b.needCommit = true
|
||||||
|
b.config.Cmd = []string{"/bin/sh", "-c", args}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BuilderClient) CmdExpose(args string) error {
|
||||||
|
ports := strings.Split(args, " ")
|
||||||
|
b.config.PortSpecs = append(ports, b.config.PortSpecs...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BuilderClient) CmdInsert(args string) error {
|
||||||
|
// FIXME: Reimplement this once the remove_hijack branch gets merged.
|
||||||
|
// We need to retrieve the resulting Id
|
||||||
|
return fmt.Errorf("INSERT not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BuilderClient) commit() error {
|
||||||
|
body, _, err := b.cli.call("POST", "/containers/create", b.config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -134,53 +177,11 @@ func (b *BuilderClient) Run(args string) error {
|
||||||
}
|
}
|
||||||
b.tmpImages[apiId.Id] = struct{}{}
|
b.tmpImages[apiId.Id] = struct{}{}
|
||||||
b.image = apiId.Id
|
b.image = apiId.Id
|
||||||
b.needCommit = false
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BuilderClient) Env(args string) error {
|
func (b *BuilderClient) Build(dockerfile io.Reader) (string, error) {
|
||||||
b.needCommit = true
|
|
||||||
tmp := strings.SplitN(args, " ", 2)
|
|
||||||
if len(tmp) != 2 {
|
|
||||||
return fmt.Errorf("Invalid ENV format")
|
|
||||||
}
|
|
||||||
key := strings.Trim(tmp[0], " ")
|
|
||||||
value := strings.Trim(tmp[1], " ")
|
|
||||||
|
|
||||||
for i, elem := range b.config.Env {
|
|
||||||
if strings.HasPrefix(elem, key+"=") {
|
|
||||||
b.config.Env[i] = key + "=" + value
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b.config.Env = append(b.config.Env, key+"="+value)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BuilderClient) Cmd(args string) error {
|
|
||||||
b.needCommit = true
|
|
||||||
b.config.Cmd = []string{"/bin/sh", "-c", args}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BuilderClient) Expose(args string) error {
|
|
||||||
ports := strings.Split(args, " ")
|
|
||||||
b.config.PortSpecs = append(ports, b.config.PortSpecs...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BuilderClient) Insert(args string) error {
|
|
||||||
// FIXME: Reimplement this once the remove_hijack branch gets merged.
|
|
||||||
// We need to retrieve the resulting Id
|
|
||||||
return fmt.Errorf("INSERT not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBuilderClient(dockerfile io.Reader) (string, error) {
|
|
||||||
// defer b.clearTmp(tmpContainers, tmpImages)
|
// defer b.clearTmp(tmpContainers, tmpImages)
|
||||||
|
|
||||||
b := &BuilderClient{
|
|
||||||
cli: NewDockerCli("0.0.0.0", 4243),
|
|
||||||
}
|
|
||||||
file := bufio.NewReader(dockerfile)
|
file := bufio.NewReader(dockerfile)
|
||||||
for {
|
for {
|
||||||
line, err := file.ReadString('\n')
|
line, err := file.ReadString('\n')
|
||||||
|
@ -204,7 +205,7 @@ func NewBuilderClient(dockerfile io.Reader) (string, error) {
|
||||||
|
|
||||||
fmt.Printf("%s %s\n", strings.ToUpper(instruction), arguments)
|
fmt.Printf("%s %s\n", strings.ToUpper(instruction), arguments)
|
||||||
|
|
||||||
method, exists := reflect.TypeOf(b).MethodByName(strings.ToUpper(instruction[:1]) + strings.ToLower(instruction[1:]))
|
method, exists := reflect.TypeOf(b).MethodByName("Cmd" + strings.ToUpper(instruction[:1]) + strings.ToLower(instruction[1:]))
|
||||||
if !exists {
|
if !exists {
|
||||||
fmt.Printf("Skipping unknown instruction %s\n", strings.ToUpper(instruction))
|
fmt.Printf("Skipping unknown instruction %s\n", strings.ToUpper(instruction))
|
||||||
}
|
}
|
||||||
|
@ -216,49 +217,7 @@ func NewBuilderClient(dockerfile io.Reader) (string, error) {
|
||||||
fmt.Printf("===> %v\n", b.image)
|
fmt.Printf("===> %v\n", b.image)
|
||||||
}
|
}
|
||||||
if b.needCommit {
|
if b.needCommit {
|
||||||
body, _, err = b.cli.call("POST", "/containers/create", b.config)
|
b.commit()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
out := &ApiRun{}
|
|
||||||
err = json.Unmarshal(body, out)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, warning := range out.Warnings {
|
|
||||||
fmt.Fprintln(os.Stderr, "WARNING: ", warning)
|
|
||||||
}
|
|
||||||
|
|
||||||
//start the container
|
|
||||||
_, _, err = b.cli.call("POST", "/containers/"+out.Id+"/start", nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
b.tmpContainers[out.Id] = struct{}{}
|
|
||||||
|
|
||||||
// Wait for it to finish
|
|
||||||
_, _, err = b.cli.call("POST", "/containers/"+out.Id+"/wait", nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Commit the container
|
|
||||||
v := url.Values{}
|
|
||||||
v.Set("container", out.Id)
|
|
||||||
v.Set("author", b.maintainer)
|
|
||||||
body, _, err = b.cli.call("POST", "/commit?"+v.Encode(), b.config)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
apiId := &ApiId{}
|
|
||||||
err = json.Unmarshal(body, apiId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
b.tmpImages[apiId.Id] = struct{}{}
|
|
||||||
b.image = apiId.Id
|
|
||||||
}
|
}
|
||||||
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
|
||||||
|
@ -273,3 +232,12 @@ func NewBuilderClient(dockerfile io.Reader) (string, error) {
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("An error occured during the build\n")
|
return "", fmt.Errorf("An error occured during the build\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewBuilderClient(addr string, port int) *BuilderClient {
|
||||||
|
return &BuilderClient{
|
||||||
|
cli: NewDockerCli(addr, port),
|
||||||
|
config: &Config{},
|
||||||
|
tmpContainers: make(map[string]struct{}),
|
||||||
|
tmpImages: make(map[string]struct{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -134,7 +134,9 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NewBuilderClient(file)
|
if _, err := NewBuilderClient("0.0.0.0", 4243).Build(file); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue