Skip to content

16. Docker Troubleshooting

If you run docker run or docker compose up and a container immediately exits, follow this diagnostic path:

First, find out how it died. Run docker ps -a to see stopped containers. Look closely at the Status column for the exit code.

  • Exit 1: Application error (code threw an exception, missing dependency).
  • Exit 137: OOMKilled (Out of Memory) or killed via SIGKILL.
  • Exit 143: Gracefully terminated via SIGTERM.

Fetch the error output. Run docker logs <container_name>. If the container prints nothing, it means the application crashed before it could even initialize its logging framework, or it’s improperly writing to a local file instead of stdout.

If logs provide no clues, run docker inspect <container_name>. Look at the "State" block. It will tell you the exact time of death, the OOMKilled status, and the exact Error string thrown by the Docker daemon itself (e.g., missing volume mount paths).

The classic microservice problem. The web app is running, but it cannot connect to Postgres/MySQL.

  • Network: Are both containers on the exact same custom Docker bridge network? Check using docker network inspect <network_name>.
  • DNS: Is the web app trying to connect to localhost? localhost inside a container points to the container itself, not the host machine or the database container! It must use the database container’s name (e.g., db) as the hostname.
  • Port: Is the connection string using the correct internal port? (e.g., Postgres uses 5432, MySQL uses 3306).
  • Credentials: Double-check your .env file. Did you pass the correct POSTGRES_USER and POSTGRES_PASSWORD environment variables to both containers?
  • Database Status: Is the database actually ready? Run docker logs db. Massive databases take 10-30 seconds to initialize their data directories on first boot. If the web app tries to connect during this time, it will fail. (Fix: Implement a depends_on: condition: service_healthy check in Compose).
  • Firewall: If the app and DB are on different machines, check UFW or AWS Security Groups blocking the published port.

If a container is stuck in a “CrashLoopBackOff” (constantly restarting every few seconds):

  • Exit Code & Logs: Run docker logs -f <container_name>. You must catch the exact error thrown right before it crashes.
  • Entrypoint: Did you override the ENTRYPOINT in Compose? Ensure the script you are calling actually exists and has executable permissions (chmod +x).
  • Healthcheck: If a container has a strict healthcheck configured, and the app takes too long to boot, the healthcheck will fail and Docker will ruthlessly kill and restart the container. Try increasing the start_period or timeout.
  • Restart Policy: If you are actively trying to debug a broken container, temporarily remove restart: always from your compose file. Otherwise, the container will restart so fast you won’t have time to run docker exec to investigate.

You try to start a container, but you cannot reach it in your browser.

  • Port Already in Use: If you see Bind for 0.0.0.0:80 failed: port is already allocated, another process on your host machine (or another container) is already using port 80. You must map it to a different host port (e.g., -p 8080:80).
  • Wrong Port Mapping: Did you write -p 80:8080 instead of -p 8080:80? The format is always HOST_PORT : CONTAINER_PORT.
  • Service Listening on Wrong Interface: If your NodeJS app is configured to listen on 127.0.0.1 inside the container, it will drop all external traffic. The application code must be configured to listen on 0.0.0.0 to receive traffic routed by Docker.
  • Firewall: If you can curl the port locally on the host (curl localhost:8080), but cannot reach it from the internet, your cloud provider’s firewall (AWS Security Group, GCP Firewall) is blocking external access to that port.

Volumes and Bind Mounts are common failure points.

  • Permission Denied: You bind mount a host directory, but the container crashes with a write error. This happens because the Linux user running inside the container (e.g., node UID 1000) does not have write permissions to the folder on the host machine. (Fix: Run chown on the host directory, or switch to Docker Volumes).
  • Volume Missing: Did you declare a named volume in a Compose service, but forget to declare it in the global volumes: block at the bottom of the YAML file?
  • Disk Full: If docker logs abruptly stop, or containers randomly fail to start with no space left on device, run docker system df. Prune old images and volumes to free up space.
  • Wrong Mount Path: Ensure the path you are mounting into the container matches the exact path the application expects to read from (e.g., mapping a config file to /app/config.json when the app expects it at /etc/config.json).

If docker build or docker pull fails:

  • Build Failure: Read the build output carefully. Which specific step (layer) failed? Usually, this is caused by a missing dependency, a typo in a RUN command, or a broken package repository (like apt-get failing due to network issues).
  • Cache Problems: Did you update a package or source code, but the build isn’t reflecting it? Docker might be aggressively reusing a cached layer. Force a clean build with docker build --no-cache ..
  • Missing Files: If a COPY instruction fails with file not found, ensure the file isn’t accidentally listed in your .dockerignore file, and verify your build context (. usually).
  • Wrong Architecture: If you develop on an Apple Silicon Mac (ARM64) and build an image without specifying the platform, it will crash violently when you deploy it to a standard AWS/Linux server (AMD64) with an exec format error. (Fix: Build cross-platform images using docker buildx build --platform linux/amd64).