goctl docker

概述

goctl docker 命令用于生成 Dockerfile 文件,用于构建 Docker 镜像。

goctl docker 指令

  1. $ goctl docker --help
  2. Generate Dockerfile
  3. Usage:
  4. goctl docker [flags]
  5. Flags:
  6. --base string The base image to build the docker image, default scratch (default "scratch")
  7. --branch string The branch of the remote repo, it does work with --remote
  8. --exe string The executable name in the built image
  9. --go string The file that contains main function
  10. -h, --help help for docker
  11. --home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  12. --port int The port to expose, default none
  13. --remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  14. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
  15. --tz string The timezone of the container (default "Asia/Shanghai")
  16. --version string The goctl builder golang image version
goctl docker - 图1 参数字段goctl docker - 图2 参数类型goctl docker - 图3 是否必填goctl docker - 图4 默认值goctl docker - 图5 参数说明
basestringNOscratch基础镜像
branchstringNO空字符串远程模板所在 git 分支名称,仅当 remote 有值时使用
exestringNO主函数文件名输出的可执行文件名称
gostringYES空字符串主函数文件名称
homestringNO${HOME}/.goctl本地模板文件目录
portintNO0需要暴露的端口号,如果没传或者传0,则不暴露端口号
remotestringNO空字符串远程模板所在 git 仓库地址,当此字段传值时,优先级高于 home 字段值
tzstringNOAsia/Shanghai时区设置
versionstringYES空字符串Golang 镜像版本号

使用示例

我们用 goctl api new hello 命令创建一个 Demo 项目,然后进入项目目录,执行 goctl docker 命令,生成 Dockerfile 文件。

