Restrict target platform in Dockerfile

  Kiến thức lập trình

I have a Dockerfile which is intended to be used to build linux/amd64 images only. I thought I can use FROM --platform=linux/amd64 base-image to achieve this but this does not seem to work as I expect.

For example, I expect this Dockerfile to only produce linux/amd64 images:

FROM --platform=linux/amd64 ubuntu:22.04
WORKDIR /placeholder

however if I build it with docker build --platform linux/arm64 . I get an image marked as linux/arm64 while arch inside its containers actually returns x86_64.

It seems like FROM --platform specifies the actual platform of the image while docker build --platform specifies the platform the image is labeled with. And these two can differ producing strange images without throwing any errors at build time. I assume if I just build this Dockerfile on an arm64 host I’ll get the same result.

So, the question is: what would be the right way to ensure this won’t ever happen and my Dockerfile can only be used to build an image targeting linux/amd64? It would be nice to allow it to be built and run on ARM hosts (via emulation) but the target platform must still be linux/amd64.

1

LEAVE A COMMENT