aloof-appointment-30987
01/10/2023, 1:03 AMgo_binary
?
I've confirmed that if I build using
env GOOS=linux GOARCH=arm go build .
then I create a file
target that references the binary result of the above command and copy that file into a Dockerfile
image and run using ./pants run path/to/project:docker_image
The docker image executes the binary as expected.
If I instead copy the output of go_binary
into the Dockerfile
image, I get the failure exec /app: exec format error
This indicates the go binary is built for the wrong platform.
I've tried
env GOOS=linux GOARCH=arm ./pants run path/to/project:docker
without success.happy-kitchen-89482
01/10/2023, 2:16 AMhappy-kitchen-89482
01/10/2023, 2:16 AMhappy-kitchen-89482
01/10/2023, 2:17 AMgo env -json
, so it might well work!happy-kitchen-89482
01/10/2023, 2:18 AMfast-nail-55400
01/10/2023, 6:45 AMdocker_environment
target type to run the Go compilation inside of a Docker container (and the container image will need a Go SDK installed)fast-nail-55400
01/10/2023, 6:45 AMfast-nail-55400
01/10/2023, 6:46 AMfast-nail-55400
01/10/2023, 6:47 AMfast-nail-55400
01/10/2023, 6:48 AMdocker_environment
is likely the best method for now)aloof-appointment-30987
01/10/2023, 5:10 PMenv GOOS=linux GOARCH=arm ./pants --subprocess-environment-env-vars="['GOOS','GOARCH']" --no-local-cache run apps/go/greeter_en:docker
I was hoping not to have to copy the git repo source code into the Dockerfile. Being able to compile a binary in the same way Pants
supports pex
would be fabulous as would the automatic inference that a docker_image
is a dependee of its transitive dependencies. If there is an intent to support this, I'd love to watch it and when I become skilled enough in Go and Pants I'd be happy to contribute. Presently, I'm still just mesmerized by your awesome toolset!aloof-appointment-30987
01/10/2023, 5:40 PMdocker_enviromment
and it appears to be exactly what I need both for this concern and to support Apple arm64. Is there an example project that demonstrates this new target in action? The docs use terms that aren't familiar to me yet. I'll let you know if I can coax it out on my own!aloof-appointment-30987
01/10/2023, 8:07 PMdocker_environment
is supported by go_binary
. Am I misunderstanding the docs?
Here's what I've tried.
• updated my pants version to "2.15.0rc1"
• defined a docker_environment
in a root level BUILD file
docker_environment(
name="golang_alpine",
platform="linux_x86_64",
image="1.17.13-alpine3.16",
)
• referenced said env in go_binary
go_binary(
name="greeter_en",
environment="golang_alpine"
)
• The binary output is referenced by a Dockerfile, left untouched
./pants run apps/go/greeter_en:docker
14:55:52.54 [ERROR] 1 Exception encountered:
Engine traceback:
in `run` goal
in Resolve transitive targets
in Resolve direct dependencies of target - apps/go/greeter_en:docker
in Find all packageable targets in project
in Find all targets in the project
InvalidFieldException: Unrecognized field `environment=golang_alpine` in target apps/go/greeter_en:greeter_en. Valid fields for the target type `go_binary`: ['_dependencies', 'description', 'main', 'output_path', 'restartable', 'tags'].
fast-nail-55400
01/10/2023, 9:24 PMaloof-appointment-30987
01/11/2023, 7:43 PMdocker_environment
is not supported by the Pants Go backend so I cannot tap into it to build go inside a Pants managed Docker container
• GOOS and GOARCH are not passed through to the go build
sub process using --subprocess-environment-env-vars
• go_binary
compiles artifacts for the host platform only
I'm struggling to configure my multi-module monorepo such that I can
• Leverage Pants to test, lint, etc
• Produce binary with 1st and 3rd party dependencies
• Containerize the same using Docker
Specifically the third bullet is presenting challenge. Using the target docker_image
, the sandbox does not have the full source tree so I am unclear as to how to include the needed source files, and in what expected directory structure, so I can use then build inside the container.
How can I structure my multi-module monorepo, and it's go.mod definitions, so that I can both
• Execute goals via Pants
• Assemble source so that I can execute Go build commands inside a Dockerfile?
Would it be advisable or possible to copy the entire repo as source into the container to then use pants inside the container?aloof-appointment-30987
01/11/2023, 10:59 PM├── apps
│ ├── go
│ │ ├── greeter_en
│ │ │ ├── BUILD
│ │ │ ├── Dockerfile
│ │ │ └── main.go
│ │ └── greeter_es
│ │ ├── BUILD
│ │ └── main.go
├── libraries
│ ├── go
│ │ ├── BUILD
│ │ ├── embed_example
│ │ │ ├── BUILD
│ │ │ ├── hello.txt
│ │ │ ├── lib.go
│ │ │ ├── lib_test.go
│ │ │ └── testdata
│ │ │ └── f.txt
│ │ ├── greeter
│ │ │ ├── BUILD
│ │ │ ├── greet.go
│ │ │ └── greet_test.go
│ │ └── uuid
│ │ ├── BUILD
│ │ ├── uuid_gen.go
│ │ └── uuid_gen_test.go
├── BUILD.pants
├── go.mod
├── pants
└── pants.toml
./libraries/go/BUILD contains:
files(
name = "source-files",
sources = ["./**/*"]
)
This makes the source files available to the Dockerfile, via docker_image
, inside the sandbox.
Likewise, ./apps/go/greeter_en/BUILD contains:
files(
name = "source-files",
sources = ["./**/*.go"]
)
go_package(name="go-package")
go_binary(
name="greeter_en",
)
docker_image(
name="docker",
dependencies=[
"libraries/go:source-files",
"//:go-mod-file",
":source-files"
]
)
The docker_image
> dependencies
includes:
• all library source files
• the go.mod file
• the app source files
The Dockerfile then copies these source files as:
COPY go.mod .
COPY libraries/go ./libraries/go
COPY apps/go/greeter_en .
This makes the source files available inside the container so that go build .
etc has what it needs.
It works!
I can run the local go binary via:
./pants run apps/go/greeter_en:greeter_en
and run the docker image via:
./pants run apps/go/greeter_en:docker
aloof-appointment-30987
01/11/2023, 11:04 PM