Placing a function inside the ENV in Dockerfile

0

I need to create a Variable that captures the Docker network interface gateway, follow the command:

RUN declare -x SERVER_IP=$(ip route|awk '/default/ { print $3 }')
ENV GATEWAY="$SERVER_IP"
...

During the construction of Dockerfile it seems that it does not work by leaving blank, I also tried that way straight:

ENV GATEWAY="$(ip route|awk '/default/ { print $3 }')"

Also, nothing, leaving the details I'm using the Ubuntu Image: 16.04 and before arriving at the variable I already installed the iproute2 for the command ip .

    
asked by anonymous 23.12.2016 / 03:25

1 answer

1

The ENV command has the idea of defining "static" variables, for example: ENV ENV_TYPE DEV ENV PROD_VERSION 3.1.1

It will not try to process the value of the variable, it will only accept it as the final value.

For values that are calculated / retrieved at runtime it would be best to use ENTRYPOINT or the CMD command to add a script to run the container, so every time you run docker run suaimage this script will run.

Example:

In a folder I have two files: entrypoint.sh and Dockerfile :

  • entrypoint.sh :
#!/bin/sh

export SERVER_IP=$(ip route|awk '/default/ { print $3 }')
echo "SERVER_IP: $SERVER_IP"
  • Dockerfile :
FROM alpine:3.5

ADD entrypoint.sh /

ENTRYPOINT ["/bin/sh", "/entrypoint.sh"]

That way when you do docker build and docker run the output will be as below:

$ docker build -t test .
Sending build context to Docker daemon 28.67 kB
Step 1/3 : FROM alpine:3.5
 ---> 4a415e366388
Step 2/3 : ADD entrypoint.sh /
 ---> bb398e3cd80a
Removing intermediate container 7d1f1aa2ba81
Step 3/3 : ENTRYPOINT /bin/sh /entrypoint.sh
 ---> Running in 1c71f190fa2e
 ---> f43f141922cf
Removing intermediate container 1c71f190fa2e
Successfully built f43f141922cf
$ docker run test
SERVER_IP: 172.17.0.1

Instead of echo "SERVER_IP: $SERVER_IP" , there must be the specific logic to start the service that your container wants to provide.

Entrypoints documentation: link

    
09.03.2017 / 12:38