Docker Community Forums

Share and learn in the Docker community.

Is it possible to use variables exported in entrypoint script when launching a docker container?

docker

(Dockermiguelpinto25) #1

So, my problem is that I’m trying to build a docker service with 3 replicas using etcd.

I need to know prior to launching the container its IP and Name so I created an entrypoint script to generate that for me and than I exported that to an environment variable.

How can I use those variables or reference them when running docker service?

Dockerfile

FROM bitnami/etcd:latest
COPY etcd.sh /
ENV ALLOW_NONE_AUTHENTICATION yes
ENV ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379
ENV ETCD_DATA_DIR=/etcd-data/LOGS
USER root
RUN chmod +x /etcd.sh
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
ENTRYPOINT ["/etcd.sh"]

etcd.sh

#!/bin/bash

ETCD_VERSION=latest
TOKEN=my-etcd-token
CLUSTER_STATE=new
DATA_DIR=/etcd-data/LOGS

echo "[DEBUG] Setting up static variables...DONE"

a=0

while [ $a -lt 3 ]
do
    NAME[$a]=etcd-node-$a
    a=`expr $a + 1`
done

echo "[DEBUG] Setting up names of the nodes...DONE"

apt-get update
apt-get install -y dnsutils
cluster="$(nslookup tasks.etcd-server | grep Address | awk 'NR>1{print $2}' ORS=',' | head -c -1)"

echo "[DEBUG] Installing nslookup and looking for other nodes in the network...DONE"

IFS=',' read -ra ADDR <<< "$cluster"

a=0

for i in "${ADDR[@]}"
do
    HOST[$a]=$i
    a=`expr $a + 1`
done

echo "[DEBUG] Setting up IP addresses of the nodes...DONE"

a=0
while [ $a -lt 3 ]
do 
    CLUSTER=${NAME[$a]}=http://${HOST[$a]}:2380,$CLUSTER
    a=`expr $a + 1`
done

export CLUSTER="${CLUSTER::-1}"

echo "Setup phase...DONE"
echo "------------------"
echo $CLUSTER
echo "------------------"

export THIS_IP="$(hostname -i)"

for i in "${!HOST[@]}"; do
   if [[ "${HOST[$i]}" = "${THIS_IP}" ]]; then
       export THIS_NAME="${HOST[$i]}";
   fi
done

The variables from the script that I want to use are CLUSTER, THIS_IP, THIS_NAME.

With this variables I could run from my host machine the following command:

docker service create --replicas 3 \
--network etcd-network \
--name etcd-server \
--mount source=etcd-vol,target=/etcd-data/LOGS custom-etcd 3 \
--data-dir=/etcd-data --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2380 \
--listen-peer-urls http://${THIS_IP}:2380 \
--advertise-client-urls http://${THIS_IP}:2379 \
--listen-client-urls http://${THIS_IP}:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state new