qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2'
Problem Statement
When building Docker images on Apple Silicon (M1/M2) Macs, you may encounter the error:
qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2': No such file or directory
This occurs because Docker on Apple Silicon Macs defaults to using ARM64 architecture containers, but your Dockerfile is trying to run x86_64 (AMD64) binaries that require the x86_64 dynamic linker/loader (ld-linux-x86-64.so.2
), which is not present in ARM64 base images.
Solutions
There are several approaches to resolve this issue, depending on your specific needs.
Option 1: Build with explicit platform flag (Recommended)
The simplest solution is to specify the target platform during the build command:
docker build --platform linux/amd64 -t te-grafana-dashboards-toolchain --no-cache .
This ensures Docker builds an x86_64 container from the start, providing the necessary x86_64 libraries and avoiding emulation issues.
Option 2: Modify Dockerfile with platform specification
Add platform specification to your FROM
statement:
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
Option 3: Use environment variable for default platform
Set a global environment variable to default to x86_64 architecture:
export DOCKER_DEFAULT_PLATFORM=linux/amd64
docker build -t te-grafana-dashboards-toolchain --no-cache .
Option 4: Use multi-architecture approach with TARGETARCH
For a more flexible Dockerfile that works on multiple architectures:
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
Build with explicit architecture:
docker build --platform linux/amd64 -t te-grafana-dashboards-toolchain --no-cache .
Explanation
Apple Silicon Architecture
Apple Silicon Macs use ARM64 architecture, while many Docker images and binaries are built for x86_64 architecture. Docker Desktop for Mac uses QEMU emulation to run x86_64 containers on ARM64 hardware, but this requires the appropriate x86_64 libraries to be present in the container.
The error occurs because:
- Without explicit platform specification, Docker uses ARM64 base images on Apple Silicon
- x86_64 binaries (like the Go toolchain in your Dockerfile) require the x86_64 dynamic linker
- ARM64 images don't contain x86_64 libraries, causing the missing file error
Performance Consideration
Using --platform=linux/amd64
on Apple Silicon will use QEMU emulation, which may result in slower build times compared to native ARM64 builds. For optimal performance, consider using ARM64 base images and ARM64 binaries when possible.
Best Practices
- Explicit platform specification: Always specify your target platform explicitly in either the Dockerfile or build command
- Multi-architecture images: When possible, create Dockerfiles that support both ARM64 and x86_64 architectures
- Image verification: Use
docker image inspect [image]
to check the architecture of your built images - Base image selection: Choose base images that offer multi-architecture support
Troubleshooting
If you continue to experience issues:
- Verify Docker Desktop is updated to the latest version
- Check that virtualization is enabled in Docker Desktop settings
- Ensure you're using compatible base image tags that support the target architecture
By following these approaches, you can successfully build and run x86_64 Docker containers on your Apple Silicon Mac while maintaining compatibility with existing toolchains and dependencies.