selfie of Kenton

Kenton Vizdos

i make cool things in go. this is my dev log. note: it's not a technical masterpiece, I just like writing (sometimes poorly) about what I learn!

How to fix Docker BuildX lfstack push Error

When does this issue happen? #

When you build a Docker image with buildx for cross-compilation (e.g. M series Mac to AMD64), you may see the error:

 1fatal error: lfstack.push
 2
 3runtime stack:
 4runtime.throw({0x9b2656?, 0x100000000000000?})
 5	runtime/panic.go:1047 +0x5d fp=0xffff9d386638 sp=0xffff9d386608 pc=0x4357dd
 6runtime.(*lfstack).push(0x2?, 0xffff9d3866e8?)
 7	runtime/lfstack.go:29 +0x125 fp=0xffff9d386678 sp=0xffff9d386638 pc=0x40b4c5
 8runtime.(*spanSetBlockAlloc).free(...)
 9	runtime/mspanset.go:322
10runtime.(*spanSet).reset(0xd54ed0)
11	runtime/mspanset.go:264 +0x87 fp=0xffff9d3866a8 sp=0xffff9d386678 pc=0x42f747
12runtime.finishsweep_m()
13	runtime/mgcsweep.go:260 +0x9c fp=0xffff9d3866e8 sp=0xffff9d3866a8 pc=0x42377c
14runtime.gcStart.func1()
15	runtime/mgc.go:668 +0x17 fp=0xffff9d3866f8 sp=0xffff9d3866e8 pc=0x463397
16runtime.systemstack()
17	runtime/asm_amd64.s:496 +0x49 fp=0xffff9d386700 sp=0xffff9d3866f8 pc=0x467dc9

Why does this issue happen? #

This issue occurs because the image you imported using a FROM does not have the correct platform set. Therefor, any dependencies that get installed will be installed under an incorrect architecture. It's really weird that Docker doesn't do this for you, but the fix is easy.

Fixing fatal error: lfstack.push is simple. #

Each time you use FROM, be sure to include --platform=$BUILDPLATFORM. Docker automatically sets the environment variable, so no need to worry about setting things manually.

Full code example:

1FROM --platform=$BUILDPLATFORM node:lts-alpine AS node-builder

Note: You should set this on every Docker stage; not just a builder / dependency install stage.