283

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?

7 Answers 7

438

You can set the environment variable DOCKER_DEFAULT_PLATFORM:

export DOCKER_DEFAULT_PLATFORM=linux/amd64
Sign up to request clarification or add additional context in comments.

4 Comments

This was introduced in v19.03.0 in 03/2019. I do not understand how I missed that.
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.
is it possible to configure this option within a docker configuration file rather than a shell environment?
@jeffmcc that link is dead now as well. it's here: blog.driftingruby.com/docker-builds-are-slow-on-m1
314

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  

7 Comments

What is the source of the "issues"? You mean like accidentally running ARM image on AMD64 platforms? Is it even possible?
I prefer this solution (I'm using docker-compose) over the accepted answer of setting env variable on the host OS
This works great. I'm using docker-compose to build linux/amd64 applications on a M1 MacBook Pro.
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.
I prefer putting this in the docker-compose.yml file as well. This saved me. Thanks a lot!
|
63

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.

2 Comments

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.
best solution when using compose ty
28

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

docker buildx build --platform linux/amd64 .

2 Comments

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.
19

you can set

export DOCKER_DEFAULT_PLATFORM=linux/amd64

in a .zshrc file for Mac M1

1 Comment

This would affect all containers, though
5

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
}

Comments

2

You can add in your docker-compose.yaml:

services:
  service_name:
    environment:
      - DOCKER_DEFAULT_PLATFORM=linux/amd64

3 Comments

That doesnt really work for me. I think the "platform" is the way to go.
This would work for docker-in-docker. Not clear otherwise how this variable would be made available to the system docker daemon
I just tried this and it had no effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.