Docker was supposed to kill "works on my machine". Now it works on my container.
Bon. Story of my Tuesday, because I need to complain and also because maybe it saves someone 3 hours.
New colleague, new MacBook with an M3. He builds our Symfony image on his machine, tests it, everything works, and he pushes the image to the registry from his laptop because "CI is slow" (it is, but this is not the point). Deploy on our servers. Container starts and dies directly:
exec /usr/local/bin/docker-php-entrypoint: exec format error
The famous "it works on my machine", but now with extra steps and a registry. His Mac is arm64. Our servers are amd64. Docker on the Mac built an arm64 image by default, of course, and the server cannot execute it. Nobody told the image it was going to production.
Three solutions, from "ok" to "how it should be since the beginning":
- Force the platform when you build locally.
docker build --platform linux/amd64 -t app:dev .
It works, it uses emulation, it is slow on the Mac. Ok for testing, not a process. - Build multi-arch with buildx, so the same tag works on Macs and on servers:
docker buildx build --platform linux/amd64,linux/arm64 -t registry.example.com/app:1.4.2 --push .
The registry stores a manifest list and each machine pulls its own architecture. - Never push to the registry from a laptop. Only CI pushes. CI builds what goes to production. Laptops build for laptops.
Also check your FROM. Some base images exist only for amd64, Docker on the Mac will silently emulate them, it will be slow, and then people say "Docker is slow on Mac". No. Your image is wrong.
We did 2 and 3, and I added this to the Dockerfile so the build log says loudly what it is building:
ARG TARGETPLATFORM
RUN echo "building for $TARGETPLATFORM"
Bref. Docker solved "works on my machine" the same way the RER solves transport: in theory.
Get the best of #works-on-my-machine weekly
One email each Monday with the most useful posts. No spam; unsubscribe in one click.
Comments
Sign in to join the conversation.
Sign in to comment