[英]How to use go mod with local package and docker?
I have two go modules github.com/myuser/mymainrepo
and github.com/myuser/commonrepo
我有两个 go 模块github.com/myuser/mymainrepo
和github.com/myuser/commonrepo
Here is how i have the files in my local computer这是我在本地计算机中保存文件的方式
- allmyrepos
- mymainrepo
- Dockerfile
- go.mod
- commonrepo
- go.mod
...
require (
github.com/myuser/commonrepo
)
replace (
github.com/myuser/commonrepo => ../commonrepo
)
It works well i can do local development with it.它运作良好,我可以用它进行本地开发。 Problem happens when i'm building docker image of mymainrepo当我构建 mymainrepo 的 docker 图像时,会出现问题
...
WORKDIR /go/src/mymainrepo
COPY go.mod go.sum ./
RUN go mod download
COPY ./ ./
RUN go build -o appbinary
...
Here replace
replaces github.com/myuser/commonrepo
with ../commonrepo
but in Docker /go/src/commonrepo
does not exists.这里用github.com/myuser/commonrepo
replace
../commonrepo
但在 Docker /go/src/commonrepo
中不存在。
I'm building the Docker image on CI/CD which needs to fetch directly from remote github url but i also need to do local development on commonrepo
.我正在 CI/CD 上构建 Docker 图像,需要直接从远程 github url 获取,但我还需要在commonrepo
上进行本地开发。 How can i do both?我怎么能两者都做?
I tried to put all my files in GOPATH
so it's ~/go/src/github.com/myuser/commonrepo
and go/src/github.com/myuser/mymainrepo
.我试图将我所有的文件放在GOPATH
中,所以它是~/go/src/github.com/myuser/commonrepo
和go/src/github.com/myuser/mymainrepo
。 And i removed the replace
directive.我删除了replace
指令。 But it looks for commonrepo
inside ~/go/pkg/mod/...
that's downloaded from github.但它会在~/go/pkg/mod/...
中查找 commonrepo,它是从commonrepo
下载的。
Create two go.mod
files: one for local development, and one for your build.创建两个go.mod
文件:一个用于本地开发,一个用于您的构建。 You can name it go.build.mod
for example.例如,您可以将其命名为go.build.mod
。
Keep the replace
directive in your go.mod
file but remove it from go.build.mod
.将replace
指令保留在go.mod
文件中,但将其从go.build.mod
中删除。
Finally, in your Dockerfile
:最后,在您的Dockerfile
中:
COPY go.build.mod ./go.mod
COPY go.sum ./
I still can't find other better solution even the voted answer doesn't work for me.即使投票的答案对我不起作用,我仍然找不到其他更好的解决方案。 Here a trick I've done that workaround for me.这是我为我完成的解决方法的一个技巧。 This is an example structure for doing this:这是执行此操作的示例结构:
|---sample
| |---...
| |---go.mod
| |---Dockerfile
|---core
| |---...
| |---go.mod
# Use the offical golang image to create a binary.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.16.3-buster AS builder
# Copy core library
RUN mkdir /core
COPY core/ /core
# Create and change to the app directory.
WORKDIR /app
# Retrieve application dependencies.
# This allows the container build to reuse cached dependencies.
# Expecting to copy go.mod and if present go.sum.
COPY go.* ./
RUN go mod download
# Copy local code to the container image.
COPY . ./
# Build the binary
RUN go build -o /app/sample cmd/main.go
...
...
Ok, our working dir is /app
and our core lib placed next to it /core
.好的,我们的工作目录是/app
,我们的核心库放在它旁边/core
。
cp -R ../core . && docker build --tag sample-service . && rm -R core/
Update A way better, create a Makefile
next to Dockerfile
, with content below:更新一个更好的方法,在Makefile
旁边创建一个Dockerfile
,内容如下:
build:
cp -R ../core .
docker build -t sample-service .
rm -R core/
Then command, make build
in the sample
directory.然后命令,在sample
目录中make build
。
You can create make submit
or make deploy
commands as you like to.您可以根据需要创建make submit
或make deploy
命令。
=> Production ready! => 生产就绪!
Be aware that if there's an error occurs during docker build process, it won't delete back the core
folder we have copied to sample
.请注意,如果在 docker 构建过程中发生错误,它不会删除我们复制到sample
的core
文件夹。
Pls let me know if you find any better solution.如果您找到更好的解决方案,请告诉我。 ;) ;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.