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
:
#!/bin/sh
export SERVER_IP=$(ip route|awk '/default/ { print $3 }')
echo "SERVER_IP: $SERVER_IP"
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