Dockerfileãæ¸ããã«Buildpacksã§å§åçã«è»½éãªDockerã¤ã¡ã¼ã¸ãä½æãã(539MB->245MB)
ã¯ããã«
2018 å¹´ 10 æã« Cloud Native Buildpacks 㯠Cloud Native Computing Foundation (CNCF)ã« Sandbox ã¨ãã¦åãå ¥ãããã¾ããã CNCF ã«ã¯ Kubernetes, Prometheus, Envoy, Fluentd ãªã©æåããã¸ã§ã¯ããå¤ãåãå ¥ãããã¦ãã¾ãã Buildpacks ã使ããã¨ã§ãDockerfile ãæ¸ããªãã¦ã Docker ã¤ã¡ã¼ã¸ãä½æã§ãã¾ãã ã¾ããä½æãããã¤ã¡ã¼ã¸ã¯ããªã軽éã§ããã
試ãã¦ã¿ã
ä»åã¯ã以ä¸ã®ãªãã¸ããªã® Java ã¢ããªã±ã¼ã·ã§ã³ã® Docker ã¤ã¡ã¼ã¸ãä½æãã¾ãã github.com
ã¤ã³ã¹ãã¼ã«
# Mac $ brew install buildpacks/tap/pack # Linux $ wget https://github.com/buildpacks/pack/releases/download/v0.12.0/pack-v0.12.0-linux.tgz $ tar xvf pack-v0.12.0-linux.tgz $ rm pack-v0.12.0-linux.tgz $ ./pack --help # Windows $ choco install pack --version=0.12.0
ãã¥ã¼ããªã¢ã«ã«ããä¾ (301MB)
pack build <image-name>
ã§ã¤ã¡ã¼ã¸ãä½æãã¾ãã
301MB ã®ã¤ã¡ã¼ã¸ãä½æããã¾ããã
docker run
ã§ã¢ããªã±ã¼ã·ã§ã³ãèµ·åã§ãã¾ããã
$ pack build myapp --builder cnbs/sample-builder:bionic $ docker run -it -p 8080:8080 myapp:latest
ãã«ãã¼ã--builder
ã§æå®ãã¾ãã
ä»ã«ããã¾ãã¾ãªãã«ãã¼ãé¸æã§ãã¾ãã
Suggested builders: Google: gcr.io/buildpacks/builder:v1 Ubuntu 18 base image with buildpacks for .NET, Go, Java, Node.js, and Python Heroku: heroku/buildpacks:18 heroku-18 base image with buildpacks for Ruby, Java, Node.js, Python, Golang, & PHP Paketo Buildpacks: gcr.io/paketo-buildpacks/builder:base Ubuntu bionic base image with buildpacks for Java, NodeJS and Golang Paketo Buildpacks: gcr.io/paketo-buildpacks/builder:full-cf cflinuxfs3 base image with buildpacks for Java, .NET, NodeJS, Golang, PHP, HTTPD and NGINX Paketo Buildpacks: gcr.io/paketo-buildpacks/builder:tiny Tiny base image (bionic build image, distroless run image) with buildpacks for Golang
spring-boot(2.3.0) ããã®æ°æ©è½ (245MB)
Java éå®ã®è©±ã«ãªãã¾ãããSpring Boot 2.3.0 ãã Buildpacks ãå
é¨çã«ä½¿ã£ã¦ããæ©è½ãçµã¿è¾¼ã¾ãã¾ããã
mvn spring-boot:build-image
㧠Docker ã¤ã¡ã¼ã¸ãä½æã§ãã¾ãã
pom.xml
ãç·¨éãã¦ãã«ãããããã¸ã§ã¯ãã® Spring Bootã®ãã¼ã¸ã§ã³ã 2.3.0 以ä¸ã¸ã¢ãããã¼ããã¾ãã
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
mvn spring-boot:build-image
ã§ãã«ãã§ãã¾ãã
245MBã¨æã軽éãªã¤ã¡ã¼ã¸ãä½æããã¾ããã
docker run
ã§ã¢ããªã±ã¼ã·ã§ã³ãèµ·åã§ãã¾ããã
$ mvn spring-boot:build-image $ docker run -it -p 8080:8080 sample:0.0.1-SNAPSHOT
èªå㧠Doker Build (539MB)
èªå㧠Dockerfile ãæ¸ãæ¹æ³ã§ãã ãã«ãã¹ãã¼ã¸ãã«ããå©ç¨ãã¦ãã«ããã¦ããä»ããéã539MB ã§ããã
FROM maven:3.6.3-jdk-8 as builder WORKDIR /apps/java-maven COPY pom.xml pom.xml RUN mvn dependency:resolve COPY src src RUN mvn package -DskipTests=true FROM openjdk:8 WORKDIR /apps/java-maven COPY --from=builder /apps/java-maven/target/sample-0.0.1-SNAPSHOT.jar /apps/java-maven/target/sample-0.0.1-SNAPSHOT.jar ENTRYPOINT ["java", "-jar", "/apps/java-maven/target/sample-0.0.1-SNAPSHOT.jar"] EXPOSE 8080
é常éãã以ä¸ã®ã³ãã³ãã§ãã«ããã¦ã¢ããªã±ã¼ã·ã§ã³ãèµ·åãã¾ãã
$ docker build -t self-build . $ docker run -it -p 8080:8080 self-build:latest
ãããã«
æ¬è¨äºã§ã¯ Java ã§ã®ä¾ã§ããããGo,Node,Ruby,PHP,Python ãªã©ã§ããã°ãã«ãã§ãã¾ãã
pack build <image-name> --builder gcr.io/paketo-buildpacks/builder:tiny
ãªã©ã®ã³ãã³ãã§è¨èªã«ãã£ããã«ãã¼ãæå®ããã°ãã«ãã§ãã¾ãã
èªå㧠Docker ãã«ããããå ´åã¯ã¤ã¡ã¼ã¸ã¯ 539MB ã§ããããBuildpacks ãå©ç¨ããã° Dockerfile ãæ¸ããªãã¦ãããä¸ã«ã¤ã¡ã¼ã¸ã¯ 245MB ã«ãªãã¾ããã
ãªãã§å°ãããªããã¯diveã§æ·±æãããããããã¥ã¡ã³ãèªãã ããã¦ã¾ãè¨äºã«ãã¾ãã
— kotaroooo0 (@kotaroooo0) 2020å¹´8æ16æ¥