使用Go应用构建Docker:找不到软件包 [英] Build Docker with Go app: cannot find package
问题描述
我的Dockerfile位于 src / myapp
文件夹的根目录中, myapp
包含 myapp.go
与主软件包。
I have my Dockerfile in the root of directory with src/myapp
folder, myapp
contains myapp.go
with main package.
Dockerfile
如下所示:
FROM golang:1.9.2
ADD . /
RUN go build myapp;
ENTRYPOINT ["/go/bin/myapp"]
我明白了出现以下错误:
I get following error:
can't load package: package myapp: cannot find package "myapp" in any of:
/usr/local/go/src/myapp (from $GOROOT)
/go/src/myapp (from $GOPATH)
我做错了什么? docker完成 ADD
之后,能否登录 ls
命令吗?
What am I doing wrong? Can I log ls
command after docker has done ADD
?
推荐答案
您正在将所有文件复制到Image根目录,未安装任何依赖项,试图进行构建,然后从 / go / bin运行二进制文件/ app
。二进制文件在该目录中不存在,并且会产生错误。
You are copying all the files to Image root directory, Didn't installed any dependencies, Trying to Build it and then run the binary from /go/bin/app
. The binary doesn't exists in that directory and it's generating errors.
我建议使用这样的Dockerfile,
I would recommend using a Dockerfile like this,
FROM golang:1.9.2
ADD . /go/src/myapp
WORKDIR /go/src/myapp
RUN go get myapp
RUN go install
ENTRYPOINT ["/go/bin/myapp"]
这将执行以下操作。
- 将项目文件复制到
/ go / src / myapp
。 - 将工作目录设置为
/ go / src / myapp
。 - 安装依赖项,我用go get但用您正在使用的依赖管理工具替换它。
- 安装/构建二进制文件。
- 设置入口点。
- Copy project files to
/go/src/myapp
. - Set Working directory to
/go/src/myapp
. - Install dependencies, I used go get but replace it with which ever dependency management tool you are using.
- Install/build the binary.
- Set entry point.
您可以使用 ls
或任何其他命令> docker exec 。
You can run ls
or any other command using docker exec
.
示例:
docker exec <image name/hash> ls
您也可以在生成的图像中输入外壳,以使用
You can also enter the shell in the generated image to understand it well using
docker run --rm -it <image hash/name> /bin/sh
这篇关于使用Go应用构建Docker:找不到软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!