Home > Software design >  How to stay inside docker when error arises?
How to stay inside docker when error arises?

Time:07-26

I am running a docker container using the docker run -it flags to compile some programs there.

Whenever a error, keyboard interrupt, segfault arises inside the environment, I get kicked out of the docker environment.

What's the reason for that behaviour? Is it something related to my setup or is it the default? I would like to change that since it is not too uncommon to get some errors while trying to develop something.

CodePudding user response:

How to stay inside docker when error arises?

Do not terminate your process when error arises.

What's the reason for that behaviour?

The main process that is running inside the container has terminated.

Is it something related to my setup or is it the default?

Yes and yes. It's not really "default", it's how it works.

I would like to change

So wrap your compilation process in a different process, so that your main process do not exit. Typically, with shell:

docker ... sh -c 'if ! your_compilation_command ; then echo Och nooo, compiling failed; echo Starting interactive shell; sh -li; fi'
docker_run_then_if_failed_drop_to_shell() {
    docker ... sh -c 'if ! "$@"; then ...' _ "$@"
}
docker_run_then_if_failed_drop_to_shell your compilation command

CodePudding user response:

In docker, containers are ephemeral which mean is you can reach the container only on running state. Also remember that every every container working as process on host system. If container is down, there will be no process which represent the container. If you would like to observe or fetch data for debug purpose, I can advice you to use volume then you can store debug files on shared disk space between container and host.

BR,

CodePudding user response:

didn't quite understand the scenario and what you are trying to develop through docker but may be you can try following:

docker run <image:tag> -d

and then do :

docker exec -it <container_id> /bin/bash

for retrieving container_id, user docker ps

  • Related