在docker入口点脚本中使用exec有什么目的? [英] What purpose does using exec in docker entrypoint scripts serve?

查看:498
本文介绍了在docker入口点脚本中使用exec有什么目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如在redis官方图片中:

For example in the redis official image:

https://github.com/docker-library/redis/blob/master/2.8/docker-entrypoint.sh

#!/bin/bash
set -e

if [ "$1" = 'redis-server' ]; then
    chown -R redis .
    exec gosu redis "$@"
fi

exec "$@"

为什么不在没有exec的情况下像往常一样运行命令?

Why not just run the commands as usual without exec preceding them?

推荐答案

正如@Peter Lyons所说,使用exec将替换父进程,而不是运行两个进程.

As @Peter Lyons says, using exec will replace the parent process, rather than have two processes running.

这在Docker中对于正确代理信号很重要.例如,如果Redis是在没有exec的情况下启动的,则它不会在docker stop时收到SIGTERM,也没有机会干净地关闭.在某些情况下,这可能会导致数据丢失或僵尸进程.

This is important in Docker for signals to be proxied correctly. For example, if Redis was started without exec, it will not receive a SIGTERM upon docker stop and will not get a chance to shutdown cleanly. In some cases, this can lead to data loss or zombie processes.

如果您确实启动了子进程(即不使用exec),则父进程将负责适当地处理和转发信号.这是在容器中运行多个进程时最好使用supervisor或类似命令的原因之一,因为它将适当地转发信号.

If you do start child processes (i.e. don't use exec), the parent process becomes responsible for handling and forwarding signals as appropriate. This is one of the reasons it's best to use supervisord or similar when running multiple processes in a container, as it will forward signals appropriately.

这篇关于在docker入口点脚本中使用exec有什么目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