docker/docker — pull request #8021 by tonistiigi (2014-09-13 18:56:49+00:00)
Title: Support for nested build steps

This is an nested build implementation roughly based on #7149 by @proppy

Consider a Dockerfile:

```
FROM crosbymichael/golang
ADD . /go/src/github.com/tonistiigi/dnsdock
ENV CGO_ENABLED 0
RUN cd /go/src/github.com/tonistiigi/dnsdock && \
go get -d ./... && \
go install -a ./...

BUILD /go/bin

FROM busybox
ADD . /bin
ENTRYPOINT ["/bin/dnsdock"]
```

Running `docker build -t tonistiigi/dnsdock .` will result in the creation of following images:

```
> docker images
tonistiigi/dnsdock latest 3f7a44c4701a 6 seconds ago 10.28 MB
tonistiigi/dnsdock-builder latest dac44acfcdd9 10 seconds ago 474.7 MB
```

The final image is minimal busybox, containing only a binary compiled by previous image. Builder layers are kept to keep build cache working.

Parent build step can be also used for dynamic Dockerfile creation. In this case `BUILD` instruction needs to be the final instruction in the Dockerfile.

```
FROM ubuntu
ADD . /src
RUN /src/build-new-dockerfile.sh > /src/Dockerfile
BUILD /src
```

Number of build steps isn't limited. Last image gets the tag.

```
cat < Dockerfile
FROM busybox
RUN /bin/mkdir -p /out && /bin/touch /out/a
BUILD /out

FROM busybox
ADD . /out
RUN /bin/mkdir -p /out && /bin/touch /out/b
BUILD /out

FROM busybox
ADD . /out
RUN /bin/mkdir -p /out && /bin/touch /out/c

EOT

docker build -t abc .
docker run abc /bin/ls /out

# prints: a b c
```

I realize that the proposals with different solutions for fixing this problem have not yet been agreed on by the maintainers. Just hoping that having one implementation ready for testing/review brings the final solution for minimal containers closer for everybody.