What purpose does using exec in docker entrypoint scripts serve?

前端 未结 3 1100
眼角桃花
眼角桃花 2020-12-23 11:26

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\"         


        
3条回答
  •  Happy的楠姐
    2020-12-23 12:12

    Without exec, the parent shell process survives and waits for the child to exit. With exec, the child process replaces the parent process entirely so when there's nothing for the parent to do after forking the child, I would consider exec slightly more precise/correct/efficient. In the grand scheme of things, I think it's probably safe to classify it as a minor optimization.

    without exec

    • parent shell starts
    • parent shell forks child
      • child runs
      • child exits
    • parent shell exits

    with exec

    • parent shell starts
    • parent shell forks child, replaces itself with child
    • child program runs taking over the shell's process
    • child exits

提交回复
热议问题