Home > OS >  Docker - How to build image for M1 Mac or AMD conditionally from Dockerfile?
Docker - How to build image for M1 Mac or AMD conditionally from Dockerfile?

Time:12-30

Some context:
-I'm a Docker newbie (on it since 1 day),
-I've got a small VM running linux/AMD and I own a M1 Mac (ARM),
-I'd like to also use Container for Dev (instead of virtual env).

For building my container for prod, being on a M1 Mac, I have the below Dockerfile:
See the --platform=linux/amd64 arg in FROM and It works (= I'm able to deploy).

FROM --platform=linux/amd64 python:3.10-slim-bullseye
WORKDIR /usr/src/app
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_ENV=development 
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]

However, how can I say in my Dockerfile to not use --platform=linux/amd64 if I want to build for local dev?
I've seen some post on SO with conditions in dockerfile but only for RUN command.
Any idea or best practices?
Thanks.

CodePudding user response:

I believe you can use the --platform parameter on docker buildx build or docker build to set platform(s) to build the image which will be used within any FROM calls within the Dockerfile if nothing else is specified (see Dockerfile FROM), as mentioned in the documentation.

You can then use the TARGETPLATFORM variable within your Dockerfile to get what platform it's being built for if needed. If you want to change the default platform to build for, you can set the DOCKER_DEFAULT_PLATFORM environment variable.

  • Related