<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] 參考:[https://www.bookstack.cn/read/docker\_practice-v1.1.0/image-multistage-builds-README.md](https://www.bookstack.cn/read/docker_practice-v1.1.0/image-multistage-builds-README.md) ## 概述 盡管Docker很強大,但使用過程當中也遇到了一些問題。比如說我想要構建一個編譯型語言鏡像,需要先在一個Dockerfile中編譯,然后再使用另外一個Dockerfile把編譯好的文件放到鏡像中。這樣無形當中就增大了CI/CD的復雜度。 > Docker多階段構建是17.05以后引入的新特性,旨在解決編譯和構建復雜的問題。減小鏡像大小。因此要使用多階段構建特性必須使用高于或等于17.05的Docker。 ## 在多階段構建之前 ### 全部放入一個 Dockerfile 一種方式是將所有的構建過程編包含在一個`Dockerfile`中,包括項目及其依賴庫的編譯、測試、打包等流程,這里可能會帶來的一些問題: * 鏡像層次多,鏡像體積較大,部署時間變長 * 源代碼存在泄露的風險 ### 分散到多個 Dockerfile 另一種方式,就是我們事先在一個`Dockerfile`將項目及其依賴庫編譯測試打包好后,再將其拷貝到運行環境中,這種方式需要我們編寫兩個`Dockerfile`和一些編譯腳本才能將其兩個階段自動整合起來,這種方式雖然可以很好地規避第一種方式存在的風險,但明顯部署過程較復雜。 例如,編寫`Dockerfile.build`文件 ``` FROM golang:1.9-alpine RUN apk --no-cache add git WORKDIR /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 . ``` 編寫`Dockerfile.copy`文件 ``` FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY app . CMD ["./app"] ``` 新建`build.sh` ``` #!/bin/sh echo Building go/helloworld:build docker build -t go/helloworld:build . -f Dockerfile.build docker create --name extract go/helloworld:build docker cp extract:/go/src/github.com/go/helloworld/app ./app docker rm -f extract echo Building go/helloworld:2 docker build --no-cache -t go/helloworld:2 . -f Dockerfile.copy rm ./app ``` 現在運行腳本即可構建鏡像 ``` $ chmod +x build.sh $ ./build.sh ``` 對比兩種方式生成的鏡像大小 ``` $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB go/helloworld 1 f55d3e16affc 2 minutes ago 295MB ``` ## 使用多階段構建 為解決以上問題,Docker v17.05 開始支持多階段構建 (`multistage builds`)。使用多階段構建我們就可以很容易解決前面提到的問題,并且只需要編寫一個`Dockerfile`: 例如,編寫`Dockerfile`文件 ``` FROM golang:1.9-alpine as builder RUN apk --no-cache add git WORKDIR /go/src/github.com/go/helloworld/ RUN go get -d -v github.com/go-sql-driver/mysql COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . FROM alpine:latest as prod RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=0 /go/src/github.com/go/helloworld/app . CMD ["./app"] ``` 構建鏡像 ``` $ docker build -t go/helloworld:3 . ``` 對比三個鏡像大小 ``` $ docker image ls REPOSITORY 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 ``` 很明顯使用多階段構建的鏡像體積小,同時也完美解決了上邊提到的問題。 ### 只構建某一階段的鏡像 我們可以使用`as`來為某一階段命名,例如 ``` FROM golang:1.9-alpine as builder ``` 例如當我們只想構建`builder`階段的鏡像時,增加`--target=builder`參數即可 ``` $ docker build --target builder -t username/imagename:tag . ``` ### 構建時從其他鏡像復制文件 上面例子中我們使用`COPY --from=0 /go/src/github.com/go/helloworld/app .`從上一階段的鏡像中復制文件,我們也可以復制任意鏡像中的文件。 ``` $ COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看