87

I have a Rancher Deskop(dockerd) on M1 MacOS and when I am trying to build below dockerfile I am getting an error such as below. Here is the command how I am trying to build the image docker build -t te-grafana-dashboards-toolchain --no-cache .

I tried to change the platforms but nonae of them worked for me. I am a bit lost about this platform issue for M1 but any help will be appreciated, What I am doing wrong? What might the root cause of this?

Removing intermediate container 70af516d5d6b
 ---> a69229847153
Step 5/6 : RUN GO111MODULE="on" go get github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb; ln -s $(go env GOPATH)/bin/jb /usr/bin/jb
 ---> Running in 13545862fffe
qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2': No such file or directory
qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2': No such file or directory
Removing intermediate container 13545862fffe

Dockerfile

FROM --platform=linux/amd64 ubuntu:focal
RUN apt update; apt install -y curl jq build-essential python3.8 python3-pip docker-compose jsonnet bison mercurial
RUN ln -s /usr/bin/python3.8 /usr/bin/python
RUN curl -OL https://golang.org/dl/go1.17.linux-amd64.tar.gz; mkdir /etc/golang; tar -xvzf go1.17.linux-amd64.tar.gz -C /etc/golang; ln -s /etc/golang/go/bin/go /usr/bin/go; rm -f go1.17.linux-amd64.tar.gz
RUN GO111MODULE="on" go get github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb; ln -s $(go env GOPATH)/bin/jb /usr/bin/jb
WORKDIR /workspace
semural
  • 3,583
  • 8
  • 37
  • 67

7 Answers7

170

Incidentally, in case it's helpful to another who lands here, I have the same issue on an M1 Max MacBook Pro laptop attempting to do a docker build from a company repo that should be a pretty well traveled path, but I might be the only one (it's a small company) that has an ARM64 M1 "Apple Silicon" Mac. However I found the solution (well, a solution) to my situation was exactly the opposite of the solution to the OP's, and that was to add --platform=linux/amd64 to the FROM line of the docker file.

Example:

FROM --platform=linux/amd64 ubuntu:20.04

Otherwise it was using an ARM64 image to start from without me being the wiser but then later in the Dockerfile the build attempts to install and execute code compiled for x86_64. Starting the build process by requesting the base image be linux/amd64 ends up with then the base image having /lib64/ld-linux-x86-64.so.2. This probably means everything is being emulated as x86_64 on the ARM64 CPU via qemu-x86_64 and so if you have the option to start from an ARM64 image and can compile within the container during build time any software you can't install as ARM64 binaries, it'll probably go faster when you later run the container on the M1 based Mac. I'm not able to try that myself just yet for this case.

stevec
  • 41,291
  • 27
  • 223
  • 311
slowkoni
  • 2,146
  • 1
  • 14
  • 7
  • 9
    Exactly same problem and with `docker run` adding a `--platform linux/amd64` solved it. I was installing arm64 go and it couldn't run. Thanks. – Pablo LION Mar 26 '22 at 21:25
  • 8
    And if you use docker-compose, you can set `services.service_name.platform` to be `"linux/amd64"`. Taken from https://stackoverflow.com/questions/68434301/docker-compose-run-with-specified-platform – Ben Butterworth Apr 01 '22 at 11:00
  • @slowkoni, I hope you can still help out. But what if in my case is not a docker file? I'm trying to install golang on a centOS8 docker container, and I followed the steps in here: https://linuxize.com/post/how-to-install-go-on-centos-8/ But when I get to 'go build', I get that error... – TiagoRibeiro Apr 23 '22 at 20:47
  • @BenButterworth setting on docker-compose file works fine! thanks! – Diogo Alves May 23 '22 at 15:42
  • 5
    Thanks. This format worked: `FROM --platform=linux/amd64 ubuntu:20.04 as image_name` – komodovaran_ Jul 04 '22 at 11:07
  • Thank you. `services.service_name.platform`: `"linux/amd64"` worked – Kouji Kawasaki Oct 06 '22 at 03:00
  • Another way to use this without modifying the Dockerfile : `docker image build --platform=linux/amd64 -t IMAGE_NAME . ` Note that this works with docker alternatives like podman too. – Rohit Nov 10 '22 at 18:32
  • It is causing qemu: uncaught target signal 11 (Segmentation fault) - core dumped – bonijad383 Jan 02 '23 at 14:16
  • On Mac M1, for Ubuntu 22.04/Golang 1.20.1, `--platform=linux/amd64` is required. for some of the other combinations, it seemed fine without specifying the platform. – Qiang Li Mar 06 '23 at 23:22
  • Note that it's also possible to use the same argument in the docker command itself to specify the [target platform](https://docs.docker.com/build/building/multi-platform/#building-multi-platform-images). Example: `docker build --platform=linux/amd64` ([source](https://stackoverflow.com/questions/68630526/lib64-ld-linux-x86-64-so-2-no-such-file-or-directory-error/69075554#comment123798591_69075554)). – stevec Apr 01 '23 at 01:36
