Containers

You’re a DevOps engineer deploying a Node.js application using Docker. You run docker run -d -p 3000:3000 my-node-app, but the container exits immediately. Using docker ps -a, you see the container status as Exited. How would you troubleshoot and resolve this issue?

Difficulty: unrated

Source: bregman-arie/devops-exercises by Arie Bregman

Answer

To troubleshoot a container exiting immediately:

  1. Check Logs: Run docker logs my-node-app to view error messages. Common issues include missing dependencies (e.g., npm install failed) or an incorrect command.
  2. Inspect the Container: Use docker inspect my-node-app to check Config.Cmd or Config.Entrypoint. Ensure the command (e.g., node app.js) is valid.
  3. Verify the Dockerfile: Check if CMD or ENTRYPOINT is correct, e.g., CMD ["node", "app.js"]. Update and rebuild if needed: docker build -t my-node-app ..
  4. Test Interactively: Run docker run -it my-node-app sh to debug manually (e.g., node app.js).
  5. Check Resources: Ensure the host has enough memory/CPU using docker stats.

Example Fix: If logs show node: command not found, update the Dockerfile to use FROM node:18, rebuild, and rerun.