登录
首页 >  Golang >  Go问答

如何在 Docker 中同时运行多个本地 Go 模块,而无需依赖 Github 拉取?

来源:stackoverflow

时间:2024-02-28 12:21:24 420浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《如何在 Docker 中同时运行多个本地 Go 模块,而无需依赖 Github 拉取?》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

所以我的目标是建立一个本地开发环境,其中多个go服务(全部作为独立模块)运行在多个docker容器和一个go模块中,所有服务都可以使用它来启动一个连接到数据库等的新服务.无需在每个模块/服务中重复此代码。

所以我的结构看起来像这样(在 $gopath/src/github.com/name/backend/ 中):

|--services
|  |--service1
|     |--dockerfile
|     |--main.go
|     |--go.mod
|  |--service2
|     |--dockerfile
|     |--main.go
|     |--go.mod
|--servicehelper
|  |--servicehelper.go
|  |--go.mod

我的 dockerfile 目前只是一个普通的 go dockerfile:

from golang:alpine as build-env
workdir /backend
add . /backend
run cd /backend && go build -o service1

from alpine
run apk update && \
   apk add ca-certificates && \
   update-ca-certificates && \
   rm -rf /var/cache/apk/*
workdir /backend
copy --from=build-env /backend/service1 /backend
expose 8080
entrypoint ["./service1"]

我的 go.mod 文件也只是:

module github.com/name/backend/services/service1

go 1.17

我现在遇到的问题是,你要么必须从 github 存储库中提取一个模块,这是我不想做的,要么将 servicehelper 代码放在服务的每个模块中,这是我不想做的也不想做。

我使用 vscode,从那时起我就了解到必须将单个模块放入单个工作区文件夹中。我仍然无法在本地配置模块以导入 github.com/gorilla/mux 等普通包以及一项服务中的本地包。我使用 apple m1,希望这也不会造成问题。

我需要如何配置 go.mod 文件、docker 文件和 go 导入,以便我可以在编辑器中正常调试 go(即 servicehelper 模块不只是直接加载到 docker 容器中),并且还可以在本地运行所有内容,而无需从 github 获取 servicehelper?

更新: 我已经尝试了很多变体,但是使用这个(感谢您的回答 colm.anseo),我收到的错误消息最少,但它仍然尝试连接到 github,这是我不想要的。 因此更新后的 go.mod 文件如下所示:

module github.com/name/backend/services/service1

go 1.17

require (
    github.com/name/backend/servicehelper v1.0.0
    github.com/gorilla/mux v1.8.0
)

replace github.com/name/backend/servicehelper => ../../servicehelper

当我尝试使用 go mod tidy 构建新的 go.sum 时,会发生此错误(这就是我所说的“并且还可以在本地运行所有内容,而不必从 github 获取 servicehelper”的错误,因为我得到了这个之前的错误):

github.com/name/backend/servicehelper: cannot find module providing package github.com/name/backend/servicehelper: module github.com/name/backend/servicehelper: git ls-remote -q origin in /Users/myName/go/pkg/mod/cache/vcs/...: exit status 128:
        ERROR: Repository not found.
        fatal: Could not read from remote repository.

        Please make sure you have the correct access rights
        and the repository exists.

我不想让它连接到github,我只想让它在本地运行。在 colm.anseo 的答案的帮助下,我想我知道如何创建一个可用的 dockerfile,所以这不再是一个问题。


正确答案


replace github.com/name/backend/servicehelper => ../../servicehelper
...
github.com/name/backend/servicehelper: cannot find module

导入区分大小写。我建议您在导入、替换语句以及要导入的包的 go.mod 文件中将所有内容都设为小写。

如果您还没有准备好将代码发布到互联网上git repo(如 github.com),您可以在 go.mod 中使用 replace directive

module github.com/me/my_app

go 1.17

require (
    github.om/gorilla/handlers v1.5.1
    github.com/me/my_srv1
)

replace github.com/me/my_srv1 => ./my_srv1

在您的 go 代码中,您可以导入此代码,就像它来自互联网一样:

// go code
import (
    "github.om/gorilla/handlers"
    "github.com/me/my_srv1"
)

在您的 docker 上下文中,您必须确保目录 ./my_srv1 已复制并可在与 go build 相同的相对路径中访问。

go buildgo.mod 结合使用,将从互联网上提取 gorilla/mux 包 - 但使用本地开发目录作为您的(尚未发布)存储库的替代品。

本篇关于《如何在 Docker 中同时运行多个本地 Go 模块,而无需依赖 Github 拉取?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>