Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi-arch images with rules_oci #28

Merged
merged 9 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,46 @@ Loaded image: projects/go_web:oci_tarball
### Deploying Go web app to Heroku

```
➜ bazel** run projects/go_web:bazoku-deployment --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64
➜ bazel run projects/go_web:bazoku-deployment --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64
...
```

### Publishing Multi-arch Docker image to DockerHub

```
➜ bazel run examples/multi_arch_go:publish
...
2023/08/18 16:41:38 registry.hub.docker.com/krisfoster96/monorepo-go-web:6: digest: sha256:c50ffe127f340301993cbc323e510211d6dab9a547aa1b1a81035426e4e9d1b1 size: 648
```

```
➜ docker buildx imagetools inspect registry.hub.docker.com/krisfoster96/monorepo-go-web:6
Name: registry.hub.docker.com/krisfoster96/monorepo-go-web:6
MediaType: application/vnd.oci.image.index.v1+json
Digest: sha256:c50ffe127f340301993cbc323e510211d6dab9a547aa1b1a81035426e4e9d1b1

Manifests:
Name: registry.hub.docker.com/krisfoster96/monorepo-go-web:6@sha256:18b8c8a2606c3daf70a86db275c850080353bf5bd79a7045cca6732736b693c3
MediaType: application/vnd.oci.image.manifest.v1+json
Platform: linux/arm64

Name: registry.hub.docker.com/krisfoster96/monorepo-go-web:6@sha256:fbc6e9ca088bbb683c0743b16aa98b51583e7af8564febd26154e638b12b484a
MediaType: application/vnd.oci.image.manifest.v1+json
Platform: linux/amd64
```

```
➜ docker run --platform linux/arm64 registry.hub.docker.com/krisfoster96/monorepo-go-web:6
...
2023/08/18 15:56:08 running program's operating system target: linux
2023/08/18 15:56:08 running program's architecture target: arm64
2023/08/18 15:56:08 Going to listen on port: 8080
```

```
➜ docker run --platform linux/amd64 registry.hub.docker.com/krisfoster96/monorepo-go-web:6
...
2023/08/18 15:56:08 running program's operating system target: linux
2023/08/18 15:56:08 running program's architecture target: amd64
2023/08/18 15:56:08 Going to listen on port: 8080
```
3 changes: 1 addition & 2 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ http_archive(
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.7.0/rules_nodejs-5.7.0.tar.gz"],
)


http_archive(
name = "rules_oci",
sha256 = "176e601d21d1151efd88b6b027a24e782493c5d623d8c6211c7767f306d655c8",
Expand Down Expand Up @@ -206,7 +205,7 @@ oci_pull(
name = "distroless_base",
image = "gcr.io/distroless/base-debian11",
tag = "debug",
platforms = ["linux/amd64"],
platforms = ["linux/amd64", "linux/arm64"],
)

oci_pull(
Expand Down
55 changes: 51 additions & 4 deletions projects/go_web/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball", "oci_image_index", "oci_push")
load("@bazoku//:bazoku.bzl", "bazoku")

go_library(
Expand Down Expand Up @@ -33,10 +33,57 @@ oci_image(
tars = [":tar"],
)

archs = ["arm64", "amd64"]

[
go_binary(
name = "go_web_" + arch,
embed = [":go_web_lib"],
visibility = ["//visibility:public"],
goos = "linux",
goarch = arch,
)
for arch in archs
]

[
pkg_tar(
name = "tar_" + arch,
srcs = [":go_web_" + arch],
)
for arch in archs
]

[
oci_image(
name = "oci_image_" + arch,
base = "@distroless_base_linux_" + arch,
# with :debug distroless image, entrypoint can be changed to "sh" for debug purposes
entrypoint = ["go_web_" + arch + "_/go_web_" + arch],
tars = [":tar_" + arch],
)
for arch in archs
]

oci_image_index(
name = "multi_arch_image",
images = [
":oci_image_arm64",
":oci_image_amd64"
],
)

oci_push(
name = "publish",
repository = "registry.hub.docker.com/krisfoster96/monorepo-go-web",
remote_tags = ["6"],
image = ":multi_arch_image",
)

oci_tarball(
name = "oci_tarball",
image = ":oci_image",
repo_tags = ["projects/go_web:oci_tarball"],
name = "oci_tarball_amd64",
image = ":oci_image_amd64",
repo_tags = ["projects/go_web:oci_tarball_amd64"],
)

bazoku(
Expand Down
3 changes: 3 additions & 0 deletions projects/go_web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net/http"
"os"
"runtime"

"github.com/gorilla/mux"
"github.com/kriscfoster/multi-language-bazel-monorepo/projects/go_hello_world"
Expand All @@ -29,6 +30,8 @@ func main() {
r.HandleFunc("/", YourHandler)
// Bind to a port and pass our router in
port := getPort()
log.Println("running program's operating system target: " + runtime.GOOS)
log.Println("running program's architecture target: " + runtime.GOARCH)
log.Println("Going to listen on port: " + port)
log.Fatal(http.ListenAndServe(":"+port, r))
}
Loading