How can I keep running when the docker container exits after running?

The nsenter tool enters the docker container

Overview


For docker containers running in the background, what we often need to do is to enter the container, docker provides us with docker exec, docker attach commands, and also provides nsenter tools, external tools for us to use. The problem with docker attach is: when multiple windows are attached to the same container at the same time, all windows will be displayed synchronously. If one of the windows is blocked, other windows will also be blocked. The docker attach command can be said to be the most efficient. Inconvenient way to get into the background docker container. The docker exec command is a more convenient command added after docker 1.3 than the docker attach command. A command almost as convenient as docker exec is the nsenter tool.

nsenter installation


$ cd /tmp;
$ curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf-; cd util-linux-2.24;
$ ./configure  --without-ncurses
$ make nsenter && sudo cp nsenter /usr/local/bin
  • 1
  • 2
  • 3
  • 4

nsenter use


Before using the nsenter command, you need to get the process of the docker container, and then use the nsenter tool to enter the docker container. The specific usage is as follows:

$ docker inspect -f {{.State.Pid}} 容器名或者容器id   #每一个容器都有.State.Pid,所以这个命令除了容器的id需要我们根据docker ps -a去查找,其他的全部为固定的格式
$ nsenter --target 上面查到的进程id --mount --uts --ipc --net --pid  #输入该命令便进入到容器中
  • 1
  • 2

Explain the meaning of the parameters after the process id in the nsenter command: 
* –mount parameter is to enter the mount namespace 
* –uts parameter is to enter the uts namespace 
* –ipc parameter is to enter the System V IPC namaspace 
* –net parameter is to enter Go to the network namespace 
* –pid parameter is to enter the pid namespace 
* –user parameter is to enter the user namespace

In Linux, the easiest way to view the meaning of the specified command parameters is to type in the terminal:

$ nsenter --help  #会回显所有与该命令有关的参数
$ man nsenter  #能查到更加详细的使用示例和参数说明

Phenomenon

Start the docker container 
docker run –name [CONTAINER_NAME] [CONTAINER_ID] 
Check the running status  of the container
docker ps -a 
finds that the mydocker container just started has exited

reason

The main thread of the docker container (command executed by CMD in the dockfile) ends, and the container exits

Method

Interactive launch can be used

docker run -i [CONTAINER_NAME or CONTAINER_ID]
  • 1

The above is not very friendly, it is recommended to use background mode and tty options

docker run -dit [CONTAINER_NAME or CONTAINER_ID]
  • 1

View container status

docker ps -a
  • 1

Docker calls out the background container

docker attach [CONTAINER_NAME or CONTAINER_ID]
  • 1

TIPs : When exiting, use [ctrl + D], which will end the current thread of docker and the container ends, you can use [ctrl + P] [ctrl + Q] to exit without terminating the container running

The following command will execute the specified command in the specified container. After [ctrl+D] exits, the container will not be terminated.

docker exec -it [CONTAINER_NAME or CONTAINER_ID] /bin/bash

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326239238&siteId=291194637