Hi. I'm testing out pants 2.15 and the Target Envi...
# general
a
Hi. I'm testing out pants 2.15 and the Target Environments feature specifically. I must say it looks really good and will definitely replace our previous approach of using docker-in-docker and a builder image (because of the necessity of running pants in the same environment as the target). I have a question though. Is there a way to specify the target platform for a
docker_image
target? An example would be building a linux/amd64 image on an m1 mac. I managed to build a pex using
platform=linux_x86_64
but I suppose that a docker_image built on an arm will target
arm64
. Do I understand correctly that this example workflow from the docs considers the case with an x86 mac - the same architecture?https://www.pantsbuild.org/docs/environments#use-a-docker_environment-to-build-the-inputs-to-a-docker_image I'm not even sure if this is something I should expect to be available. This is a very specific case of quick-prototyping - building a dev version of an image locally, publishing and running it in the test environment that is based on x86
Maybe I'm mistaken or missing something. Any help would be appreciated 🙂
w
We use this plugin @ AMRA:
Copy code
import platform
from typing import Iterable

from pants.backend.docker.target_types import DockerBuildOptionFieldMixin
from pants.backend.docker.target_types import DockerImageTarget
from pants.backend.docker.target_types import OptionValueFormatter
from pants.engine.rules import Rule
from pants.engine.target import StringField
from pants.util.strutil import softwrap

X86_64 = "linux/amd64"
ARM_64 = "linux/arm64"


class DockerImagePlatformField(DockerBuildOptionFieldMixin, StringField):
    alias = "platform"
    help = softwrap(
        """
        Target platform of the built image (e.g., "linux/amd64")
        """
    )
    docker_build_option = "--platform"
    default = X86_64

    _python_to_docker_platforms = {
        "x86_64": X86_64,
        "aarch64": ARM_64,
    }

    def option_values(self, *, value_formatter: OptionValueFormatter) -> Iterable[str]:
        if self.value:
            machine = platform.machine()
            if self._python_to_docker_platforms.get(machine) != self.value:
                yield value_formatter(self.value)


def rules() -> Iterable[Rule]:
    return [
        DockerImageTarget.register_plugin_field(DockerImagePlatformField),
    ]
but I suppose that a docker_image built on an arm will target
arm64
.
Yep
I'll just note that depending on a multitude of factors:
This is a very specific case of quick-prototyping - building a dev version of an image locally, publishing and running it in the test environment that is based on x86
This can be a lot of pain. I have found that
qemu
for silicon is not particularly reliable yet
a
Thank you very much. I'll look into the plugin you shared
As for the reliability, thanks for your notice. I don't really have much trust in this approach. We need something like this now, but I hope we'll be able to drop it as soon as our infrastructure transitions to a more mature state
👍 1