Cover image for A Simple Docker Compose Trick for Debugging Containers Stuck in a Restart Loop

Nurul Islam Rimon

When a Docker container is continuously restarting, you can't use docker exec because the container never stays in a running state long enough.

docker exec -it myapp sh

Enter fullscreen mode Exit fullscreen mode

You'll typically get:

Error response from daemon: Container <id> is restarting, wait until the container is running

Enter fullscreen mode Exit fullscreen mode

The Solution

Start a temporary container using the same Docker Compose service, but override its entrypoint with a shell.

docker compose run --rm --entrypoint sh <service-name>

Enter fullscreen mode Exit fullscreen mode

Example:

docker compose run --rm --entrypoint sh rusikapi

Enter fullscreen mode Exit fullscreen mode

Why It Works

This command creates a new container using the same:

  • Image
  • Environment variables
  • Volumes
  • Networks
  • Service configuration

Instead of starting your application, it opens an interactive shell, allowing you to investigate the environment before anything crashes.

Common Debugging Tasks

Once inside the container, you can:

# Verify environment variables
printenv

# Inspect application files
ls -la

# Run Prisma migrations manually
npx prisma migrate deploy

# Start the application
npm run start

Enter fullscreen mode Exit fullscreen mode

This makes it much easier to identify startup failures, database connectivity issues, or configuration problems.

Takeaway

docker compose run --rm --entrypoint sh is a simple but effective debugging technique that every Docker Compose user should know.

docker compose run --rm --entrypoint sh <service-name>

Enter fullscreen mode Exit fullscreen mode