从之前的阶段复制基于Go镜像的Go软件包?

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

Copy go package from previous stage based go image?

问题

在基于图像的Python中,我想使用go包gnostic运行一个命令(*):

RUN gnostic --grpc-out=test test/openapi/loyalty-bff.yaml

我编写了以下Dockerfile:

FROM golang:1.17 AS golang
RUN go get -u github.com/google/gnostic@latest
RUN go get -u github.com/googleapis/gnostic-grpc@latest

FROM python:3.7.10
WORKDIR /app
ADD requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/
COPY --from=golang /go/bin/gnostic /go/bin/gnostic-grpc
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000"]

当我运行命令(*)时,出现错误:

Command 'gnostic --grpc-out=loyalty-bff-1634180849463365375 loyalty-bff-1634180849463365375/loyalty-bff.yaml' returned non-zero exit status 127.

另一方面,如果不使用多阶段构建,我可以运行。将Python安装在基于go的图像中,但构建时间非常长:

FROM golang:1.17
WORKDIR /app
RUN go get -u github.com/google/gnostic@latest
RUN go get -u github.com/googleapis/gnostic-grpc@latest
RUN apt-get update
RUN apt-get install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev wget libbz2-dev
RUN wget https://www.python.org/ftp/python/3.7.8/Python-3.7.8.tgz
RUN tar -xf Python-3.7.8.tgz
RUN cd Python-3.7.8 \
    && ./configure --enable-shared \
    && make && make install
RUN apt-get install python3-pip -y

ADD requirements.txt /app/
RUN pip3 install -r requirements.txt
ADD . /app/
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000"]
英文:

In image based Python, I want to run one command (*) using go package gnostic:

RUN gnostic --grpc-out=test test/openapi/loyalty-bff.yaml

I did wrote following dockerfile:

FROM golang:1.17 AS golang
RUN go get -u github.com/google/gnostic@latest
RUN go get -u github.com/googleapis/gnostic-grpc@latest

FROM python:3.7.10
WORKDIR /app
ADD requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/
COPY --from=golang /go/bin/gnostic /go/bin/gnostic-grpc
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000"]

I got error when run the command (*):

Command 'gnostic --grpc-out=loyalty-bff-1634180849463365375 loyalty-bff-1634180849463365375/loyalty-bff.yaml' returned non-zero exit status 127.

On the other hand, i can run when not use multi stages. Replace by install python in image based go, but building time is very long:

FROM golang:1.17
WORKDIR /app
RUN go get -u github.com/google/gnostic@latest
RUN go get -u github.com/googleapis/gnostic-grpc@latest
RUN apt-get update
RUN apt-get install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev wget libbz2-dev
RUN wget https://www.python.org/ftp/python/3.7.8/Python-3.7.8.tgz
RUN tar -xf Python-3.7.8.tgz
RUN cd Python-3.7.8 \
    && ./configure --enable-shared \
    && make && make install
RUN apt-get install python3-pip -y

ADD requirements.txt /app/
RUN pip3 install -r requirements.txt
ADD . /app/
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000"]

答案1

得分: 0

退出代码127通常表示找不到可执行文件。

如果你查看golang:1.17的环境变量,你会发现默认的PATH包含/go/bin

$ docker run --rm -it golang:1.17 env
PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=a9b7efb588ea
TERM=xterm
GOLANG_VERSION=1.17.2
GOPATH=/go
HOME=/root

这就是为什么你可以在基于golang的容器中找到gnostic的原因。

但是,在python:3.7.10中,情况是这样的:

$ docker run --rm -it python:3.7.10 env
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

因此,对于你的情况,将复制命令更改为下面的命令可以使其正常工作:

COPY --from=golang /go/bin/gnostic /go/bin/gnostic-grpc /usr/local/bin/
英文:

Exit code 127 usually means can't find the executable.

If you have a look for env of golang:1.17, you could see by default the PATH has /go/bin:

$ docker run --rm -it golang:1.17 env
PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=a9b7efb588ea
TERM=xterm
GOLANG_VERSION=1.17.2
GOPATH=/go
HOME=/root

This is the reason why you could find gnostic in golang based container.

But, in python:3.7.10, it's next:

$ docker run --rm -it python:3.7.10 env
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

So, for your scenario, change the copy to next could make it work:

COPY --from=golang /go/bin/gnostic /go/bin/gnostic-grpc /usr/local/bin/

huangapple
  • 本文由 发表于 2021年10月14日 11:35:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/69564785.html
匿名

发表评论

匿名网友

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

确定