Skip to content

How to Fix 'Docker Container Exited with Code 1'

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix 'Docker Container Exited with Code 1. We cover key concepts, practical examples, and best practices.

The Problem

You run a Docker container and it exits immediately:

docker run my-app

The container stops with:

docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS     NAMES
a1b2c3d4e5f6   my-app    "node server.js"         5 seconds ago   Exited (1) 3 seconds ago             angry_curie

Exit code 1 means the main process inside the container failed. No network service, no output -- just a dead container.

Quick Fix

1. Check the logs first

Always start by reading the container logs:

docker logs <container-name-or-id>
docker logs angry_curie

If the container was removed, try:

docker logs --tail 50 a1b2c3d4e5f6

Logs often show the exact error -- a missing file, a failed import, or a misconfigured environment variable.

2. Run interactively to see the error

Drop into the container with an interactive shell to debug:

docker run -it --entrypoint /bin/sh my-app
# or for Alpine-based images:
docker run -it --entrypoint /bin/sh my-app

Once inside, manually run your application command:

node server.js

This bypasses any ENTRYPOINT or CMD issues and shows the raw error in your terminal.

3. Fix the entrypoint or command

Exit code 1 often means the ENTRYPOINT or CMD cannot find the file or binary. Check your Dockerfile:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --production
COPY . .
CMD ["node", "server.js"]  # <-- verify this file exists

Common entrypoint mistakes:

  • Path is relative instead of absolute: use WORKDIR /app then ./server.js
  • File lacks execute permission: run RUN chmod +x server.js
  • Wrong shebang line in a script entrypoint: use #!/bin/sh or #!/usr/bin/env node

4. Verify dependencies

If your app crashes at startup, a dependency might be missing. For Node.js:

docker run my-app npm ls

For Python:

docker run --entrypoint pip my-app list

If missing packages appear, rebuild with a fresh install:

docker build --no-cache -t my-app .

5. Check for port conflicts

If your app binds to a port that is already in use on the host, Docker exits the container. Run:

# Check what is using port 3000
sudo lsof -i :3000
# Or use ss
ss -tlnp | grep 3000

Map to a different port:

docker run -p 8080:3000 my-app

6. Use a healthcheck to prevent silent failures

Add a HEALTHCHECK to your Dockerfile so Docker restarts the container automatically:

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

Prevention

  • Always add a HEALTHCHECK to production containers.
  • Use docker build --no-cache after changing dependencies.
  • Test containers locally with docker run -it --entrypoint /bin/sh before deploying.
  • Add startup logging to your application so the first log line confirms the app started.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro