Details the method by which the docker container does not automatically exit the end run

  • 2021-01-03 21:09:42
  • OfStack

This article briefly describes the relationship between the docker container and the leading process, and how to write Dockerfile/ docker-compose.yml to make the container run permanently.

The life cycle of the docker container is related to the pre-process in the container, which is why we might encounter a container that runs for a few seconds and then ends automatically: because there is no permanent pre-process in the container, the container exits automatically after the pre-process has finished running.

Such as docker hello - world


# 1 flash   The output 1 pile 
docker run --name hello-world hello-world
#  You can see  hello-world  The container has exited 
docker ps -a

So how do you make the container not exit automatically? If we want to log in to a pure container like alpine/centos/ubuntu, install some service components on top of it, and then mirror them on commit.

There are many ways to look at the web, such as creating containers that perform an while(true) loop (under sleep 1, of course) or using the ES27en-ES28en /dev/null 1 class, in order to start a pre-process that can be permanent. We could have used the interactive and tty parameters of the docker container more elegantly to turn the sh/bash (*nix system must) command on as a front command so that the container would not exit automatically.

For example, using the alpine image as the base image, create a small alpine system container that can be run permanently so that we can log in and interactively execute certain commands.


#  use  alpine  System image creation container 
# -i interactive=true  open  stdin
# -t tty=true  Allocation session terminal 
# -d  Guardian mode   You can do without it   It goes straight into the container without being added   Need to be  ctrl+p+q  Cut out 
#  Can't  exit  Yo,  exit  Equivalent to ending  sh  The session   The container will exit 
docker run -it -d --name alpine alpine sh
# alpine  Must be running 
docker ps
#  Log in to the container 
docker exec -it alpine sh
# apline  The use of  apk  Manage as a package 
#  Install a little train 
#  You can use it later  docker commit -m "alpine with sl cmd" -a "big_cat" alpine big_cat/alpine_sl  Generate a new image 
apk add sl
#  Out of the container   Note: -d  It's ok to start, if not  -d  Activate direct access  sh terminal   You cannot exit, or the container will exit 
exit

Commit container changes to generate a new image


docker commit -m "alpine with sl cmd" -a "big_cat" alpine big_cat/alpine_sl
docker images
#  Post to if you have an account  docker hub  Go up 
docker push big_cat/alpine_sl


#  Subsequent to stop / There is no need to specify when starting the container  -it  Parameters of the 
docker stop alpine
docker start alpine

Commit container changes to generate a new image


docker commit -m "alpine with sl cmd" -a "big_cat" alpine big_cat/alpine_sl
docker images
#  Post to if you have an account  docker hub  Go up 
docker push big_cat/alpine_sl

The above command uses the sh/bash session terminal as the leading process, so that the container does not exit automatically.

If you think it would be rude to write this way when creating containers, that's fine, we can push all of this to ES56en-ES57en
docker-compose.yml


version: '3'

services:
  big_cat_alpine:
    container_name: big_cat_alpine
    image: alpine
    stdin_open: true # -i interactive
    tty: true # -t tty
    privileged: true
    entrypoint: ["sh"] #  perform  sh

Create a container & Log in to the container


docker-compose up -d big_cat_alpine ./
docker ps
docker exec -it big_cat_alpine sh

Pass in those two parameters via ES67en-ES68en and start the service container after marshalling.


Related articles: