如何获取一个Go库并在Docker中运行它?

huangapple go评论110阅读模式
英文:

How to get a Go library and run it in Docker?

问题

我有这个简单的Dockerfile:

  1. FROM golang:1.16.6-buster
  2. RUN wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2
  3. RUN golangci-lint --version

当我构建它时,无论我如何更改上面的第二行,我总是得到:

  1. > [3/3] RUN golangci-lint --version:
  2. #6 0.331 /bin/sh: 1: golangci-lint: not found

我在这里到底做错了什么?

如果我尝试:

  1. FROM golang:1.16.6-buster
  2. RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh\
  3. | sh -s v1.45.2
  4. RUN bin/golangci-lint version

我得到了这个详细的输出:

  1. docker build -t gotest .
  2. [+] Building 0.9s (6/6) FINISHED
  3. => [internal] load build definition from Dockerfile 0.0s
  4. => => transferring dockerfile: 37B 0.0s
  5. => [internal] load .dockerignore 0.0s
  6. => => transferring context: 2B 0.0s
  7. => [internal] load metadata for docker.io/library/golang:1.16.6-buster 0.5s
  8. => [1/3] FROM docker.io/library/golang:1.16.6-buster@sha256:f3923dc5a92a237db0f07a924a238a8a4a711e3b77a7b5bdb1b526e107dcb9d4 0.0s
  9. => CACHED [2/3] RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2 0.0s
  10. => ERROR [3/3] RUN bin/golangci-lint version 0.2s
  11. ------
  12. > [3/3] RUN bin/golangci-lint version:
  13. #6 0.214 /bin/sh: 1: bin/golangci-lint: not found
  14. ------
  15. executor failed running [/bin/sh -c bin/golangci-lint version]: exit code: 127

尝试:

  1. FROM golang:1.16.6-buster
  2. RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh\
  3. | sh -s v1.45.2
  4. RUN /go/bin/golangci-lint version

输出:

  1. docker build .
  2. [+] Building 1.7s (7/7) FINISHED
  3. => [internal] load build definition from Dockerfile 0.0s
  4. => => transferring dockerfile: 212B 0.0s
  5. => [internal] load .dockerignore 0.0s
  6. => => transferring context: 32B 0.0s
  7. => [internal] load metadata for docker.io/library/golang:1.16.6-buster 1.3s
  8. => [auth] library/golang:pull token for registry-1.docker.io 0.0s
  9. => [1/3] FROM docker.io/library/golang:1.16.6-buster@sha256:f3923dc5a92a237db0f07a924a238a8a4a711e3b77a7b5bdb1b526e107dcb9d4 0.0s
  10. => CACHED [2/3] RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2 0.0s
  11. => ERROR [3/3] RUN /go/bin/golangci-lint version 0.3s
  12. ------
  13. > [3/3] RUN /go/bin/golangci-lint version:
  14. #7 0.276 /bin/sh: 1: /go/bin/golangci-lint: not found
  15. ------
  16. executor failed running [/bin/sh -c /go/bin/golangci-lint version]: exit code: 127
英文:

I have this simple Dockerfile:

  1. FROM golang:1.16.6-buster
  2. RUN wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2
  3. RUN golangci-lint --version

When I build it, no matter how I change the second line above, I am always getting:

  1. > [3/3] RUN golangci-lint --version:
  2. #6 0.331 /bin/sh: 1: golangci-lint: not found

What exactly I am doing wrong here?

If I try:

  1. FROM golang:1.16.6-buster
  2. RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh\
  3. | sh -s v1.45.2
  4. RUN bin/golangci-lint version

I get this detailed output:

  1. docker build -t gotest .
  2. [+] Building 0.9s (6/6) FINISHED
  3. => [internal] load build definition from Dockerfile 0.0s
  4. => => transferring dockerfile: 37B 0.0s
  5. => [internal] load .dockerignore 0.0s
  6. => => transferring context: 2B 0.0s
  7. => [internal] load metadata for docker.io/library/golang:1.16.6-buster 0.5s
  8. => [1/3] FROM docker.io/library/golang:1.16.6-buster@sha256:f3923dc5a92a237db0f07a924a238a8a4a711e3b77a7b5bdb1b526e107dcb9d4 0.0s
  9. => CACHED [2/3] RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2 0.0s
  10. => ERROR [3/3] RUN bin/golangci-lint version 0.2s
  11. ------
  12. > [3/3] RUN bin/golangci-lint version:
  13. #6 0.214 /bin/sh: 1: bin/golangci-lint: not found
  14. ------
  15. executor failed running [/bin/sh -c bin/golangci-lint version]: exit code: 127

Trying:

  1. FROM golang:1.16.6-buster
  2. RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh\
  3. | sh -s v1.45.2
  4. RUN /go/bin/golangci-lint version

