Home > front end >  Docker / Buildx Use Multiple Architectures in Different Stages of Multi-Stage Dockerfile
Docker / Buildx Use Multiple Architectures in Different Stages of Multi-Stage Dockerfile

Time:01-16

So I have issues building my docker container for arm/v6 because a tool I use for dependency management (Poetry to be specific) depends on some python wheels that do not exist / are not prebuilt on arm/v6. I only use this tool in one stage to generate a file (requirements.txt) to use with pip in a later stage. Is it possible to run a single stage on e.g. x86, copy over this generated file and run all of the other stages with arm/v6?

CodePudding user response:

With buildkit there are a set of predefined arguments you can use, along with the --platform parameter to FROM to select a specific platform from a multi-platform image:

FROM --platform=$BUILDPLATFORM your_base:tag as build

...

FROM your_run:tag as release
COPY --from=build requirements.txt .
...

This will use the current build host platform for the build stage, and the target platform for the release stage. There are other build args documented in the Dockerfile documentation.

For more details on these features, see Docker's multi-platform build documentation.

CodePudding user response:

Yes, it is possible to run a single stage on x86 architecture and then copy the generated file to use with pip in later stages with arm/v6 architecture.

One way to do this is to use multi-stage builds in your Dockerfile. Multi-stage builds allow you to use multiple FROM statements in your Dockerfile, each with its own set of instructions.

You can use the following approach:

  1. In the first stage, you can use an x86-based image and run Poetry to generate your requirements.txt file.
  2. Then you can copy the requirements.txt file to a temporary location using the COPY instruction.
  3. In the second stage, you can use an arm/v6-based image and copy the requirements.txt file from the first stage using the COPY --from= instruction.
  4. Then you can use pip to install the dependencies specified in the requirements.txt file.
  5. In the remaining stages, you can use arm/v6 image to build your final image.

The example Dockerfile:

# First stage
FROM x86_based_image as builder
RUN poetry install
RUN poetry export -f requirements.txt > requirements.txt
COPY requirements.txt /tmp/requirements.txt

# Second stage
FROM armv6_based_image
COPY --from=builder /tmp/requirements.txt .
RUN pip install -r requirements.txt
# other instructions

This way, you can use the x86-based image to generate the requirements.txt file and then use it with arm/v6-based image to install the dependencies in the remaining stages.

Of course you need to adjust the remaining part of your Dockerfile.

  • Related