Forcing docker to use linux/amd64 platform by default on macOS
Asked Answered
G

7

225

Current beta version of docker requires you to specify a --platform=linux/amd64 each time you need to build or run an amd64 image/container.

The documentation mentions

When running an image with multi-architecture support, docker will automatically select an image variant which matches your OS and architecture.

The documentation does not specify a way to alter this automatic behaviour using env variables. It seems to ignore both BUILDPLATFORM and TARGETPLATFORM.

Is there any other way to force docker to run all build and run commands with a platform linux/amd64 instead of linux/arm64/v8 by default on macOS running on apple-silicon?

Gerik answered 7/1, 2021 at 12:29 Comment(0)
F
343

You can set the environment variable DOCKER_DEFAULT_PLATFORM:

export DOCKER_DEFAULT_PLATFORM=linux/amd64
Fluting answered 1/4, 2021 at 8:23 Comment(4)
This was introduced in v19.03.0 in 03/2019. I do not understand how I missed that.Gerik
This is great and it works, but i've also found I need to unset it when creating clusters with kind. If you get errors like docker: Cannot overwrite digest sha256:..., try unsetting the DOCKER_DEFAULT_PLATFORM var.Issue
is it possible to configure this option within a docker configuration file rather than a shell environment?Judsen
The link provided by @kctang is broken. Here is one that works: blog.driftingruby.com/articles/2022/04/09/…Divorce
D
262

Docker images built with Apple Silicon (or another ARM64 based architecture) can create issues when deploying the images to a Linux or Windows based *AMD64 environment (e.g. AWS EC2, ECS, etc.). For example, you may try to upload your docker image made on the M1 chip to an AWS ECR repository and it fails to run. Therefore, you need a way to build AMD64 based images on the ARM64 architecture, whether it's using Docker build (for individual images) or docker-compose build (e.g. for multi-image apps running in a docker compose network).

For building single docker images: Set your environment variable using the command line or modifying your .bashrc or .zshenv file as suggested in the accepted answer.

export DOCKER_DEFAULT_PLATFORM=linux/amd64

Alternatively, in the Dockerfile, include the following flag in the FROM command (for a multi-stage Dockerfile build, the flag is only needed for the first stage):

FROM --platform=linux/amd64 python:3.7-alpine

For building images as part of a docker-compose build, include the platform: linux/amd64 for each service. For example:

  services:  
    frontend:  
      platform: linux/amd64
      build: frontend  
      ports:
        - 80:80  
      depends_on:
        - backend  
    backend:  
      platform: linux/amd64
      build: backend  
Dismal answered 19/10, 2021 at 19:11 Comment(6)
What is the source of the "issues"? You mean like accidentally running ARM image on AMD64 platforms? Is it even possible?Gerik
I prefer this solution (I'm using docker-compose) over the accepted answer of setting env variable on the host OSInterinsurance
This works great. I'm using docker-compose to build linux/amd64 applications on a M1 MacBook Pro.Siloxane
This is pretty old, but this points to the best option IMO, i chose to keep platform in docker-compose.yaml but adding --platform=linux/amd64 do the FROM section of DockerFile seemed to be the best fix.Toddler
I prefer putting this in the docker-compose.yml file as well. This saved me. Thanks a lot!Vociferate
will it work for docker-compose v3 files?Myrtismyrtle
U
54

You don't need to export the env variable as mentioned in one of the answers, you can run it as part of the command a single time by doing:

DOCKER_DEFAULT_PLATFORM=linux/amd64 docker-compose build

Keep in mind that if you've already downloaded the image for a different platform, docker will keep using that image no matter what platform you specify as your default, you would delete the image using docker image rm your_img first to fix that.

Uranography answered 4/11, 2021 at 13:2 Comment(2)
mind that if you system requires sudo to run docker, you have to put the PLATFORM into the sudo, note before that. Correct example: sudo DOCKER_DEFAULT_PLATFORM=linux/amd64 docker run --gpus all --shm-size 1g -p 8080:80 ghcr.io/huggingface/text-generation-inference:1.1.0 Perhaps that is obvious, but some script kiddies can appreciate.Buffer
best solution when using compose tyIve
A
22

You can use buildx (mobi) which suipport cli for platform.

docker buildx build --platform linux/amd64 .
Aver answered 25/2, 2022 at 5:20 Comment(2)
Here is my Debian ARM docker build github.com/aem-design/docker-tini/blob/debian-arm/build.ps1 and here is the pipeline github.com/aem-design/docker-tini/blob/debian-arm/.github/…. Its used here github.com/aem-design/docker-oracle-jdk/blob/jdk8-arm/….Aver
Well buildx is an extra addition to docker hence ive mentioned it. But for Docker as is the docs on ENV variables are here docs.docker.com/engine/reference/commandline/cli/… and they seem to be clear and to the point without any snark remarks.Aver
S
14

you can set

export DOCKER_DEFAULT_PLATFORM=linux/amd64

in a .zshrc file for Mac M1

Svensen answered 25/6, 2022 at 7:58 Comment(0)
A
4

You can add in your docker-compose.yaml:

services:
  service_name:
    environment:
      - DOCKER_DEFAULT_PLATFORM=linux/amd64
Allotropy answered 8/6, 2022 at 8:59 Comment(3)
That doesnt really work for me. I think the "platform" is the way to go.Vu
This would work for docker-in-docker. Not clear otherwise how this variable would be made available to the system docker daemonSulfa
I just tried this and it had no effect.Cupronickel
M
2

Add this snipped to your ~/.zshrc and ~/.bashrc. It allows you not to repeat the flag anytime you perform a docker run command:

# useful only for Mac OS Silicon M1, 
# still working but useless for the other platforms
docker() {
  if [[ `uname -m` == "arm64" ]] && [[ "$1" == "run" || "$1" == "build" ]]; then
     /usr/local/bin/docker "$1" --platform linux/amd64 "${@:2}"
  else
     /usr/local/bin/docker "$@"
  fi
}
Mert answered 7/6, 2023 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.