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

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

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

问题

我有这个简单的Dockerfile:

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

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

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

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

如果我尝试:

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

RUN bin/golangci-lint version

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

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

尝试:

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

RUN /go/bin/golangci-lint version

输出:

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

I have this simple Dockerfile:

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

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

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

What exactly I am doing wrong here?

If I try:

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

RUN bin/golangci-lint version

I get this detailed output:

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

Trying:

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

RUN /go/bin/golangci-lint version

Output:

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

答案1

得分: 1

golangci-lint位于bin目录中:

FROM golang:1.16.6-buster

RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh\
    | sh -s v1.45.2
RUN pwd
#RUN /go/bin/golangci-lint --version

构建输出:

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

golangci-lint is in bin directory :

FROM golang:1.16.6-buster

RUN curl -s https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh\
    | sh -s v1.45.2
RUN pwd
#RUN /go/bin/golangci-lint --version

build output :

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

答案2

得分: 0

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

FROM golang:1.16.0-buster
RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.2
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.

FROM golang:1.16.0-buster
RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.2
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:

确定