Blog

Segmentation fault + docker + gdb

The following scenario happened to me twice after upgrading the Symfony version. The container with the application would crash with a "Segmentation fault" error.
This means the process tried to access memory it didn’t have access to. It’s not a logical error but something more serious—a system-level or core issue—and it’s tough to diagnose.
The difficulty lies in the fact that you can’t catch it with an exception and check the backtrace.
If your application is also running in a Docker container, the complexity increases.

This post is a note for the future in case a similar situation arises again. The assumption of this post is that you’re using a Linux-based container.
To solve the problem, use the gdb command.

gdb stands for GNU Debugger. You can find installation instructions online.

First, add it to the system or install it temporarily in the Dockerfile and rebuild the container.
Second, copy the runtime file.

cp index.php test.php
echo '<?php sleep(3600) > index.php'

This ensures the container starts correctly.
Third, access the container using the exec command.

gdb php test.php
// or standalone script
gdb php bin/console app:troubles

In gdb, use the following commands:

  • bt (backtrace) – displays the stack,
  • list – shows code fragments around the location where the error occurred.

Now, go to your favorite IDE and set a breakpoint just before that spot. It’s highly likely that, upon rerunning, you’ll get some trace.
For example, an exception after catching an exception.