以下通过终端指令演示如何从零开始创建一个 goctl 项目,然后生成 Dockerfile 文件,到最后构建 Docker 镜像,最后运行 Docker 容器的过程。

  1. # 进入到个人 home 目录
  2. $ cd ~
  3. # 创建一个 hello 项目
  4. $ goctl api new hello
  5. Done.
  6. # 进入到 hello 项目目录
  7. $ cd hello
  8. # 查看目录结构
  9. $ tree
  10. .
  11. ├── etc
  12. └── hello-api.yaml
  13. ├── go.mod
  14. ├── hello.api
  15. ├── hello.go
  16. └── internal
  17. ├── config
  18. └── config.go
  19. ├── handler
  20. ├── hellohandler.go
  21. └── routes.go
  22. ├── logic
  23. └── hellologic.go
  24. ├── svc
  25. └── servicecontext.go
  26. └── types
  27. └── types.go
  28. 7 directories, 10 files
  29. # 生成 Dockerfile 文件
  30. $ goctl docker --go hello.go --exe hello
  31. Hint: run "docker build ..." command in dir:
  32. /Users/keson/hello
  33. Done.
  34. # 查看 Dockerfile 文件
  35. $ cat Dockerfile
  36. FROM golang:alpine AS builder
  37. LABEL stage=gobuilder
  38. ENV CGO_ENABLED 0
  39. ENV GOPROXY https://goproxy.cn,direct
  40. RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
  41. RUN apk update --no-cache && apk add --no-cache tzdata
  42. WORKDIR /build
  43. ADD go.mod .
  44. ADD go.sum .
  45. RUN go mod download
  46. COPY . .
  47. COPY ./etc /app/etc
  48. RUN go build -ldflags="-s -w" -o /app/hello hello.go
  49. FROM scratch
  50. COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
  51. COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
  52. ENV TZ Asia/Shanghai
  53. WORKDIR /app
  54. COPY --from=builder /app/hello /app/hello
  55. COPY --from=builder /app/etc /app/etc
  56. CMD ["./hello", "-f", "etc/hello-api.yaml"]
  57. # 拉取 go 依赖
  58. $ go mod tidy
  59. go: finding module for package github.com/zeromicro/go-zero/core/conf
  60. go: finding module for package github.com/zeromicro/go-zero/core/logx
  61. go: finding module for package github.com/zeromicro/go-zero/rest
  62. go: finding module for package github.com/zeromicro/go-zero/rest/httpx
  63. go: found github.com/zeromicro/go-zero/core/conf in github.com/zeromicro/go-zero v1.4.3
  64. go: found github.com/zeromicro/go-zero/rest in github.com/zeromicro/go-zero v1.4.3
  65. go: found github.com/zeromicro/go-zero/rest/httpx in github.com/zeromicro/go-zero v1.4.3
  66. go: found github.com/zeromicro/go-zero/core/logx in github.com/zeromicro/go-zero v1.4.3
  67. # 在 hello 项目目录下,执行 docker build 命令,生成镜像
  68. $ docker build -t hello:v1 .
  69. [+] Building 72.5s (20/20) FINISHED
  70. => [internal] load build definition from Dockerfile 0.0s
  71. => => transferring dockerfile: 37B 0.0s
  72. => [internal] load .dockerignore 0.0s
  73. => => transferring context: 2B 0.0s
  74. => [internal] load metadata for docker.io/library/golang:alpine 1.0s
  75. => [internal] load build context 0.2s
  76. => => transferring context: 142.47kB 0.1s
  77. => [builder 1/10] FROM docker.io/library/golang:alpine@sha256:a9b24b67dc83b3383d22a14941c2b2b2ca6a10 25.5s
  78. => => resolve docker.io/library/golang:alpine@sha256:a9b24b67dc83b3383d22a14941c2b2b2ca6a103d805cac682 0.0s
  79. => => sha256:7f1d6579712341e8062db43195deb2d84f63b0f2d1ed7c3d2074891085ea1b56 116.88MB / 116.88MB 19.9s
  80. => => sha256:a9b24b67dc83b3383d22a14941c2b2b2ca6a103d805cac6820fd1355943beaf1 1.65kB / 1.65kB 0.0s
  81. => => sha256:d34d005738c897bad9671117acf4a27fe7d5ab80e129bf2aba2fa7c344c416e4 1.16kB / 1.16kB 0.0s
  82. => => sha256:3b877c93f9b7d6e7c07329c02d3a29d306b35bbe06d1c79d00d54d6ce2e5a360 5.13kB / 5.13kB 0.0s
  83. => => sha256:261da4162673b93e5c0e7700a3718d40bcc086dbf24b1ec9b54bca0b82300626 3.26MB / 3.26MB 2.7s
  84. => => sha256:bc729abf26b5aade3c4426d388b5ea6907fe357dec915ac323bb2fa592d6288f 286.22kB / 286.22kB 1.8s
  85. => => sha256:652874aefa1343799c619d092ab9280b25f96d97939d5d796437e7288f5599c9 156B / 156B 2.3s
  86. => => extracting sha256:261da4162673b93e5c0e7700a3718d40bcc086dbf24b1ec9b54bca0b82300626 0.2s
  87. => => extracting sha256:bc729abf26b5aade3c4426d388b5ea6907fe357dec915ac323bb2fa592d6288f 0.1s
  88. => => extracting sha256:7f1d6579712341e8062db43195deb2d84f63b0f2d1ed7c3d2074891085ea1b56 5.2s
  89. => => extracting sha256:652874aefa1343799c619d092ab9280b25f96d97939d5d796437e7288f5599c9 0.0s
  90. => [builder 2/10] RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories 0.6s
  91. => [builder 3/10] RUN apk update --no-cache && apk add --no-cache tzdata 24.6s
  92. => [builder 4/10] WORKDIR /build 0.0s
  93. => [builder 5/10] ADD go.mod . 0.0s
  94. => [builder 6/10] ADD go.sum . 0.0s
  95. => [builder 7/10] RUN go mod download 11.3s
  96. => [builder 8/10] COPY . . 0.0s
  97. => [builder 9/10] COPY ./etc /app/etc 0.0s
  98. => [builder 10/10] RUN go build -ldflags="-s -w" -o /app/hello hello.go 9.1s
  99. => [stage-1 1/5] COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates 0.0s
  100. => [stage-1 2/5] COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shangh 0.0s
  101. => [stage-1 3/5] WORKDIR /app 0.0s
  102. => [stage-1 4/5] COPY --from=builder /app/hello /app/hello 0.0s
  103. => [stage-1 5/5] COPY --from=builder /app/etc /app/etc 0.0s
  104. => exporting to image 0.1s
  105. => => exporting layers 0.1s
  106. => => writing image sha256:586fe3aab42d3d27ad73118334be072577801de18c22694b380161f00656dd7a 0.0s
  107. => => naming to docker.io/library/hello:v1 0.0s
  108. Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
  109. # 启动服务
  110. $ docker run --rm -it -p 8888:8888 hello:v1
  111. Starting server at 0.0.0.0:8888...
  112. # 单开一个终端 curl 测试
  113. $ curl -i http://localhost:8888/from/you
  114. curl -i http://localhost:8888/from/you
  115. HTTP/1.1 200 OK
  116. Content-Type: application/json; charset=utf-8
  117. Traceparent: 00-7950f2af01228e73c5adcf1670e309d2-2d8262ef5bd4f5a2-00
  118. Date: Fri, 06 Jan 2023 06:41:34 GMT
  119. Content-Length: 4
  120. null%