20

Modifying Dockerfile seems to be the most popular answer but you can also set the DOCKER_DEFAULT_PLATFORM environment variable to linux/amd64.

export DOCKER_DEFAULT_PLATFORM=linux/amd64

The cause seems to reside in the AArch64 image.

Curious Sam
  • 884
  • 10
  • 12
  • this would be my preferred, but when set that variable (on host system), docker run can no longer find any of my images. solution to that? – tef2128 Jul 07 '23 at 17:35
6

Instead of editing the Dockerfile, as suggested in this answer, or setting an environment variable, as suggested in this answer, I prefer to pass the platform as an argument to the docker build command, with the --platform flag. The command used by the OP would then be:

docker build --platform linux/amd64 -t te-grafana-dashboards-toolchain --no-cache .
ccoutinho
  • 3,308
  • 5
  • 39
  • 47
2

this resolved my issue.

FROM ubuntu:focal
RUN apt update; apt install -y curl jq build-essential python3.8 python3-pip docker-compose jsonnet bison mercurial
RUN ln -s /usr/bin/python3.8 /usr/bin/python
RUN curl -OL https://golang.org/dl/go1.17.linux-arm64.tar.gz; mkdir /etc/golang; tar -xvzf go1.17.linux-arm64.tar.gz -C /etc/golang; ln -s /etc/golang/go/bin/go /usr/bin/go; rm -f go1.17.linux-arm64.tar.gz
RUN GO111MODULE="on" go get github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@latest; ln -s /root/go/bin/jb /usr/bin/jb
WORKDIR /workspace
larsks
  • 277,717
  • 41
  • 399
  • 399
semural
  • 3,583
  • 8
  • 37
  • 67
2

Passing following flag to C preprocessor as CPPFLAGS solved similar issue in my M1

-DPNG_ARM_NEON_OPT=0

Pass the value as env var with key CPPFLAGS to relevant service.

Hemel
  • 86
  • 1
  • 5
  • The needed `docker run` flag is `-e CPPFLAGS=-DPNG_ARM_NEON_OPT=0`. Without it, the error I get is `npm ERR! code 1 npm ERR! path /build/explorer/node_modules/optipng-bin npm ERR! command failed npm ERR! command sh -c node lib/install.js npm ERR! compiling from source npm ERR! Command failed: /build/explorer/node_modules/optipng-bin/vendor/optipng --version npm ERR! qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2': No such file or directory npm ERR! npm ERR! npm ERR! optipng pre-build test failed npm ERR! Error: Command failed: /bin/sh -c make install ` – Hannes Apr 15 '23 at 00:11
2

Provided the base image includes the target architecture, another option that might work in your case is using Docker's built-in TARGETARCH build arg. This works for me on macOS M1.

FROM ubuntu:focal
ARG TARGETARCH
RUN apt update; apt install -y curl jq build-essential python3.8 python3-pip docker-compose jsonnet bison mercurial
RUN ln -s /usr/bin/python3.8 /usr/bin/python
RUN curl -OL https://golang.org/dl/go1.17.linux-${TARGETARCH}.tar.gz; mkdir /etc/golang; tar -xvzf go1.17.linux-${TARGETARCH}.tar.gz -C /etc/golang; ln -s /etc/golang/go/bin/go /usr/bin/go; rm -f go1.17.linux-${TARGETARCH}.tar.gz
RUN GO111MODULE="on" go get github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb; ln -s $(go env GOPATH)/bin/jb /usr/bin/jb
WORKDIR /workspace
guicassolato
  • 411
  • 1
  • 4
  • 10
  • This should be the first method to try. Most of other answers rely on x86 emulation, which does come with performance overhead and potential bug. This works perfectly in my case. – Choznerol May 22 '23 at 03:54
1

I have a Mac M1 Max computer and encountered an error when attempting to install Camunda Platform 8 using Helm on a Kubernetes cluster running on a KIND node. The error occurred in the Camunda Operate pod. However, after changing the default image using the '--platform linux/amd64' option, I was able to start the container successfully.

Specifically, I ran the following commands:

docker pull camunda/operate:8.1.6 --platform linux/amd64
kind load docker-image --name camunda-platform-local camunda/operate:8.1.6
Toni
  • 3,296
  • 2
  • 13
  • 34