bazelbuild / rules_docker

Rules for building and handling Docker images with Bazel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use binary output from go_binary for go_image

salrashid123 opened this issue Β· comments

🐞 bug report

Affected Rule

  • container_image
  • go_image

Is this a regression?

not sure, this is more of a question/clarification

Description

I'd like to specify an entrypoint for an container_image that bases off of a go_image.

In Dockerfile speak, i'd build the go binary as /server and i'd load and use that as the entrypoint like this:

FROM golang:1.19 as build

WORKDIR /go/src/app
COPY . .
RUN go mod download
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /go/bin/server

FROM gcr.io/distroless/base@sha256:75f63d4edd703030d4312dc7528a349ca34d48bec7bd754652b2d47e5a0b7873
COPY --from=build /go/bin/server /
EXPOSE 8080
CMD ["/server"]

with bazel, the BUILD

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_source")
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@bazel_gazelle//:def.bzl", "gazelle")

gazelle(name = "gazelle")

go_binary(
    name = "main",
    out = "server",
    embed = [":go_default_library"],
    visibility = ["//visibility:public"],
    goos = "linux", 
    goarch = "amd64",     
)

go_image(
    name = "main_image",  
    out = "server",
    base = "@distroless_static_debian11//image",  
    visibility = ["//visibility:private"],
    binary = ":main",     
)

container_image(
    name = "server",
    base = ":main_image",
    workdir = "/",
    entrypoint = ["/server"],
    ports = ["8080"],
)

go_library(
    name = "go_default_library",
    srcs = ["main.go"],
    importpath = "main",
    visibility = ["//visibility:private"],
    deps = [
        "@org_golang_x_net//http2:go_default_library",
        "@com_github_gorilla_mux//:go_default_library",
    ],
)

i do specify the go_binary rule with out="server" which'll just output bazel-bin/server

but unsure how to specify the binary to use an run in the entrypoint for the container_image.

i do know i can workaround this by setting the following in container_image but this seems like a workaround for something basic i maybe missing

    symlinks = {"/server": "/app//main"},    

🌍 Your Environment

Operating System:

  
linux/amd  debian
  

Output of bazel version:

  
bazel 6.1.1
  

Rules_docker version:

  
rules_docker-v0.23.0
  

Anything else relevant?

similar ask, i think:
#1649