需要帮助以在GO Workspace(多模制)模式下创建Docker容器

发布于 2025-02-11 00:12:12 字数 761 浏览 0 评论 0原文

我需要帮助,我正在GO工作区工作,并且使用共同功能在工作区中有多个项目。因此,我要做的是制作一个单独的模块,该模块包含其他模块使用的所有常见功能。

所有模块都可以在我们使用GO工作区时共享其代码。

文件结构如下:

Workspace
│
├── Project1
│      ├── main.go
│      ├── docker-compose.yml
│      ├── Dockerfile
│      └── go.mod
│
├── Project2
│      ├── main.go
│      ├── docker-compose.yml
│      ├── Dockerfile
│      └── go.mod
│
├── Common
│      ├── functionality.go
│      └── go.mod
│
└── go.work

project1和project2使用common/fuctional.go中的功能,通过将它们导入项目中。

当我运行docker -compose -d命令中的project1中,它说您在项目中导入的公共模块不在goroot中。 因为在这里Docker仅在目录中的文件Project1中容纳文件,

我该如何分别dockerize project1project2 ???

I need help, I'm working in the go workspace and there are multiple projects in the workspaces using the common functionality. So, What I did is to make a separate module that contains all the common functionalities that are used by the other modules.

All modules can share their code as we are using the go workspace.

The file structure is as follows:

Workspace
│
├── Project1
│      ├── main.go
│      ├── docker-compose.yml
│      ├── Dockerfile
│      └── go.mod
│
├── Project2
│      ├── main.go
│      ├── docker-compose.yml
│      ├── Dockerfile
│      └── go.mod
│
├── Common
│      ├── functionality.go
│      └── go.mod
│
└── go.work

Project1 and Project2 use the functionalities that are in the common/functionality.go by importing them into the project.

when I run the docker-compose up -d command in project1 It says the common module that you import in the project is not in the GOROOT.
because here docker only containerizes the files that are in the directory Project1

How can I dockerize separately Project1 and Project2 ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老街孤人 2025-02-18 00:12:12

要编译您的应用程序,编译器需要所有导入的软件包。当您构建Docker映像时确定可用的文件;在您的情况下,这将是project1(例如)。这意味着,当您在dockerfile中运行时, 唯一可用的文件是project1文件夹中的文件。编译器将检查一个工作空间文件,但找不到一个(因为它在上下文中)。

您可以通过更改上下文来解决此问题,以包括完整的工作区,例如project1/docker-compose.yml :(

project1:
    build:
      context: ../
      dockerfile: Project1/dockerfile
    environment:
      ....

您需要更新dockerfile也是因为您将在子目录等中复制)。

但是,以上并不是真正理想的(它在某种程度上打败了将项目放在单独的文件夹中的地步)。根据您的发布过程,更好的选项可能是检查所有内容(例如git repo),做go get -u(更新go.mod) ,然后构建Docker映像(允许从存储库中检索依赖项)。

To compile your application the compiler requires all imported packages. When you build a docker image the context determines what files are available; in your case this will be Project1 (for example). This means that when you run go build in your Dockerfile the only files available are those in the Project1 folder. The compiler will check for a workspace file but will not find one (because it's not in the context).

You can work around this by changing the context such that it includes the full workspace e.g. Project1/docker-compose.yml:

project1:
    build:
      context: ../
      dockerfile: Project1/dockerfile
    environment:
      ....

(you will need to update your Dockerfile as well because you will be copying in subdirectories etc).

However the above is not really ideal (it somewhat defeats the point of putting the projects in separate folders). Depending upon your release process a better option might be to check everything in (e.g. to a Git repo), do a go get -u (to update the go.mod), and then build the docker image (allowing go to retrieve the dependencies from the repo).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文