Debugging docker exit 0

Guy Thomas
1 min readJan 27, 2024

Today, I had an issue with a node JS app failing to connect to a DB. It resulted in the container stopping with exit 0.

I wondered how I could debug the failed DB connection, since it wasn’t possible to exec onto the docker container. It turns out that the solution is relatively simple.

Here’s how my nodejs docker container looked:

FROM node:21-bookworm
LABEL org.label-schema.name="My app"
WORKDIR /usr/app
COPY ./app /usr/app
COPY ./.env /usr/app

ARG PORT=80
ENV PORT=${PORT}

EXPOSE $PORT

RUN npm install

ENTRYPOINT ["node", "app.js"]

The entry point is the nodeJs app, but that dies almost immediately. How can I exec onto it if the entrypoint immediately exits with 0? Simple, change the last part of the docker file as follows:

# ENTRYPOINT ["node", "app.js"]
CMD tail -f /dev/null

Now, when the docker container starts, it’ll run a tail on /dev/null as the main process. This means the container isn’t going to die on startup and I can start debugging it. Let’s assume that my docker container is called “myapp” — I can exec onto it as follows.

docker exec -it myapp bash

Now I can run anything I want on the docker container. In my case this was installing mongosh so that I could debug the connection to my db container.

--

--