Home > Operation and Maintenance > Docker > How docker prevent the container from exiting automatically

How docker prevent the container from exiting automatically

WBOY
Release: 2022-03-08 10:40:21
Original
5769 people have browsed it

In docker, you can add a resident pre-process to the container so that the container does not exit automatically. You can use the interactive and tty parameters of the docker container to enable the "sh/bash" command as a potential command. This way the container will not exit automatically.

How docker prevent the container from exiting automatically

The operating environment of this tutorial: linux7.3 system, docker-1.13.1 version, Dell G3 computer.

How docker prevents the container from automatically exiting

The life cycle of a docker container is related to the pre-process in the container. This is why we may encounter some containers that only run for a few times. The reason why it automatically ends in seconds: Because there is no resident front-end process in the container, the container automatically exits after the front-end process finishes running.

For example, docker hello-world

# 一闪而过 输出一堆东西
docker run --name hello-world hello-world
# 可以看到 hello-world 容器已经退出了
docker ps -a
Copy after login

How can I prevent the container from automatically exiting? If we want to log into a pure container such as alpine/centos/ubuntu, install some service components based on it, and then commit into our own image.

Looking at the Internet, there are many methods to execute a while(true) infinite loop (of course, sleep for a while) or use tail -f /dev/null when creating a container. Anyway, it is to open a container that can The purpose of the resident front-end process. In fact, we can use the interactive and tty parameters of the docker container more elegantly to open the sh/bash (*nix system must have) command as a pre-command, so that the container will not exit automatically.

For example, use the alpine image as the base image to create an alpine system small container so that it can run permanently so that we can log in and execute certain commands interactively.

# 使用 alpine 系统镜像创建容器
# -i interactive=true 开启 stdin
# -t tty=true 分配会话终端
# -d 守护模式 不加也可以 不加就直接进入容器中了 需要 ctrl+p+q 切出
# 不能 exit 哟, exit 相当于结束 sh 会话了 容器会退出的
docker run -it -d --name alpine alpine sh
# alpine 肯定在运行
docker ps
# 登入容器
docker exec -it alpine sh
# apline 使用的 apk 作为包管理
# 安装个小火车
# 后续可以使用 docker commit -m "alpine with sl cmd" -a "big_cat" alpine big_cat/alpine_sl 生成新的镜像
apk add sl
# 退出容器 注:-d 启动的才可以,如果没有 -d 启动直接进入的 sh终端 则不能退出,否则容器也会退出
exit
Copy after login

Submit container changes to generate a new image

docker commit -m "alpine with sl cmd" -a "big_cat" alpine big_cat/alpine_sl
docker images
# 有账号的话发布到 docker hub 上去
docker push big_cat/alpine_sl
# 后续停止/启动容器时就不需要在指定 -it 参数了
docker stop alpine
docker start alpine
Copy after login

Submit container changes to generate a new image

docker commit -m "alpine with sl cmd" -a "big_cat" alpine big_cat/alpine_sl
docker images
# 有账号的话发布到 docker hub 上去
docker push big_cat/alpine_sl
Copy after login

The above command actually uses the sh/bash session terminal as a pre-process. So that the container will not exit automatically.

If you think writing like this when creating a container will be crude, it doesn’t matter, we can push these to docker-compose

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"] # 执行 sh
Copy after login

Create Container & Login Container

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

Pass in those two parameters through docker-compose, and start the service container after orchestration.

Recommended learning: "docker video tutorial"

The above is the detailed content of How docker prevent the container from exiting automatically. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template