Output:

  1. docker build .
  2. [+] Building 1.7s (7/7) FINISHED
  3. => [internal] load build definition from Dockerfile 0.0s
  4. => => transferring dockerfile: 212B 0.0s
  5. => [internal] load .dockerignore 0.0s
  6. => => transferring context: 32B 0.0s
  7. => [internal] load metadata for docker.io/library/golang:1.16.6-buster 1.3s
  8. => [auth] library/golang:pull token for registry-1.docker.io 0.0s
  9. => [1/3] FROM docker.io/library/golang:1.16.6-buster@sha256:f3923dc5a92a237db0f07a924a238a8a4a711e3b77a7b5bdb1b526e107dcb9d4 0.0s
  10. => CACHED [2/3] RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2 0.0s
  11. => ERROR [3/3] RUN /go/bin/golangci-lint version 0.3s
  12. ------
  13. > [3/3] RUN /go/bin/golangci-lint version:
  14. #7 0.276 /bin/sh: 1: /go/bin/golangci-lint: not found
  15. ------
  16. executor failed running [/bin/sh -c /go/bin/golangci-lint version]: exit code: 127

答案1

得分: 1

golangci-lint位于bin目录中:

  1. FROM golang:1.16.6-buster
  2. RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh\
  3. | sh -s v1.45.2
  4. RUN pwd
  5. #RUN /go/bin/golangci-lint --version

构建输出:

  1. $ docker build --pull --no-cache --tag golang .
  2. Sending build context to Docker daemon 15.36kB
  3. Step 1/3 : FROM golang:1.16.6-buster
  4. 1.16.6-buster: library/golang 拉取
  5. Digest: sha256:f3923dc5a92a237db0f07a924a238a8a4a711e3b77a7b5bdb1b526e107dcb9d4
  6. Status: Image is up to date for golang:1.16.6-buster
  7. ---> 028d102f774a
  8. Step 2/3 : RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2
  9. ---> 正在运行 4700e3995fcc
  10. golangci/golangci-lint info checking GitHub for tag 'v1.45.2'
  11. golangci/golangci-lint info found version: 1.45.2 for v1.45.2/linux/amd64
  12. golangci/golangci-lint info installed ./bin/golangci-lint
  13. Removing intermediate container 4700e3995fcc
  14. ---> 03634ff2897c
  15. Step 3/3 : RUN bin/golangci-lint --version
  16. ---> 正在运行 f1348b5de5e2
  17. golangci-lint has version 1.45.2 built from 8bdc4d3f on 2022-03-24T11:51:26Z
  18. Removing intermediate container f1348b5de5e2
  19. ---> dc810407dc0d
  20. Successfully built dc810407dc0d
  21. Successfully tagged golang:latest
英文:

golangci-lint is in bin directory :

  1. FROM golang:1.16.6-buster
  2. RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh\
  3. | sh -s v1.45.2
  4. RUN pwd
  5. #RUN /go/bin/golangci-lint --version

build output :

  1. $ docker build --pull --no-cache --tag golang .
  2. Sending build context to Docker daemon 15.36kB
  3. Step 1/3 : FROM golang:1.16.6-buster
  4. 1.16.6-buster: Pulling from library/golang
  5. Digest: sha256:f3923dc5a92a237db0f07a924a238a8a4a711e3b77a7b5bdb1b526e107dcb9d4
  6. Status: Image is up to date for golang:1.16.6-buster
  7. ---> 028d102f774a
  8. Step 2/3 : RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2
  9. ---> Running in 4700e3995fcc
  10. golangci/golangci-lint info checking GitHub for tag 'v1.45.2'
  11. golangci/golangci-lint info found version: 1.45.2 for v1.45.2/linux/amd64
  12. golangci/golangci-lint info installed ./bin/golangci-lint
  13. Removing intermediate container 4700e3995fcc
  14. ---> 03634ff2897c
  15. Step 3/3 : RUN bin/golangci-lint --version
  16. ---> Running in f1348b5de5e2
  17. golangci-lint has version 1.45.2 built from 8bdc4d3f on 2022-03-24T11:51:26Z
  18. Removing intermediate container f1348b5de5e2
  19. ---> dc810407dc0d
  20. Successfully built dc810407dc0d
  21. Successfully tagged golang:latest

答案2

得分: 0

这是最终的工作方式。由于版本不匹配,没有下载任何二进制文件。我将Go升级到1.16.0,将linter升级到1.45.2。此外,我使用go install代替了wget,这适用于这个Go版本。这样就可以在默认文件夹/bin中下载二进制文件,并修复了CI作业。

  1. FROM golang:1.16.0-buster
  2. RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.2
  3. RUN ./bin/golangci-lint --version
英文:

This is how it worked finally. No binary was getting downloaded due to versions mismatch. I upgraded Go to 1.16
0
and the linter to 1.45.2. Also instead of wget I went for go install which works with this Go version. This got a binary downloaded in the default folder /bin and the CI job was fixed.

  1. FROM golang:1.16.0-buster
  2. RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.2
  3. RUN ./bin/golangci-lint --version

huangapple
  • 本文由 发表于 2022年4月14日 22:32:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/71873309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定