Debug a container that exits immediately due to missing env var
Easy

Problem

A teammate just deployed myapp:latest to a server and messaged you that the container won't stay up. docker run prints a container id and the prompt returns — but when you come back a second later the container is already gone. The app's startup doesn't panic; it exits cleanly. Something it needs isn't there.

Your job: find what the app is missing, and restart the container so it reaches running and stays running.

Initial setup

Two containers on the host:

  • api-servermyapp:latest, exited with code 1 two minutes ago. No env vars set on it.
  • db-postgres — healthy postgres:15-alpine, listening on 5432. Already has a database named app with user/password postgres/postgres.
Both containers are on the default bridge network so they can reach each other by container name.

Example interaction

$ docker ps -a
CONTAINER ID   IMAGE                 STATUS                      NAMES
f1e2d3c4b5a6   myapp:latest          Exited (1) 2 minutes ago    api-server
a6b5c4d3e2f1   postgres:15-alpine    Up 1 hour                   db-postgres

$ docker logs api-server
Starting myapp...
Error: DATABASE_URL environment variable is not set.

The log tells you what is missing — now you need to restart the container with that piece of configuration set correctly.

Acceptance

You've solved it when:

  • You've identified that DATABASE_URL is not set on api-server (from logs or inspect).
  • You've restarted api-server with DATABASE_URL pointing at the running db-postgres container.
  • api-server reaches status=running and does not exit again.

Constraints

  • Tools: docker CLI only.
  • Time limit: 10 minutes.
  • Don't modify or rebuild the image. The fix is purely runtime config.

Follow-up

  1. If you had 10 env vars to pass instead of 1, would you still use -e? What's the docker run flag designed for larger config sets?
  2. What does docker inspect api-server --format '{{range .Config.Env}}{{println .}}{{end}}' tell you, and how does that compare to what you passed on the command line?
Live session
Code
SavedNo commands yet