Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Docker マルチステージビルドで幸せコンテナライフ / Understanding doc...

Docker マルチステージビルドで幸せコンテナライフ / Understanding docker's multi-stage builds

Docker v17.05 から導入された multi-stage builds 機能とは何か、どんな問題を解決するためのものなのかをまとめました.

Presented at Docker Tokyo.

Tori Hara

May 25, 2017
Tweet

More Decks by Tori Hara

Other Decks in Technology

Transcript

  1. SUPINF Inc. // twitter.com/toricls # Artifacts Builder FROM golang:1.8.2 AS

    builder WORKDIR /go/src/github.com/toricls/repos COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . # Runner FROM scratch COPY --from=builder /go/src/github.com/toricls/repos/app / CMD ["/app"] multi-stage builds ./Dockerfile
  2. SUPINF Inc. // twitter.com/toricls # Artifacts Builder FROM golang:1.8.2 AS

    builder WORKDIR /go/src/github.com/toricls/repos COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . # Runner FROM scratch COPY --from=builder /go/src/github.com/toricls/repos/app / CMD ["/app"] multi-stage builds 1 Ƒƛ Dockerfile ˰ƘґϺƆƸ 2 ƑƛǷȏǭǗȗǪǑǝǬ
  3. SUPINF Inc. // twitter.com/toricls FROM golang:1.8.2 WORKDIR /go/src/github.com/toricls/repos RUN go

    get -d -v github.com/pkg/errors COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . CMD ["./app"] Ɩƹżƶű۪̌ȥŹƷƛLjȅȜǜǙLjǞƘ౐űŹůƸŸ FROM golang:1.8.2 AS builder WORKDIR /go/src/github.com/toricls/repos RUN go get -d -v github.com/pkg/errors COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . FROM scratch COPY --from=builder /go/src/github.com/ toricls/repos/app / CMD ["/app"] ڰ఻ƘǷȏǭƄƌƱƑ multi-stage buildsƄƌƱƑ
  4. SUPINF Inc. // twitter.com/toricls FROM golang:1.8.2 WORKDIR /go/src/github.com/toricls/repos RUN go

    get -d -v github.com/pkg/errors COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . CMD ["./app"] Ɩƹżƶű۪̌ȥŹƷƛLjȅȜǜǙLjǞƘ౐űŹůƸŸ FROM golang:1.8.2 AS builder WORKDIR /go/src/github.com/toricls/repos RUN go get -d -v github.com/pkg/errors COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . FROM scratch COPY --from=builder /go/src/github.com/ toricls/repos/app / CMD ["/app"] ڰ఻ƘǷȏǭƄƌƱƑ multi-stage buildƄƌƱƑ + +
  5. SUPINF Inc. // twitter.com/toricls ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛ̵̰\T ▶︎ ǽȜǝLjȅȜǜƿ఍ಊƘƆƸ ▶︎ Alpine Linux,

    Scratch... ▶︎ ࡒ൜ƗƯƛƿऊż ▶︎ ޠΙű ▶︎ Ƿȏǭࣄƕҩ଻ࣄƔ̘ƛLjȅȜǜƿʎų
  6. SUPINF Inc. // twitter.com/toricls ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛ̵̰\T ▶︎ ǽȜǝLjȅȜǜƿ఍ಊƘƆƸ ▶︎ Alpine Linux,

    Scratch... ▶︎ ࡒ൜ƗƯƛƿऊż ▶︎ ޠΙű ▶︎ Ƿȏǭࣄƕҩ଻ࣄƔ̘ƛLjȅȜǜƿʎų ࡽƘƀƀƘ ߦŽƫƄṵ̵̋Ź
  7. SUPINF Inc. // twitter.com/toricls ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛ̵̰\T   SGJBCP.?RRCPL%MJ?LE FROM golang:1.8.2

    WORKDIR /go/src/github.com/toricls/repos RUN go get -d -v github.com/pkg/errors COPY app.go . RUN CGO_ENABLED=0 GOOS=linux \ go build -a -installsuffix cgo -o app . FROM scratch COPY app / CMD ["/app"] ./Dockerfile.build ./Dockerfile (ҩ଻ࣄ)
  8. SUPINF Inc. // twitter.com/toricls ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛ̵̰\T   SGJBCP.?RRCPL%MJ?LE `Builder pattern`

    ƛҩࢨƘƜGlueǗȜǭࣶƗƯƛŹքୟ #!/bin/sh #!/bin/sh # Builder docker build -t my-image:builder . -f Dockerfile.build #!/bin/sh # Builder docker build -t my-image:builder . -f Dockerfile.build # Extract artifacts docker create --name artifacts my-image:builder #!/bin/sh # Builder docker build -t my-image:builder . -f Dockerfile.build # Extract artifacts docker create --name artifacts my-image:builder docker cp artifacts:/go/src/github.com/toricls/repos/app ./app #!/bin/sh # Builder docker build -t my-image:builder . -f Dockerfile.build # Extract artifacts docker create --name artifacts my-image:builder docker cp artifacts:/go/src/github.com/toricls/repos/app ./app docker rm -f artifacts #!/bin/sh # Builder docker build -t my-image:builder . -f Dockerfile.build # Extract artifacts docker create --name artifacts my-image:builder docker cp artifacts:/go/src/github.com/toricls/repos/app ./app docker rm -f artifacts # Build runtime image docker build --no-cache -t my-image:latest . -f Dockerfile
  9. SUPINF Inc. // twitter.com/toricls FROM ruby:2.4.1-alpine # Install Runtime Packages

    libxslt, yaml, postgresql-client ... ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛƌƳƫƙ̵̰\T   SGJBCP.?RRCPL0S@WML0?GJQ ./Dockerfile.base ./Dockerfile.builder ./Dockerfile.base.builder ./Dockerfile (ҩ଻ࣄ) FROM my-base:latest # Install Development Packages libxslt-dev, yaml-dev, postgresql-dev ... FROM my-base:latest WORKDIR /app COPY . /app CMD ["bundle", "exec", "rails", "server"... FROM my-base-builder:latest # Install Gems, Compile Assets ...
  10. SUPINF Inc. // twitter.com/toricls FROM ruby:2.4.1-alpine # Install Runtime Packages

    ... FROM my-base:latest # Install Development Packages ... FROM my-base:latest WORKDIR /app COPY . /app CMD ["bundle", "exec", "rails", "server"... FROM my-base-builder:latest # Install Gems, Compile Assets ... ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛƌƳƫƙ̵̰\T   SGJBCP.?RRCPL0S@WML0?GJQ ./Dockerfile.base ./Dockerfile.builder ./Dockerfile.base.builder ./Dockerfile
  11. SUPINF Inc. // twitter.com/toricls # Artifacts Builder FROM golang:1.8.2 AS

    builder WORKDIR /go/src/github.com/toricls/repos RUN go get -d -v github.com/pkg/errors COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . # Runner FROM scratch COPY --from=builder /go/src/github.com/toricls/repos/app / CMD ["/app"] multi-stage builds ./Dockerfile
  12. SUPINF Inc. // twitter.com/toricls References ▶︎ Use multi-stage builds -

    Docker Documentation ▶︎ https://docs.docker.com/engine/userguide/eng-image/multistage-build/ ▶︎ Builder pattern vs. Multi-stage builds in Docker ▶︎ http://blog.alexellis.io/mutli-stage-docker-builds/