16. Docker Troubleshooting
16.1 Container Won’t Start
Section titled “16.1 Container Won’t Start”If you run docker run or docker compose up and a container immediately exits, follow this diagnostic path:
Step 1: docker ps -a
Section titled “Step 1: docker ps -a”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.
Step 2: docker logs
Section titled “Step 2: docker logs”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.
Step 3: docker inspect
Section titled “Step 3: docker inspect”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).
16.2 Application Can’t Reach Database
Section titled “16.2 Application Can’t Reach Database”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?localhostinside 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 uses3306). - Credentials: Double-check your
.envfile. Did you pass the correctPOSTGRES_USERandPOSTGRES_PASSWORDenvironment 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 adepends_on: condition: service_healthycheck in Compose). - Firewall: If the app and DB are on different machines, check UFW or AWS Security Groups blocking the published port.
16.3 Container Keeps Restarting
Section titled “16.3 Container Keeps Restarting”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
ENTRYPOINTin 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_periodortimeout. - Restart Policy: If you are actively trying to debug a broken container, temporarily remove
restart: alwaysfrom your compose file. Otherwise, the container will restart so fast you won’t have time to rundocker execto investigate.
16.4 Port Problems
Section titled “16.4 Port Problems”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:8080instead of-p 8080:80? The format is alwaysHOST_PORT : CONTAINER_PORT. - Service Listening on Wrong Interface: If your NodeJS app is configured to listen on
127.0.0.1inside the container, it will drop all external traffic. The application code must be configured to listen on0.0.0.0to 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.
16.5 Storage Problems
Section titled “16.5 Storage Problems”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.,
nodeUID 1000) does not have write permissions to the folder on the host machine. (Fix: Runchownon 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 logsabruptly stop, or containers randomly fail to start withno space left on device, rundocker 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.jsonwhen the app expects it at/etc/config.json).
16.6 Image Problems
Section titled “16.6 Image Problems”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
RUNcommand, or a broken package repository (likeapt-getfailing 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
COPYinstruction fails withfile not found, ensure the file isn’t accidentally listed in your.dockerignorefile, 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 usingdocker buildx build --platform linux/amd64).