DEV Community

Cover image for Get a debug console on failing docker container launches.
Dimitrios Desyllas
Dimitrios Desyllas

Posted on

Get a debug console on failing docker container launches.

Using docker compose I am building an image:

docker compose build someservice
Enter fullscreen mode Exit fullscreen mode

Or directly via docker for example:

docker build .
Enter fullscreen mode Exit fullscreen mode

But when I try to launch it:

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

But for some reason is unable to be launched and it is unexpectedly terminated.

Via docker ps -a we detect our failing docker container for example:

$ docker ps -a

CONTAINER ID   IMAGE                           COMMAND                  CREATED              STATUS                      PORTS                                                             NAMES
67a0f27524a6   blog-testprod-django-testprod   "/bin/sh /app/entryp…"   About a minute ago   Exited (127) 1 second ago                                                                     blog-testprod-django-testprod-1
54d03ad72f70   nginx:stable-alpine             "/docker-entrypoint.…"   39 minutes ago       Up 38 minutes               0.0.0.0:8001->80/tcp, [::]:8001->80/tcp                           blog-testprod-nginx-1
99864e6ab6a5   axllent/mailpit                 "/mailpit"               39 minutes ago       Up 39 minutes               1025/tcp, 1110/tcp, 0.0.0.0:8026->8025/tcp, [::]:8026->8025/tcp   blog-testprod-mailer-1
6232c1fdeaea   postgres:17                     "docker-entrypoint.s…"   39 minutes ago       Up 39 minutes (healthy)     0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcp                       blog-testprod-postgresql-1
d0d614339873   moby/buildkit:buildx-stable-1   "buildkitd --allow-i…"   13 months ago        Up 13 hours                                                                                   buildx_buildkit_loving_hawking0
Enter fullscreen mode Exit fullscreen mode

As we can see the container blog-testprod-django-testprod-1 is failing and we need to diagnose why it happens. For that we need to access a console in order to run our diagnostic commands.

For that we commit it into a new image:

docker commit blog-testprod-django-testprod-1 debug-image
Enter fullscreen mode Exit fullscreen mode

And then we run overriding the entrypoint with a shell. I our case we are using /bin/bash:

docker run -it --rm --entrypoint /bin/bash debug-image
Enter fullscreen mode Exit fullscreen mode

The entrypoint override happend via --entrypoint param.

If /bin/bash is not available use /bin/sh instead:

docker run -it --rm --entrypoint /bin/sh debug-image
Enter fullscreen mode Exit fullscreen mode

We do not need to provide any further arguments after debug-image whebn executing command above. Using shell as an entrypoint alongside the -ti we receive an interactive shell.

Now we are able to execute any command nessesary in order to diagnose any problem, the commands needed for problem diagnosis depend on each case.

Top comments (0)