Golang Build在docker中找不到本地导入 [英] Golang Build in docker not finding local import
问题描述
我对GoLang很陌生。我有一个非常简单的dockerFile,它正在尝试构建goLang Web服务。
I am pretty new to GoLang. I have a pretty simple dockerFile that is trying to build a goLang webservice.
FROM golang:alpine
WORKDIR /app/webservice_refArch
ADD . /app/webservice_refArch
RUN apk add git
RUN apk upgrade
RUN cd /app/webservice_refArch/ && go get webservice_refArch/restapi
RUN cd /app/webservice_refArch/cmd/reference-w-s-server && go build -o ../../server
ENTRYPOINT ./goapp
go get webservice_refArch/restapi
我得到的错误是:
package webservice_refArch / restapi :无法识别的导入路径
webservice_refArch / restapi(导入路径不是以
主机名开头)
package webservice_refArch/restapi: unrecognized import path "webservice_refArch/restapi" (import path does not begin with hostname)
何时我在本地(在同一文件夹中)运行相同的命令,它运行正常。我确定我会丢失一些愚蠢的东西,但是对从docker运行时为什么它失败的任何想法都会受到赞赏。
When I run that same command on my local (in the same folder) it runs just fine. I am sure that I am missing something stupid but any thoughts on why it fails when running from docker would be appreciated.
推荐答案
缺少的是将您的项目放在 $ GOPATH
下,如果您有本地名称空间,它也应该在相同的名称空间 $ GOPATH / domain.com /下
。
What is missing is to put your project under $GOPATH
and if you have local namespace it should also be under the same namespace $GOPATH/domain.com/
.
一种合适的方法是进行多阶段构建。多阶段涉及的基本原理包括调用一个临时容器,该容器可以简化应用程序的构建,然后将已构建资产从该空间复制到容器映像中,该容器映像具有运行该应用程序所需的最少组件数量。
A proper way also to do that is to have a multi-stage builds. The basic principle involved with Multi-stage involves invoking a temporary container which can facilitate the application build, then copying the built assets out of that space into a container image that has the least amount of components required to run the app.
# builder
FROM golang:1-alpine AS builder
RUN apk add git ca-certificates --update
ENV SERVICE_NAME my_project
ENV NAMESPACE mydomain #if you don't use namespace space just ignore it and remove it from the following lines
ENV APP /src/${NAMESPACE}/${SERVICE_NAME}/
ENV WORKDIR ${GOPATH}${APP}
WORKDIR $WORKDIR
ADD . $WORKDIR
RUN go build
###############################################################
#image
FROM alpine
RUN apk add ca-certificates --update
ENV SERVICE_NAME webservice_refArch
ENV NAMESPACE my_domain #if you don't use namespace space just ignore it and remove it from the following lines
ENV APP /src/${NAMESPACE}/${SERVICE_NAME}/
ENV GOPATH /go
ENV WORKDIR ${GOPATH}${APP}
COPY --from=builder ${WORKDIR}${SERVICE_NAME} $WORKDIR
CMD ${WORKDIR}${SERVICE_NAME}
这篇关于Golang Build在docker中找不到本地导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!