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

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

84907687e50c8ac2a09b02e0d1b36ab1?s=47 Tori
May 25, 2017

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

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

Presented at Docker Tokyo.

84907687e50c8ac2a09b02e0d1b36ab1?s=128

Tori

May 25, 2017
Tweet

Transcript

  1. SUPINF Inc. // twitter.com/toricls multi-stage builds Yasuhiro HARA May 23,

    2017
  2. SUPINF Inc. // twitter.com/toricls multi-stage builds (v17.05~)

  3. SUPINF Inc. // twitter.com/toricls Multi-stage builds ୘ٵƛǷȏǭǗȗǪǑǝǬ೐Ɣ ǹDžLjȏƛƱƷ΂ƷŹƔźƸݮ੧

  4. 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
  5. 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 ƑƛǷȏǭǗȗǪǑǝǬ
  6. SUPINF Inc. // twitter.com/toricls Multi-stage builds ୘ٵƛǷȏǭǗȗǪǑǝǬ೐ƛ ʓґ֕ƿ৤इƘ୴మƂƈƓżƹƸݮ੧

  7. SUPINF Inc. // twitter.com/toricls multi-stage builds https://www.slideshare.net/Docker/dockercon-2017-general-session-day-1-solomon-hykes-75362520

  8. SUPINF Inc. // twitter.com/toricls multi-stage builds Ɩƹżƶű౐ųƛ෇

  9. 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ƄƌƱƑ
  10. 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ƄƌƱƑ + +
  11. SUPINF Inc. // twitter.com/toricls

  12. SUPINF Inc. // twitter.com/toricls multi-stage buildsɪ̣ƛɛൂƛњ೗ƛދΒ

  13. SUPINF Inc. // twitter.com/toricls ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸ̵̰ (~v17.03)

  14. SUPINF Inc. // twitter.com/toricls ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛ̵̰\T ▶︎ ǽȜǝLjȅȜǜƿ఍ಊƘƆƸ ▶︎ Alpine Linux,

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

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

  17. SUPINF Inc. // twitter.com/toricls ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛ̵̰\T Builder Pattern - Golang -

  18. 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 (ҩ଻ࣄ)
  19. 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
  20. SUPINF Inc. // twitter.com/toricls ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛ̵̰\T GoƜǴLjǮȎ׼˥ƆƹƝűűƍƺųžƖ ɤƛ୭கƍƕƖųƄƓƌƛ෇

  21. SUPINF Inc. // twitter.com/toricls ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿʊƸƌƮƛƌƳƫƙ̵̰\T Builder Pattern - Ruby on

    Rails -
  22. 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 ...
  23. 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
  24. SUPINF Inc. // twitter.com/toricls multi-stage builds

  25. 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
  26. SUPINF Inc. // twitter.com/toricls ƫƕƮ multi-stage builds ƿȥ׮Ƙ̙ࣄƄƓ ఍ಊƔࡒ൜ƛƗűLjȅȜǜƿ ɟƫƔƵƷƯݓƘ

    ׮Ƙ˥ƹƫƄƴų
  27. SUPINF Inc. // twitter.com/toricls References

  28. 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/
  29. SUPINF Inc. // twitter.com/toricls Thank you :)