1 #!/usr/bin/dumb-init /bin/sh
5 # Note above that we run dumb-init as PID 1 in order to reap zombie processes
6 # as well as forward signals to all processes in its session. Normally, sh
7 # wouldn't do either of these functions so we'd leak zombies as well as do
8 # unclean termination of all our sub-processes.
9 # As of docker 1.13, using docker run --init achieves the same outcome.
11 # You can set CONSUL_BIND_INTERFACE to the name of the interface you'd like to
12 # bind to and this will look up the IP and pass the proper -bind= option along
15 if [ -n "$CONSUL_BIND_INTERFACE" ]; then
16 CONSUL_BIND_ADDRESS=$(ip -o -4 addr list $CONSUL_BIND_INTERFACE | head -n1 | awk '{print $4}' | cut -d/ -f1)
17 if [ -z "$CONSUL_BIND_ADDRESS" ]; then
18 echo "Could not find IP for interface '$CONSUL_BIND_INTERFACE', exiting"
22 CONSUL_BIND="-bind=$CONSUL_BIND_ADDRESS"
23 echo "==> Found address '$CONSUL_BIND_ADDRESS' for interface '$CONSUL_BIND_INTERFACE', setting bind option..."
26 # You can set CONSUL_CLIENT_INTERFACE to the name of the interface you'd like to
27 # bind client intefaces (HTTP, DNS, and RPC) to and this will look up the IP and
28 # pass the proper -client= option along to Consul.
30 if [ -n "$CONSUL_CLIENT_INTERFACE" ]; then
31 CONSUL_CLIENT_ADDRESS=$(ip -o -4 addr list $CONSUL_CLIENT_INTERFACE | head -n1 | awk '{print $4}' | cut -d/ -f1)
32 if [ -z "$CONSUL_CLIENT_ADDRESS" ]; then
33 echo "Could not find IP for interface '$CONSUL_CLIENT_INTERFACE', exiting"
37 CONSUL_CLIENT="-client=$CONSUL_CLIENT_ADDRESS"
38 echo "==> Found address '$CONSUL_CLIENT_ADDRESS' for interface '$CONSUL_CLIENT_INTERFACE', setting client option..."
41 # CONSUL_DATA_DIR is exposed as a volume for possible persistent storage. The
42 # CONSUL_CONFIG_DIR isn't exposed as a volume but you can compose additional
43 # config files in there if you use this image as a base, or use CONSUL_LOCAL_CONFIG
45 CONSUL_DATA_DIR=/consul/data
46 CONSUL_CONFIG_DIR=/consul/config
48 # You can also set the CONSUL_LOCAL_CONFIG environemnt variable to pass some
49 # Consul configuration JSON without having to bind any volumes.
50 if [ -n "$CONSUL_LOCAL_CONFIG" ]; then
51 echo "$CONSUL_LOCAL_CONFIG" > "$CONSUL_CONFIG_DIR/local.json"
54 # If the user is trying to run Consul directly with some arguments, then
55 # pass them to Consul.
56 if echo "$1" | grep '^-' >/dev/null; then
60 # Look for Consul subcommands.
61 if [ "$1" = 'agent' ]; then
64 -data-dir="$CONSUL_DATA_DIR" \
65 -config-dir="$CONSUL_CONFIG_DIR" \
69 elif [ "$1" = 'version' ]; then
70 # This needs a special case because there's no help output.
72 elif consul --help "$1" 2>&1 | grep -q "consul $1"; then
73 # We can't use the return code to check for the existence of a subcommand, so
74 # we have to use grep to look for a pattern in the help output.
78 # If we are running Consul, make sure it executes as the proper user.
79 if [ "$1" = 'consul' ]; then
80 # If the data or config dirs are bind mounted then chown them.
81 # Note: This checks for root ownership as that's the most common case.
82 if [ "$(stat -c %u /consul/data)" != "$(id -u consul)" ]; then
83 chown consul:consul /consul/data
85 if [ "$(stat -c %u /consul/config)" != "$(id -u consul)" ]; then
86 chown consul:consul /consul/config
89 # If requested, set the capability to bind to privileged ports before
90 # we drop to the non-root user. Note that this doesn't work with all
91 # storage drivers (it won't work with AUFS).
92 if [ ! -z ${CONSUL_ALLOW_PRIVILEGED_PORTS+x} ]; then
93 setcap "cap_net_bind_service=+ep" /bin/consul
96 # Instead of using this we run our pod as a non-root user.
97 # set -- su-exec consul:consul "$@"