goctl docker

Overview

The goctl docker command is used to generate a Dockerfile file file and build a Docker mirror.

goctl docker directive

  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 Parameter fieldgoctl docker - 图2 Parameter Typegoctl docker - 图3 Required?goctl docker - 图4 Default valuegoctl docker - 图5 Parameter Description
basestringNOscratchBase Image
branchstringNOEmpty stringRemote template name is used only if remote has value
exestringNOPrimary function filenameOutput executable name
gostringYESEmpty stringPrimary function filename
homestringNO${HOME}/.goctlLocal Template File Directory
portintNO0Port number to be exposed, if not transmitted or passed 0
remotestringNOEmpty stringRemote template is a git repository address. Priority is higher than home field value when this field is passed
tzstringNOAsia/ShanghaiSet Time Zone
versionstringYESEmpty stringGolang Mirror Version Number

Examples

We create a Demo project with goctl api new hello command, then enter the project directory, execute goctl docker command, generate a Dockerfile file.

The following end instructions demonstrate how to create a goctl project from scratch, then generate a Dockerfile file file to finally build a Docker mirror, and finally run a Docker container.

  1. # go to your personal home directory
  2. $ cd ~
  3. # create a hello project
  4. $ goctl api new hello
  5. Done.
  6. # enter the hello project directory
  7. $ cd hello
  8. # view directory structure
  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. # generate Dockerfile
  30. $ goctl docker --go hello.go --exe hello
  31. Hint: run "docker build ..." command in dir:
  32. /Users/keson/hello
  33. Done.
  34. # view 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 mod tidy
  58. go: finding module for package github.com/zeromicro/go-zero/core/conf
  59. go: finding module for package github.com/zeromicro/go-zero/core/logx
  60. go: finding module for package github.com/zeromicro/go-zero/rest
  61. go: finding module for package github.com/zeromicro/go-zero/rest/httpx
  62. go: found github.com/zeromicro/go-zero/core/conf in github.com/zeromicro/go-zero v1.4.3
  63. go: found github.com/zeromicro/go-zero/rest in github.com/zeromicro/go-zero v1.4.3
  64. go: found github.com/zeromicro/go-zero/rest/httpx in github.com/zeromicro/go-zero v1.4.3
  65. go: found github.com/zeromicro/go-zero/core/logx in github.com/zeromicro/go-zero v1.4.3
  66. # execute command `docker build` to build image in hello directory
  67. $ docker build -t hello:v1 .
  68. [+] Building 72.5s (20/20) FINISHED
  69. => [internal] load build definition from Dockerfile 0.0s
  70. => => transferring dockerfile: 37B 0.0s
  71. => [internal] load .dockerignore 0.0s
  72. => => transferring context: 2B 0.0s
  73. => [internal] load metadata for docker.io/library/golang:alpine 1.0s
  74. => [internal] load build context 0.2s
  75. => => transferring context: 142.47kB 0.1s
  76. => [builder 1/10] FROM docker.io/library/golang:alpine@sha256:a9b24b67dc83b3383d22a14941c2b2b2ca6a10 25.5s
  77. => => resolve docker.io/library/golang:alpine@sha256:a9b24b67dc83b3383d22a14941c2b2b2ca6a103d805cac682 0.0s
  78. => => sha256:7f1d6579712341e8062db43195deb2d84f63b0f2d1ed7c3d2074891085ea1b56 116.88MB / 116.88MB 19.9s
  79. => => sha256:a9b24b67dc83b3383d22a14941c2b2b2ca6a103d805cac6820fd1355943beaf1 1.65kB / 1.65kB 0.0s
  80. => => sha256:d34d005738c897bad9671117acf4a27fe7d5ab80e129bf2aba2fa7c344c416e4 1.16kB / 1.16kB 0.0s
  81. => => sha256:3b877c93f9b7d6e7c07329c02d3a29d306b35bbe06d1c79d00d54d6ce2e5a360 5.13kB / 5.13kB 0.0s
  82. => => sha256:261da4162673b93e5c0e7700a3718d40bcc086dbf24b1ec9b54bca0b82300626 3.26MB / 3.26MB 2.7s
  83. => => sha256:bc729abf26b5aade3c4426d388b5ea6907fe357dec915ac323bb2fa592d6288f 286.22kB / 286.22kB 1.8s
  84. => => sha256:652874aefa1343799c619d092ab9280b25f96d97939d5d796437e7288f5599c9 156B / 156B 2.3s
  85. => => extracting sha256:261da4162673b93e5c0e7700a3718d40bcc086dbf24b1ec9b54bca0b82300626 0.2s
  86. => => extracting sha256:bc729abf26b5aade3c4426d388b5ea6907fe357dec915ac323bb2fa592d6288f 0.1s
  87. => => extracting sha256:7f1d6579712341e8062db43195deb2d84f63b0f2d1ed7c3d2074891085ea1b56 5.2s
  88. => => extracting sha256:652874aefa1343799c619d092ab9280b25f96d97939d5d796437e7288f5599c9 0.0s
  89. => [builder 2/10] RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories 0.6s
  90. => [builder 3/10] RUN apk update --no-cache && apk add --no-cache tzdata 24.6s
  91. => [builder 4/10] WORKDIR /build 0.0s
  92. => [builder 5/10] ADD go.mod . 0.0s
  93. => [builder 6/10] ADD go.sum . 0.0s
  94. => [builder 7/10] RUN go mod download 11.3s
  95. => [builder 8/10] COPY . . 0.0s
  96. => [builder 9/10] COPY ./etc /app/etc 0.0s
  97. => [builder 10/10] RUN go build -ldflags="-s -w" -o /app/hello hello.go 9.1s
  98. => [stage-1 1/5] COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates 0.0s
  99. => [stage-1 2/5] COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shangh 0.0s
  100. => [stage-1 3/5] WORKDIR /app 0.0s
  101. => [stage-1 4/5] COPY --from=builder /app/hello /app/hello 0.0s
  102. => [stage-1 5/5] COPY --from=builder /app/etc /app/etc 0.0s
  103. => exporting to image 0.1s
  104. => => exporting layers 0.1s
  105. => => writing image sha256:586fe3aab42d3d27ad73118334be072577801de18c22694b380161f00656dd7a 0.0s
  106. => => naming to docker.io/library/hello:v1 0.0s
  107. Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
  108. # Run service
  109. $ docker run --rm -it -p 8888:8888 hello:v1
  110. Starting server at 0.0.0.0:8888...
  111. # Open a new terminal and execute curl for testing
  112. $ curl -i http://localhost:8888/from/you
  113. curl -i http://localhost:8888/from/you
  114. HTTP/1.1 200 OK
  115. Content-Type: application/json; charset=utf-8
  116. Traceparent: 00-7950f2af01228e73c5adcf1670e309d2-2d8262ef5bd4f5a2-00
  117. Date: Fri, 06 Jan 2023 06:41:34 GMT
  118. Content-Length: 4
  119. null%