How can the docker container run until it exits of

  • 2021-01-02 22:03:05
  • OfStack

The phenomenon of

Start the docker container


docker run  � name [CONTAINER_NAME] [CONTAINER_ID] 

View the health status of the container


docker ps -a 

Found that the mydocker container you just started has exited

why

It is important to note that the Docker container must have a foreground process to run in the background.

If the container runs commands that are not directly suspended by 1 (such as running top, tail), it will exit automatically

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

The solution

You can use interactive startup


docker run -i [CONTAINER_NAME or CONTAINER_ID]

The above is not very friendly, and the background mode and tty options are recommended


docker run -dit [CONTAINER_NAME or CONTAINER_ID]

View the container status


docker ps -a

docker pulls out the background container


docker attach [CONTAINER_NAME or CONTAINER_ID]

TIPs: When exiting, use [ctrl + D]. This terminates docker the current thread and the container. You can exit using [ctrl + P][ctrl + Q] without termining the container

The following command executes in the specified container and [ctrl+D] does not terminate the container run after exit


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

Related articles: