需要帮助在Go工作区(多模块)模式下创建Docker容器。

huangapple go评论80阅读模式
英文:

Need help to create Docker container in Go workspace (multi-modular) mode

问题

我需要帮助,我正在工作的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/functionality.go中的功能来使用共同功能。

当我在project1中运行docker-compose up -d命令时,它会显示在项目中导入的共同模块不在GOROOT中。
因为这里Docker只容器化了Project1目录中的文件。

我该如何分别将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 ???

答案1

得分: 3

编译应用程序时,编译器需要所有导入的包。当构建Docker镜像时,context决定了可用的文件;在你的情况下,这将是Project1文件夹(例如)。这意味着当你在Dockerfile中运行go build时,只有Project1文件夹中的文件可用。编译器将检查工作区文件,但不会找到它(因为它不在上下文中)。

你可以通过更改context来解决这个问题,使其包括完整的工作区,例如Project1/docker-compose.yml

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

(你还需要更新你的Dockerfile,因为你将复制子目录等)。

然而,上述方法并不是非常理想(它在某种程度上违背了将项目放在单独文件夹中的初衷)。根据你的发布流程,更好的选择可能是将所有内容都检入(例如到Git仓库),执行go get -u(更新go.mod),然后构建Docker镜像(允许go从仓库中检索依赖项)。

英文:

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).

huangapple
  • 本文由 发表于 2022年6月28日 17:37:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/72784081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定