78

I want to run a docker container for Ganache on my MacBook M1, but get the following error:

The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested

After this line nothing else will happen anymore and the whole process is stuck, although the qemu-system-aarch64 is running on 100% CPU according to Activity Monitor until I press CTRL+C.

My docker-files come from this repository. After running into the same issues there I tried to isolate the root cause and came up with the smallest setup that will run into the same error.

This is the output of docker-compose up --build:

Building ganache
Sending build context to Docker daemon  196.6kB
Step 1/17 : FROM trufflesuite/ganache-cli:v6.9.1
 ---> 40b011a5f8e5
Step 2/17 : LABEL Unlock <ops@unlock-protocol.com>
 ---> Using cache
 ---> aad8a72dac4e
Step 3/17 : RUN apk add --no-cache git openssh bash
 ---> Using cache
 ---> 4ca6312438bd
Step 4/17 : RUN apk add --no-cache   python   python-dev   py-pip   build-base   && pip install virtualenv
 ---> Using cache
 ---> 0be290f541ed
Step 5/17 : RUN npm install -g npm@6.4.1
 ---> Using cache
 ---> d906d229a768
Step 6/17 : RUN npm install -g yarn
 ---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
 ---> Running in 991c1d804fdf

docker-compose.yml:

version: '3.2'
services:
  ganache:
    restart: always
    build:
      context: ./development
      dockerfile: ganache.dockerfile
    env_file: ../.env.dev.local
    ports:
      - 8545:8545

  ganache-standup:
    image: ganache-standup
    build:
      context: ./development
      dockerfile: ganache.dockerfile
    env_file: ../.env.dev.local
    entrypoint: ['node', '/standup/prepare-ganache-for-unlock.js']
    depends_on:
      - ganache

ganache.dockerfile:

The ganache.dockerfile can be found here.

Running the whole project on an older iMac with Intel-processor works fine.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
MrsBookik
  • 825
  • 1
  • 7
  • 6
  • can you run `hello-world` docker image? – Sachith Muhandiram Sep 04 '21 at 12:22
  • 1
    It really looks like the image you're trying to use isnt available for arm architecture which is the one for the M1 MAC. Try running `docker run ubuntu:latest echo hello world` instead. this image comes with linux/arm64/v8 architcture comapred to v5 in `hello-world:linux` (which is the only one that comes with any dort of arm support) – Noam Yizraeli Sep 04 '21 at 14:37
  • 3
    Hello World works, yes. But so many other docker images not that we are using in the company. I am only person with M1 :( – MrsBookik Sep 04 '21 at 19:34

8 Answers8

49

If you're planning to run the image in your laptop, you need to build it for the cpu architecture of that particular machine. You can provide the --platform option to docker build (or even to docker-compose) to define the target platform you want to build the image for.

For example:

docker build --platform linux/arm64  .
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Theofilos Papapanagiotou
  • 5,133
  • 1
  • 18
  • 24
  • 2
    add "buildx" and run like: $ docker buildx build --platform linux/amd64 . – izy Sep 01 '22 at 00:50
  • 4
    for those using docker compose, add the `platform: linux/amd64` option to the affected images in your `docker-compose.yml` – Stetzon Jan 24 '23 at 17:14
39

On M1 MacBook Pro, I've had success using docker run --platform linux/amd64

Example

docker run --platform linux/amd64 node
Ryan
  • 4,594
  • 1
  • 32
  • 35
  • 4
    Note that `node` should be replaced with the appropriate image name. Otherwise it'll be pulling and running `node` rather than the desired image. – leanne Jan 02 '22 at 03:38
  • 7
    Doesn't work for me. Perhaps you meant `arm64` or this solution works for specific images only. – Mikhail_Sam Jan 17 '22 at 09:51
  • This worked for me with pydicom/dicom image. Thank you! – Jay Jan 28 '22 at 03:38
  • 1
    Did not work for me. Used following command on M1: docker run --platform linux/amd64 -p 4000:4000 in28min/hello-world-python:0.0.1.RELEASE – Hiren Feb 10 '22 at 17:51
  • 1
    This example is just for the node image and is run to an `amd64` image on `arm64` hardware. Not all images appear to support this. – Ryan Mar 20 '22 at 21:44
  • I don't this works anymore this is 2023 already outdated answer – user1034912 Mar 08 '23 at 06:10
  • It still works fine. `❯ docker run --platform linux/amd64 node --version` `v19.7.0` – Ryan Mar 15 '23 at 09:41
23

With docker-compose you also have the platform option.

version: "2.4"
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:7.1.1
    hostname: zookeeper
    container_name: zookeeper
    platform: linux/amd64
    ports:
      - "2181:2181"
Ryan
  • 4,594
  • 1
  • 32
  • 35
7

We were facing this issue with the localstack image.

Below is the snippet from the docker-compose.yml

localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack:1.2.0
    ports:
      - "4566:4566"
    environment:
      - DOCKER_HOST=unix:///var/run/docker.sock

One of the developers working with M1 chipset on Mac was getting this issue.

So there are few approaches

  1. Add platform: linux/amd64 in the image declaration in docker-compose.yml
  2. Run this command export DOCKER_DEFAULT_PLATFORM=linux/amd64 before running the docker-compose.yml
  3. Best is to refer the architecture specific image. For e.g. in our case, we used image: localstack/localstack:1.2.0-amd64

Reference https://hub.docker.com/layers/localstack/localstack/1.2.0-amd64/images/sha256-474600686aa98e8c79df96a9e7e5f689487c3a624ba8e464a0b6c3f45985cbcd?context=explore

Sanjay Bharwani
  • 3,317
  • 34
  • 31
4

Build the image by passing the list of architecture

Try this:

docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t username/demo:latest --push .

Note: ensure to place "." at the end

2

You should have the docker buildx installed. If you don't have the docker-desktop you can download the binary buildx from github: https://github.com/docker/buildx/

After installation you can build your image like Theofilos Papapanagiotou said

<downloaded_path>/buildx --platform linux/amd64 ...

1

Use colima. then you'll be able to run x86/64 only images on your M1/M2 Mac as well

brew install colima
colima start --memory 4 --arch x86_64
docker run [image name you want to run]

That's it. it's quite simple. https://github.com/abiosoft/colima

Shane Park
  • 81
  • 4
  • 1
    Looks rather messy. What does it do behind the scene? – user1034912 Mar 08 '23 at 06:14
  • 1
    it's just a lightweight container runtime like Docker Desktop. You can check out if you're interested https://github.com/abiosoft/colima btw, Docker Desktop team is trying to add new feat which is using Rosetta for old arch images, it doesn't work well yet but we'll be able to run x86/64 images soon i reckon – Shane Park Mar 09 '23 at 05:56
-1

You might need to run

docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

in order to register foreign file formats with the kernel.

  • Tried this one at M1 but it doesn't work for me :( – Mikhail_Sam Jan 17 '22 at 09:55
  • 1
    Maybe this does not apply for M1. I based this on the steps needed for x86, see here https://www.stereolabs.com/docs/docker/building-arm-container-on-x86/ There, there is the pre-requisite of installing `qemu binfmt-support qemu-user-static` – Thor Tomasarson Jan 17 '22 at 10:24