英文:
Bazel build docker container with local golang module
问题
让我先说一下,我对Bazel还不熟悉。我正在尝试从一个包含本地模块引用的golang项目构建一个Docker容器。
首先,我正在创建一个本地的golang模块:
go mod init go-example
这是项目的一般结构:
.
├── BUILD.bazel
├── WORKSPACE
├── cmd
│ └── hello
│ ├── BUILD.bazel
│ └── main.go
├── go.mod
├── go.sum
└── pkg
└── echo
├── BUILD.bazel
└── echo.go
在main.go
中,我正在从本地模块导入pkg/echo
。
import (
"go-example/pkg/echo"
)
(top level BUILD.bazel)
...
# gazelle:prefix go-example
✅ 默认的bazel构建可以工作
$ bazel run //:gazelle
$ bazel build //cmd/hello
❌ Docker构建失败。我得到以下错误:
(cmd/hello/BUILD.bazel)
...
go_image(
name = "docker",
srcs = ["main.go"],
importpath = "go-example/cmd/hello",
)
$ bazel build //cmd/hello:docker
...
compilepkg: missing strict dependencies:
/private/var/tmp/_bazel[...]/__main__/cmd/hello/main.go: import of "go-example/pkg/echo"
No dependencies were provided.
英文:
Let me start by saying I'm new to Bazel. I am trying to build a Docker container from a golang project that contains local module references.
First I'm creating a local golang module:
go mod init go-example
Here is the general project structure:
.
├── BUILD.bazel
├── WORKSPACE
├── cmd
│   └── hello
│   ├── BUILD.bazel
│   └── main.go
├── go.mod
├── go.sum
└── pkg
   └── echo
   ├── BUILD.bazel
   └── echo.go
In main.go
I am importing pkg/echo
from the local module.
import (
"go-example/pkg/echo"
)
(top level BUILD.bazel)
...
# gazelle:prefix go-example
✅ Default bazel build works
$ bazel run //:gazelle
$ bazel build //cmd/hello
❌ Docker build fails. I get the following error:
(cmd/hello/BUILD.bazel)
...
go_image(
name = "docker",
srcs = ["main.go"],
importpath = "go-example/cmd/hello",
)
$ bazel build //cmd/hello:docker
...
compilepkg: missing strict dependencies:
/private/var/tmp/_bazel[...]/__main__/cmd/hello/main.go: import of "go-example/pkg/echo"
No dependencies were provided.
答案1
得分: 1
找到了,如果有其他人遇到这个问题,我在这里发布。
答案很简单-你需要在go_image
规则内嵌入go_library
规则。这是我的cmd/hello/BUILD.bazel
文件,我还在其中将go镜像嵌入到一个Docker容器中:
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
go_library(
name = "hello_lib",
srcs = ["main.go"],
importpath = "go-example/cmd/hello",
visibility = ["//visibility:private"],
deps = ["//pkg/echo"],
)
go_binary(
name = "hello",
embed = [":hello_lib"],
visibility = ["//visibility:public"],
)
go_image(
name = "hello_go_image",
embed = [":hello_lib"],
goarch = "amd64",
goos = "linux",
pure = "on",
)
container_image(
name = "docker",
base = ":hello_go_image",
)
现在可以使用bazel build //cmd/hello:docker
来运行了。
英文:
Figured it out, posting here if anyone else stumbles on this.
The answer is simple - you need to embed the go_library
rule within the go_image
rule. Here is my cmd/hello/BUILD.bazel
where I also embed the go image in a docker container
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
go_library(
name = "hello_lib",
srcs = ["main.go"],
importpath = "go-example/cmd/hello",
visibility = ["//visibility:private"],
deps = ["//pkg/echo"],
)
go_binary(
name = "hello",
embed = [":hello_lib"],
visibility = ["//visibility:public"],
)
go_image(
name = "hello_go_image",
embed = [":hello_lib"],
goarch = "amd64",
goos = "linux",
pure = "on",
)
container_image(
name = "docker",
base = ":hello_go_image",
)
Now it works to run bazel build //cmd/hello:docker
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论