error docker

Docker Exit Code 1 - General Error

Understanding Docker exit code 1 - the container's main process failed with a general error, such as an application crash or runtime exception.

What It Means

Docker exit code 1 indicates that the container’s main process failed with a general error. This is the most common non-zero exit code and typically means the application inside the container crashed, threw an unhandled exception, or encountered a runtime error.

Common Causes

  • Application threw an unhandled exception or error
  • Missing configuration file or environment variable
  • Failed to connect to a database or external service
  • Syntax error in the application code
  • Missing dependencies or packages
  • Invalid command or arguments in CMD/ENTRYPOINT
  • Entrypoint script failed

How to Fix

Check container logs

# View the container's stdout/stderr output
docker logs <container-id>
docker logs --tail 50 <container-id>

# Follow logs in real-time
docker logs -f <container-id>

# If the container has already been removed
docker run --name debug my-image
docker logs debug

Debug interactively

# Override entrypoint to get a shell
docker run -it --entrypoint /bin/sh my-image

# Or use bash if available
docker run -it --entrypoint /bin/bash my-image

# Check if files exist, env vars are set, etc.
ls -la /app/
env | grep DATABASE
cat /app/config.json

Fix missing environment variables

# Check what environment the container sees
docker run --rm my-image env

# Pass required environment variables
docker run -e DATABASE_URL=postgres://localhost/db \
           -e API_KEY=xxx \
           my-image
# Docker Compose
services:
  app:
    image: my-image
    environment:
      - DATABASE_URL=postgres://db:5432/mydb
      - NODE_ENV=production
    env_file:
      - .env

Fix dependency issues

# Ensure all dependencies are installed
FROM node:20-alpine

WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production  # Use ci for deterministic installs

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

Fix entrypoint script errors

#!/bin/bash
set -e  # Exit on error

# Common issue: entrypoint.sh has Windows line endings
# Fix: convert to Unix line endings
# dos2unix entrypoint.sh

# Wait for dependencies to be ready
until pg_isready -h db -U postgres; do
  echo "Waiting for database..."
  sleep 2
done

exec "$@"  # Execute the CMD

Add health checks

HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
  CMD curl -f http://localhost:3000/health || exit 1