CodePark/ questions / Healthchecks, restarts & logs
START PRACTICINGPRACTICE
Backend question · Docker › Healthchecks, restarts & logs

Your app works locally but the container exits or misbehaves. How do you debug it?

Check the container's state and logs first, then look inside it.

1. Check the state. Run docker ps -a. It shows if the container exited, and its exit code.

2. Read the logs. Run docker logs <container> to read what the app printed. Add -f to follow new output. Many problems show up right here:

  • a missing environment variable
  • a failed database connection
  • a "module not found" error

3. Look inside. If the container is running, open a shell in it with docker exec -it <container> sh. Then check:

  • that files are where you expect
  • the config, with env
  • that the app listens on 0.0.0.0, not only on localhost

If the container exits right away, you can't exec into it. Instead, start a shell from the image with docker run --rm -it --entrypoint sh <image>.

4. Inspect the config. docker inspect shows the full config: environment, mounts, networks, command and state.

Common causes:

  • Files were excluded by .dockerignore.
  • localhost points at the container itself, not at your machine.
  • Native dependencies are missing in the image.
  • Use docker ps -a to see the state and exit code.
  • Use docker logs to read the app's output.
  • Use docker exec to open a shell in a running container.
  • Use docker run --entrypoint sh for a container that exits at once.
  • Use docker inspect to see config, mounts and networks.