error docker

Docker Exit Code 139 - Segmentation Fault (SIGSEGV)

Understanding Docker exit code 139 - the container process crashed with a segmentation fault due to invalid memory access.

What It Means

Docker exit code 139 (128 + 11) indicates that the container’s main process received SIGSEGV (segmentation fault). This means the program tried to access a memory address it was not allowed to access. This is a serious error that usually points to bugs in native code, incompatible libraries, or architecture mismatches.

Common Causes

  • Native code bug (null pointer dereference, buffer overflow)
  • Incompatible native libraries or modules
  • Architecture mismatch (running amd64 image on ARM or vice versa)
  • Corrupted binary files in the image
  • Incompatible glibc or musl versions (alpine vs debian)
  • Faulty native Node.js addons or Python C extensions
  • Running an image built for a different platform without QEMU emulation

How to Fix

Check for architecture mismatch

# Check what platform the image was built for
docker inspect my-image --format='{{.Architecture}}'

# Check your host architecture
uname -m

# Pull the correct architecture
docker pull --platform linux/amd64 my-image
docker pull --platform linux/arm64 my-image

# Build for the correct platform
docker build --platform linux/amd64 -t my-image .

Rebuild native dependencies

# Ensure native modules are compiled for the container OS
FROM node:20-alpine

# Install build tools for native modules
RUN apk add --no-cache python3 make g++

WORKDIR /app
COPY package*.json ./
RUN npm ci  # Compiles native modules for this platform

COPY . .
CMD ["node", "app.js"]

Fix musl vs glibc issues

# If your binary was compiled with glibc but you're using alpine (musl):

# Option 1: Switch to a glibc-based image
FROM node:20-slim  # Debian-based, uses glibc

# Option 2: Install glibc compatibility on alpine
FROM alpine:3.19
RUN apk add --no-cache libc6-compat

Debug the segfault

# Get a core dump from the container
docker run --ulimit core=-1 --privileged my-image

# Run with strace to trace system calls
docker run --cap-add SYS_PTRACE my-image strace -f -o /tmp/trace node app.js

# Use GDB inside the container
docker run -it --cap-add SYS_PTRACE my-image
# Inside: gdb -ex run -ex bt --args node app.js

Fix Python C extension issues

FROM python:3.12-slim

# Install required system libraries
RUN apt-get update && apt-get install -y \
    libpq-dev \
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Reinstall packages with C extensions
RUN pip install --no-cache-dir --force-reinstall numpy pandas

Fix Node.js native addon issues

# Rebuild native addons inside the container
docker run -it my-image npm rebuild

# Or clean install
docker run -it my-image sh -c "rm -rf node_modules && npm install"