Using docker compose I am building an image:
docker compose build someservice
Or directly via docker for example:
docker build .
But when I try to launch it:
docker compose up -d
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
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
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
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
We do not need to provide any further arguments after
debug-imagewhebn executing command above. Using shell as an entrypoint alongside the-tiwe 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.