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

Use binary output from go_binary for go_image #2248

Open
salrashid123 opened this issue Apr 17, 2023 · 1 comment
Open

Use binary output from go_binary for go_image #2248

salrashid123 opened this issue Apr 17, 2023 · 1 comment
Labels
Can Close? Will close in 30 days unless there is a comment indicating why not

Comments

@salrashid123
Copy link

🐞 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

Copy link

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days.
Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_docker!

@github-actions github-actions bot added the Can Close? Will close in 30 days unless there is a comment indicating why not label Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Can Close? Will close in 30 days unless there is a comment indicating why not
Projects
None yet
Development

No branches or pull requests

1 participant