top of page

Docker Container Exits Immediately

You run the container and it stops within seconds. Docker shows Exited, Compose keeps restarting it, and the logs are empty or unhelpful. It might work on your laptop and fail on the server. A Codersarts DevOps engineer finds why the container won't stay up and gets your service running.

A Docker container stops when its main process ends, so an immediate exit means the command finished, crashed, couldn't start, or was killed. The exit code is the fastest clue: 0 usually means the process finished or went to the background, 1 means the application errored, 126 and 127 point to permission or command-not-found problems, and 137 means the container was killed, often for running out of memory. Architecture mismatches and dependencies that aren't ready also cause instant exits.



Typical symptoms

Exited (0), Exited (1), 127 or 137 exit codes, restart loops, exec format errors, works locally but not on the server

Most common causes

Process running in the background, application startup errors, wrong entrypoint, missing variables, image architecture mismatch

How we fix it

Read the exit code and logs, start the container interactively, fix the command, configuration, or image, verify it stays healthy

Turnaround

Same-day diagnosis; most fixes in 24–48 hours

Price

Live Debug from $20; fixed-price quote for the full fix



Signs Your Container Has This Problem

  • docker ps -a shows Exited (0) or Exited (1) seconds after starting

  • Docker Compose shows the service restarting in a loop

  • Logs show exec format error

  • Logs show no such file or directory for a script that exists

  • Exit code 137 with no application error

  • The app container exits because the database isn't ready yet

  • The image works on your laptop but fails on the server



Why Docker Containers Exit Immediately


A container runs one main process. When that process stops for any reason, the container stops with it. The work is finding out why the process stopped.


1. The main process ends or goes to the background

If the command runs a script that finishes, or starts a service in background mode, Docker sees the main process exit and stops the container with code 0. Servers need to run in the foreground.


2. The application fails on startup

A missing environment variable, a configuration error, or a failed connection to a database or API makes the application exit with an error, usually code 1.


3. The command or script can't run

A wrong entrypoint path, a binary not installed in the image, a script without execute permission, or Windows line endings in a shell script produce codes 126 or 127 and misleading “no such file” errors.


4. The image was built for a different CPU architecture

Images built on Apple Silicon default to ARM. Running them on a typical x86 cloud server causes exec format error.


5. Dependencies aren't ready or memory runs out

Compose's basic depends_on starts containers in order but doesn't wait for a database to be ready, so the app exits on its first connection attempt. Memory limits can also kill the process with code 137.



How We Diagnose the Exiting Container

  1. Read the exit code. Use the container status and inspect output, including whether it was killed for memory.

  2. Read the logs. Check container logs from the failed run, including earlier restarts.

  3. Start the container interactively. Override the entrypoint with a shell and run the command manually to see the real error.

  4. Check the image architecture. Compare the image's platform with the server's CPU architecture.

  5. Review Dockerfile and Compose files. Check the command form, script permissions, line endings, environment variables, and dependencies.

  6. Test dependency readiness. Confirm databases and services are ready before the app connects.



How We Fix It

Root cause

Fix

Process in the background

Run the service in the foreground and use the exec form of the start command

Startup errors

Supply required environment variables and fix configuration or connection failures

Command can't run

Correct entrypoint paths, install missing binaries, set execute permissions, and convert line endings

Architecture mismatch

Build the image for the server's platform or publish a multi-platform image

Dependencies not ready

Add health checks and wait for healthy dependencies, plus retry logic in the app

Out of memory

Right-size memory limits and reduce the application's memory use


We verify the container stays healthy through restarts and a fresh deployment, not just a single successful start.



Example Fix


Situation: A startup's Node.js API image ran on the developer's MacBook but exited instantly on their cloud server, and Docker Compose restarted it endlessly.


Cause: The image was built on Apple Silicon for ARM, while the server used x86, causing exec format errors. After rebuilding, the API still exited because it tried to connect to PostgreSQL before the database was ready.


Fix: Rebuilt the image for the server's platform in CI, added a database health check with the app waiting for a healthy database, and added connection retries in the API.


Result: The stack started reliably on every deploy and server reboot.



How to Keep It From Happening Again

  • Build images in CI for the target platform instead of from developer machines.

  • Add health checks for every service that other containers depend on.

  • Test containers with production-like variables before deploying.



What You Get

  • Root cause confirmed and explained

  • Container running and healthy after restarts

  • Corrected Dockerfile and Compose configuration

  • Notes on build and deploy practices





Frequently Asked Questions


Why does my Docker container exit immediately? 

The main process stopped. It either finished, moved to the background, crashed on startup, couldn't run the command, or was killed. The exit code and logs show which.


What does Docker exit code 0 mean if the container stopped? 

The process ended without an error. This usually happens when the command finishes or a server starts in background mode instead of the foreground.


What does exec format error mean in Docker? 

The image was built for a different CPU architecture than the machine running it, commonly an ARM image on an x86 server, or a script is missing its interpreter line.


Why does my container restart in a loop? 

A restart policy keeps restarting a process that fails each time. Fixing the underlying startup error, dependency, or memory problem stops the loop.


Does depends_on wait for the database to be ready? 

Not by default. It controls start order only. Use health checks with a healthy condition and add retry logic in the application.



Related Problems

  • Kubernetes pod CrashLoopBackOff

  • Django 502 Bad Gateway

  • Node.js app crashing in production

  • AWS EC2 app not accessible

  • GitHub Actions pipeline failing



Get Your Container Running

Share the exit code and container logs. Get a diagnosis and a fixed price.


Get Help Now





bottom of page