Oded Ben Noon
Oded Ben Noon

Reputation: 35

Using files from two Base images

I have two docker images. Image x just copies some files to the image, Image y copies other files and install some needed libs. I want to build third dockerfile=z that will be based on those two images.

example for z:

From x . .

From y . .

How do I build dockerfile z that can use those files from x and y?

Upvotes: 0

Views: 83

Answers (2)

Kapil Khandelwal
Kapil Khandelwal

Reputation: 1176

Try this:

FROM x as image1

FROM y as image2

FROM centos

COPY --from=image1 <source_path> <destinantion_path>

COPY --from=image2 <source_path> <destinantion_path>

Reference

Upvotes: 0

vijay v
vijay v

Reputation: 2076

Contents below are from the Docker official site.

Use multi-stage builds

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image. To show how this works, let’s adapt the Dockerfile from the previous section to use multi-stage builds.

Dockerfile:

FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]

You only need the single Dockerfile. You don’t need a separate build script, either. Just run docker build.

$ docker build -t alexellis2/href-counter:latest .

The end result is the same tiny production image as before, with a significant reduction in complexity. You don’t need to create any intermediate images and you don’t need to extract any artifacts to your local system at all.

How does it work? The second FROM instruction starts a new build stage with the alpine:latest image as its base. The COPY --from=0 line copies just the built artifact from the previous stage into this new stage. The Go SDK and any intermediate artifacts are left behind, and not saved in the final image.

Source: Docker Multi Stage Builds

Upvotes: 1

Related Questions