Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What purpose does using exec in docker entrypoint scripts serve?

Tags:

bash

docker

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 "$@" 

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

like image 665
m0meni Avatar asked Aug 27 '15 17:08

m0meni


People also ask

What is the use of Docker exec?

Description. The docker exec command runs a new command in a running container. The command started using docker exec only runs while the container's primary process ( PID 1 ) is running, and it is not restarted if the container is restarted. COMMAND will run in the default directory of the container.

Does Docker Exec use ENTRYPOINT?

Docker ENTRYPOINT Unlike CMD commands, ENTRYPOINT commands cannot be ignored or overridden—even when the container runs with command line arguments stated. A Docker ENTRYPOINT instruction can be written in both shell and exec forms: Exec form: ENTRYPOINT [“executable”, “parameter1”, “parameter2”]

What is exec $@ in docker ENTRYPOINT?

The 'exec' form allows us to specify the command line arguments to the 'docker run' command and it is appended to the end of all elements of the 'exec' form which means the specified command will run after the executable specified in entrypoint.

What is docker run and exec?

Docker Run vs Docker Exec! This is a fairly common question – but has a simple answer! In short, docker run is the command you use to create a new container from an image, whilst docker exec lets you run commands on an already running container!


1 Answers

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

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.

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.

like image 66
Adrian Mouat Avatar answered Sep 30 '22 04:09

Adrian Mouat