WARNING: the requested image platform linux amd64 does not match does not match

Error “WARNING: the requested image platform linux amd64 does not match does not match the detected host platformlinux arm64 v8 and no specific platform was requested ”: How to solve it

When attempting to build a PHP image on MacOS, you might frequently run into the error: WARNING: The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64), and no specific platform was requested. This error message, which may appear in variations such as “the requested image platform linux amd64” or “warning image platform linux amd64 does not match the expected platform linux arm64” indicates a mismatch between the image and the host platform.

This error can be frustrating and impede the progress of your development. In this article, we’ll explore the causes of this error and how to resolve it by building the PHP image using Rosetta under the hood or from a Linux workstation instead of using MacOS.

the requested image platform linux amd64

Understanding the Error

Host Platform Detection

When you try to build an image in Docker, it checks the host platform (in the case of MacOS, linux/amd64) and the platform of the requested image (linux/arm64). The error “the requested image platform linux amd64 does not match the detected host platform” occurs when there is an incompatibility between these platforms.

The error occurs when using docker run, for example:

docker run --rm -p 9000:9000 1234567.dkr.ecr.us-east-1.amazonaws.com/devops-mind:php-1 
Example of the error:
WARNING: The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64) and no specific platform was requested
standard_init_linux.go:337: exec user process caused:  exec format error

The Role of Different Architectures

Different architectures, such as amd64 and arm64, represent different instruction sets that processors use. Newer Macs with Apple Silicon chips (M1 and M2) use the arm64 architecture, while many Containers and images are built for amd64. This incompatibility can cause the error mentioned.

Why MacOS Has This Problem

MacOS, especially on Apple Silicon machines, is not the most user-friendly platform for building Docker images that have not been optimized for arm64. The lack of native support can result in various problems, including the error the requested image platform linux amd64 / warning image platform linux amd64 does not match the expected platform linux arm64 and similar:

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

Solution1: Using Rosetta 2 to build the amd64 PHP Container on macOS ARM

To solve this incompatibility problem, you can use Rosetta 2, a technology developed by Apple that allows x86 architectures to be emulated on ARM-based systems. You can run Docker applications and images built for amd64 architectures on a macOS host with an ARM CPU.

Here are the steps to use Rosetta 2 to resolve the “exec format error” error:

  1. Install Rosetta 2: Rosetta 2 is usually installed automatically when you try to run an x86 application for the first time. You can check that Rosetta 2 is installed by running the command softwareupdate --install-rosetta in the terminal.
  2. Enable Rosetta 2: Make sure that Rosetta 2 is enabled and working correctly. You can check this by running a native x86 command, such as uname -m, which should return “x86_64” even on a macOS ARM host.
  3. Build the amd64 Docker image: With Rosetta 2 enabled, you should be able to build the amd64 Docker image without any problems. Use the docker build command to build the image, specifying the desired architecture with the --platform option.
docker build --platform linux/amd64 -t meu-container-php .

Run the Container amd64: You can now run the amd64 Container on your macOS ARM host without any problems, as Rosetta 2 will emulate your system’s x86_64 (amd64) architecture.

docker run -it meu-container-php

Rosetta 2 is an elegant and transparent solution for dealing with the architectural incompatibility between Docker images and macOS ARM hosts. It allows you to run amd64 applications and Containers on ARM systems without making changes to the code or the image.

Solution2: Building the Image from a Linux Station

Preparing the Linux environment

To avoid the error the requested image platform linux amd64 does not match, it is recommended to use a Linux workstation to build the PHP image. Linux has more robust support for different architectures, making compatibility easier.

  1. Initial Configuration:
  • Set up a Linux machine (Ubuntu is a great option) in a virtual machine or on dedicated hardware.
  • Make sure that Docker is installed and operating correctly. You can consult the official Docker documentation to carry out the configuration.
  1. Checking Docker Settings:
  • Run docker --version to make sure you have the correct version installed.
  • Configure Docker to support multiple platforms by adding the line {"experimental": true} in the file /etc/docker/daemon.json.

Building the PHP Image

With the Linux environment configured, you can proceed to build the PHP image.

  1. Create a Dockerfile:
  • Create a Dockerfile file with the following content:
FROM php:7.4-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./seu-script.php" ]
  • This basic Dockerfile will copy your PHP code into the image and configure the entry point.
  1. Build da Imagem:
  • In the terminal, navigate to the directory where your Dockerfile is located.
  • Execute o comando:
    sh docker build -t my-php-app .
  1. Image verification:
  • After the Build, check that the image was created correctly by running:
    sh docker images

Testing the Image

To ensure that the built image works correctly, run a Container from it:

  1. Running the Container:
  • Execute o comando:
    sh docker run -it --rm my-php-app
  • This command will start an interactive Container and remove the Container after execution.
  1. Checking Operation:
  • Make sure that your PHP script is working as expected. Any errors or messages will be displayed in the terminal.

Conclusion

The error “WARNING: The requested image’s platform (linux/arm64) does not match the detected host platform (linux/amd64) and no specific platform” is a common obstacle when building Docker images on MacOS, especially with the new Apple Silicon chips. However, by using Rosetta 2 or a Linux workstation to build your images, you can avoid this incompatibility and ensure that your applications work correctly and manages to avoid new occurrences of the requested image platform linux amd64 / warning image platform linux amd64 does not match the expected platform linux arm64.

This approach solves the immediate problem and prepares your development environment for future builds of Containers, ensuring greater compatibility and efficiency.

Want to learn more?

If you found this article useful, be sure to check out our other content on Docker and development in PHP. Keep up to date with best practices and tips for optimizing your development workflow.

Be sure to subscribe to our newsletter to receive the latest news and tips about DevOps/SRE directly to your email:

Receive news by email

* campo obrigatório

Also check out this video about the platform issue when using Docker:

Compartilhe / Share
Fernando Müller Junior
Fernando Müller Junior

I am Fernando Müller, a Tech Lead SRE with 16 years of experience in IT, I currently work at Appmax, a fintech located in Brazil. Passionate about working with Cloud Native architectures and applications, Open Source tools and everything that exists in the SRE world, always looking to develop and learn constantly (Lifelong learning), working on innovative projects!

Articles: 36

Receba as notícias por email / Receive news by email

Insira seu endereço de e-mail abaixo e assine nossa newsletter / Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *