<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          最簡(jiǎn)單的 Go Dockerfile 編寫姿勢(shì),沒有之一!

          共 1979字,需瀏覽 4分鐘

           ·

          2020-12-17 01:33

          1. Dockerfile 一些額外注意點(diǎn)

          • 選擇最簡(jiǎn)單的鏡像

          比如 alpine,整個(gè)鏡像 5M 左右

          • 設(shè)置鏡像時(shí)區(qū)

          RUN apk add --no-cache tzdata
          ENV TZ Asia/Shanghai

          2. 多階段構(gòu)建

          • 第一階段構(gòu)建出可執(zhí)行文件,確保構(gòu)建過程獨(dú)立于宿主機(jī)

          • 第二階段將第一階段的輸出作為輸入,構(gòu)建出最終的極簡(jiǎn)鏡像

          3. 完整 Dockerfile 編寫過程

          • 首先安裝?goctl?工具

          GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl

          • 在?greet?項(xiàng)目下創(chuàng)建一個(gè)?hello?服務(wù)

          goctl api new hello

          文件結(jié)構(gòu)如下:

          greet
          ├── go.mod
          ├── go.sum
          └── service
          └── hello
          ├── etc
          │?? └── hello-api.yaml
          ├── hello.api
          ├── hello.go
          └── internal
          ├── config
          │?? └── config.go
          ├── handler
          │?? ├── hellohandler.go
          │?? └── routes.go
          ├── logic
          │?? └── hellologic.go
          ├── svc
          │?? └── servicecontext.go
          └── types
          └── types.go
          • 在?hello?目錄下一鍵生成?Dockerfile,命令如下

          goctl docker -go greet.go

          Dockerfile?內(nèi)容如下:

          FROM golang:alpine AS builder

          LABEL stage=gobuilder

          ENV CGO_ENABLED 0
          ENV GOOS linux
          ENV GOPROXY https://goproxy.cn,direct

          WORKDIR /build/zero

          ADD go.mod .
          ADD go.sum .
          RUN go mod download
          COPY . .
          COPY service/hello/etc /app/etc
          RUN go build -ldflags="-s -w" -o /app/hello service/hello/hello.go


          FROM alpine

          RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
          ENV TZ Asia/Shanghai

          WORKDIR /app
          COPY --from=builder /app/hello /app/hello
          COPY --from=builder /app/etc /app/etc

          CMD ["./hello", "-f", "etc/hello-api.yaml"]
          • 在?greet?目錄下?build?鏡像

          docker build -t hello:v1 -f service/hello/Dockerfile .

          • 查看鏡像

          hello v1 5455f2eaea6b 7 minutes ago 18.1MB

          可以看出鏡像大小約為 18M。

          • 啟動(dòng)服務(wù)

          docker run --rm -it -p 8888:8888 hello:v1

          • 測(cè)試服務(wù)

          $ curl -i http://localhost:8888/from/you
          HTTP/1.1 200 OK
          Content-Type: application/json
          Date: Thu, 10 Dec 2020 06:03:02 GMT
          Content-Length: 14

          {"message":""}

          4. 總結(jié)

          goctl?工具極大簡(jiǎn)化了?Dockerfile?文件的編寫,提供了開箱即用的最佳實(shí)踐,并且支持了模板自定義。

          如果覺得工具有幫助,歡迎?star??

          5. 項(xiàng)目地址

          https://github.com/tal-tech/go-zero

          6. 微信交流群

          瀏覽 30
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  爱情岛黄色 | 在线观看色情视频 | 性爱视频免费看 | 欧美一級黃色A片免費看野花 | 国产性在线电影 |