Solve the problem of exiting docker container immediately after starting

  • 2021-09-12 02:40:14
  • OfStack

I recently looked at how docker lets the container run certain processes directly at startup, and later found that Dockerfile can specify the container run command when the container starts.

CMD specifies, but each Dockerfile can only have one CMD instruction. If multiple CMD specifications are specified, only the last one will be executed.

So I thought of a way to write a script, start multiple processes in the script, and run this script in Dockerfile.

Finally, this method is proved to be feasible. One problem is encountered in the experiment, and the container will stop immediately after starting.

Upon access to information:

The Docker container can only manage 1 process at the same time. If this process exits, the container will exit, but this does not mean that the container can only run 1 process (other processes can run in the background), but to make the container not exit, there must be 1 process executed in the foreground.

Solution:

The last process 1 in the script must be run in the foreground mode, that is, it is not added at the end of the process & ( & Indicates running in the background, otherwise the container will exit.

Supplementary knowledge: Problems and solutions with gdbserver in the docker container

Encountered 1 problem when using gdbserver in docker container

linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME:operation not permmit

The reason is that PTRACE is disabled by default by Docker and needs to be turned on when the container is running.

docker run -ti --cap-add=SYS_PTRACE ubuntu


Related articles: