docker 多阶段构建
多階段構(gòu)建
之前的做法
在 Docker 17.05 版本之前,我們構(gòu)建 Docker 鏡像時(shí),通常會(huì)采用兩種方式:
全部放入一個(gè) Dockerfile
一種方式是將所有的構(gòu)建過(guò)程編包含在一個(gè) Dockerfile 中,包括項(xiàng)目及其依賴庫(kù)的編譯、測(cè)試、打包等流程,這里可能會(huì)帶來(lái)的一些問(wèn)題:
鏡像層次多,鏡像體積較大,部署時(shí)間變長(zhǎng)
源代碼存在泄露的風(fēng)險(xiǎn)
例如,編寫(xiě) app.go 文件,該程序輸出 Hello World!
package main import "fmt" func main(){fmt.Printf("Hello World!"); }編寫(xiě) Dockerfile.one 文件
FROM golang:1.9-alpineRUN apk --no-cache add git ca-certificatesWORKDIR /go/src/github.com/go/helloworld/COPY app.go .RUN go get -d -v github.com/go-sql-driver/mysql \&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . \&& cp /go/src/github.com/go/helloworld/app /rootWORKDIR /root/CMD ["./app"]構(gòu)建鏡像
$ docker build -t go/helloworld:1 -f Dockerfile.one .分散到多個(gè) Dockerfile
另一種方式,就是我們事先在一個(gè) Dockerfile 將項(xiàng)目及其依賴庫(kù)編譯測(cè)試打包好后,再將其拷貝到運(yùn)行環(huán)境中,這種方式需要我們編寫(xiě)兩個(gè) Dockerfile 和一些編譯腳本才能將其兩個(gè)階段自動(dòng)整合起來(lái),這種方式雖然可以很好地規(guī)避第一種方式存在的風(fēng)險(xiǎn),但明顯部署過(guò)程較復(fù)雜。
例如,編寫(xiě) Dockerfile.build 文件
FROM golang:1.9-alpineRUN apk --no-cache add gitWORKDIR /go/src/github.com/go/helloworldCOPY app.go .RUN go get -d -v github.com/go-sql-driver/mysql \&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .編寫(xiě) Dockerfile.copy 文件
FROM alpine:latestRUN apk --no-cache add ca-certificatesWORKDIR /root/COPY app .CMD ["./app"]新建 build.sh
#!/bin/sh echo Building go/helloworld:builddocker build -t go/helloworld:build . -f Dockerfile.builddocker create --name extract go/helloworld:build docker cp extract:/go/src/github.com/go/helloworld/app ./app docker rm -f extractecho Building go/helloworld:2docker build --no-cache -t go/helloworld:2 . -f Dockerfile.copy rm ./app現(xiàn)在運(yùn)行腳本即可構(gòu)建鏡像
$ chmod +x build.sh$ ./build.sh對(duì)比兩種方式生成的鏡像大小
$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZE go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB go/helloworld 1 f55d3e16affc 2 minutes ago 295MB使用多階段構(gòu)建
為解決以上問(wèn)題,Docker v17.05 開(kāi)始支持多階段構(gòu)建 (multistage builds)。使用多階段構(gòu)建我們就可以很容易解決前面提到的問(wèn)題,并且只需要編寫(xiě)一個(gè) Dockerfile:
例如,編寫(xiě) Dockerfile 文件
FROM golang:1.9-alpine as builderRUN apk --no-cache add gitWORKDIR /go/src/github.com/go/helloworld/RUN go get -d -v github.com/go-sql-driver/mysqlCOPY app.go .RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .FROM alpine:latest as prodRUN apk --no-cache add ca-certificatesWORKDIR /root/COPY --from=0 /go/src/github.com/go/helloworld/app .CMD ["./app"]構(gòu)建鏡像
$ docker build -t go/helloworld:3 .對(duì)比三個(gè)鏡像大小
$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZE go/helloworld 3 d6911ed9c846 7 seconds ago 6.47MB go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB go/helloworld 1 f55d3e16affc 2 minutes ago 295MB很明顯使用多階段構(gòu)建的鏡像體積小,同時(shí)也完美解決了上邊提到的問(wèn)題。
只構(gòu)建某一階段的鏡像
我們可以使用 as 來(lái)為某一階段命名,例如
FROM golang:1.9-alpine as builder例如當(dāng)我們只想構(gòu)建 builder 階段的鏡像時(shí),增加 --target=builder 參數(shù)即可
$ docker build --target builder -t username/imagename:tag .構(gòu)建時(shí)從其他鏡像復(fù)制文件
上面例子中我們使用 COPY --from=0 /go/src/github.com/go/helloworld/app . 從上一階段的鏡像中復(fù)制文件,我們也可以復(fù)制任意鏡像中的文件。
$ COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf實(shí)戰(zhàn)多階段構(gòu)建 Laravel 鏡像
本節(jié)適用于 PHP 開(kāi)發(fā)者閱讀。
準(zhǔn)備
新建一個(gè) Laravel 項(xiàng)目或在已有的 Laravel 項(xiàng)目根目錄下新建 Dockerfile .dockerignore laravel.conf 文件。
在 .dockerignore 文件中寫(xiě)入以下內(nèi)容。
.idea/ .git/ vendor/ node_modules/ public/js/ public/css/ yarn-error.logbootstrap/cache/* storage/# 自行添加其他需要排除的文件,例如 .env.* 文件在 laravel.conf 文件中寫(xiě)入 nginx 配置。
server {listen 80 default_server;root /app/laravel/public;index index.php index.html;location / {try_files $uri $uri/ /index.php?$query_string;}location ~ .*\.php(\/.*)*$ {fastcgi_pass laravel:9000;include fastcgi.conf;# fastcgi_connect_timeout 300;# fastcgi_send_timeout 300;# fastcgi_read_timeout 300;} }前端構(gòu)建
第一階段進(jìn)行前端構(gòu)建。
FROM node:alpine as frontendCOPY package.json /app/RUN cd /app \&& npm install --registry=https://registry.npm.taobao.orgCOPY webpack.mix.js /app/ COPY resources/assets/ /app/resources/assets/RUN cd /app \&& npm run production安裝 Composer 依賴
第二階段安裝 Composer 依賴。
FROM composer as composerCOPY database/ /app/database/ COPY composer.json composer.lock /app/RUN cd /app \&& composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ \&& composer install \--ignore-platform-reqs \--no-interaction \--no-plugins \--no-scripts \--prefer-dist整合以上階段所生成的文件
第三階段對(duì)以上階段生成的文件進(jìn)行整合。
FROM php:7.2-fpm-alpine as laravelARG LARAVEL_PATH=/app/laravelCOPY --from=composer /app/vendor/ ${LARAVEL_PATH}/vendor/ COPY . ${LARAVEL_PATH} COPY --from=frontend /app/public/js/ ${LARAVEL_PATH}/public/js/ COPY --from=frontend /app/public/css/ ${LARAVEL_PATH}/public/css/ COPY --from=frontend /app/mix-manifest.json ${LARAVEL_PATH}/mix-manifest.jsonRUN cd ${LARAVEL_PATH} \&& php artisan package:discover \&& mkdir -p storage \&& mkdir -p storage/framework/cache \&& mkdir -p storage/framework/sessions \&& mkdir -p storage/framework/testing \&& mkdir -p storage/framework/views \&& mkdir -p storage/logs \&& chmod -R 777 storage最后一個(gè)階段構(gòu)建 NGINX 鏡像
FROM nginx:alpine as nginxARG LARAVEL_PATH=/app/laravelCOPY laravel.conf /etc/nginx/conf.d/ COPY --from=laravel ${LARAVEL_PATH}/public ${LARAVEL_PATH}/public構(gòu)建 Laravel 及 Nginx 鏡像
使用 docker build 命令構(gòu)建鏡像。
$ docker build -t my/laravel --target=laravel .$ docker build -t my/nginx --target=nginx .啟動(dòng)容器并測(cè)試
新建 Docker 網(wǎng)絡(luò)
$ docker network create laravel啟動(dòng) laravel 容器, --name=laravel 參數(shù)設(shè)定的名字必須與 nginx 配置文件中的 fastcgi_pass laravel:9000; 一致
$ docker run -it --rm --name=laravel --network=laravel my/laravel啟動(dòng) nginx 容器
$ docker run -it --rm --network=laravel -p 8080:80 my/nginx瀏覽器訪問(wèn) 127.0.0.1:8080 可以看到 Laravel 項(xiàng)目首頁(yè)。
也許 Laravel 項(xiàng)目依賴其他外部服務(wù),例如 redis、MySQL,請(qǐng)自行啟動(dòng)這些服務(wù)之后再進(jìn)行測(cè)試,本小節(jié)不再贅述。
生產(chǎn)環(huán)境優(yōu)化
本小節(jié)內(nèi)容為了方便測(cè)試,將配置文件直接放到了鏡像中,實(shí)際在使用時(shí) 建議 將配置文件作為 config 或 secret 掛載到容器中,請(qǐng)讀者自行學(xué)習(xí) Swarm mode 或 Kubernetes 的相關(guān)內(nèi)容。
附錄
完整的 Dockerfile 文件如下。
FROM node:alpine as frontendCOPY package.json /app/RUN cd /app \&& npm install --registry=https://registry.npm.taobao.orgCOPY webpack.mix.js /app/ COPY resources/assets/ /app/resources/assets/RUN cd /app \&& npm run productionFROM composer as composerCOPY database/ /app/database/ COPY composer.json /app/RUN cd /app \&& composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ \&& composer install \--ignore-platform-reqs \--no-interaction \--no-plugins \--no-scripts \--prefer-distFROM php:7.2-fpm-alpine as laravelARG LARAVEL_PATH=/app/laravelCOPY --from=composer /app/vendor/ ${LARAVEL_PATH}/vendor/ COPY . ${LARAVEL_PATH} COPY --from=frontend /app/public/js/ ${LARAVEL_PATH}/public/js/ COPY --from=frontend /app/public/css/ ${LARAVEL_PATH}/public/css/ COPY --from=frontend /app/mix-manifest.json ${LARAVEL_PATH}/mix-manifest.jsonRUN cd ${LARAVEL_PATH} \&& php artisan package:discover \&& mkdir -p storage \&& mkdir -p storage/framework/cache \&& mkdir -p storage/framework/sessions \&& mkdir -p storage/framework/testing \&& mkdir -p storage/framework/views \&& mkdir -p storage/logs \&& chmod -R 777 storageFROM nginx:alpine as nginxARG LARAVEL_PATH=/app/laravelCOPY laravel.conf /etc/nginx/conf.d/ COPY --from=laravel ${LARAVEL_PATH}/public ${LARAVEL_PATH}/public總結(jié)
以上是生活随笔為你收集整理的docker 多阶段构建的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Dockerfile 指令详解2
- 下一篇: 03 | SRE切入点:选择SLI,设定