kurtosis-tech / kurtosis

A platform for packaging and launching ephemeral backend stacks with a focus on approachability for the average developer.
https://docs.kurtosistech.com/
Apache License 2.0
380 stars 53 forks source link

Provide build-time variables to Dockerfile when building images #2214

Closed leovct closed 8 months ago

leovct commented 8 months ago

Background & motivation

I would like to be able to pass build args (or build-time variables) to Dockerfile when building the images with ImageBuildSpec. This is something I can get around at the moment by executing my instructions at container startup and using environment variables, but it would be nice to have it. It could speed up the startup of devnets considerably.

$ docker build --help | grep build-arg
      --build-arg stringArray         Set build-time variables

Desired behaviour

image = ImageBuildSpec(
      # Name to give built image
      # MANDATORY
      image_name="kurtosistech/example-datastore-server"

      # Locator to build context within the Kurtosis package
      # MANDATORY
      build_context_dir="./server"

      # Stage of image build to target for multi-stage container image
      # OPTIONAL
      target_stage=""

      # Set build-time variables
      # OPTIONAL
      build_args={
            "MY_VALUE": "test",
            "MY_OTHER_VALUE": "1",
      }
)

How important is this to you?

Nice to have; this feature would make using Kurtosis more enjoyable.

What area of the product does this pertain to?

Other: anything not covered by the above

mieubrisse commented 8 months ago

Hey @leovct , this is a great idea! We don't have anyone else currently using build args so we're likely not going to be able to prioritize this soon, but would you be open to submitting a PR? The relevant code lives here: https://github.com/kurtosis-tech/kurtosis/blob/be97c3834e7e4513537c0ac22801afdcfced9065/container-engine-lib/lib/backend_interface/objects/image_build_spec/image_build_spec.go#L15

leovct commented 8 months ago

Hey @leovct , this is a great idea! We don't have anyone else currently using build args so we're likely not going to be able to prioritize this soon, but would you be open to submitting a PR? The relevant code lives here:

https://github.com/kurtosis-tech/kurtosis/blob/be97c3834e7e4513537c0ac22801afdcfced9065/container-engine-lib/lib/backend_interface/objects/image_build_spec/image_build_spec.go#L15

Will do once #2223 is merged :)

tedim52 commented 8 months ago

Hey! here's some pointers to get you started in the codebase (lmk if I can provide more detail elsewhere)

build_args need to get down to ImageBuildOptions.BuildArgs here: https://github.com/kurtosis-tech/kurtosis/blob/6245ddf4c0681cfc81021aeda0eb2e245ef5b275/container-engine-lib/lib/backend_impls/docker/docker_manager/docker_manager.go#L1432

This function is where we actually call the docker client to build the image. The function gets called here: https://github.com/kurtosis-tech/kurtosis/blob/6245ddf4c0681cfc81021aeda0eb2e245ef5b275/core/server/api_container/server/startosis_engine/startosis_validator/images_validator.go#L118

LMK and I can provide some more direciton on navigating the types of the codebase (note: we have two representations of ImageBuildSpec (one in our Starlark engine, one in our container engine library that you'll have to add build_args to!

Thanks for the contribution!