The command ENV
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 variable value, it will only accept it as the final value.
For the values that are calculated/recovered at the time of execution it would be better to use ENTRYPOINT
or the command CMD
to add a script to running the container, that way every time you run docker run suaimage
this script will be executed.
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 the 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
In place of echo "SERVER_IP: $SERVER_IP"
must be the specific logic to start the service your container wants to provide.
Documentation about entrypoints: https://docs.docker.com/engine/reference/builder/#entrypoint
when you set the value of
GATEWAY
using the commandENV
ofDockerfile
the value of it will remain in the image, but it will not be renewed when you start a container... is that really what you want? it would not be better to take this information in the entrypoint of the image?– Lucas dos Santos Abreu
There is a software that will start through the
supervisord
, and it looks for this variable in the system but since Docker does not start theinit
i pecorrir arrow a dynamic variable, in the case of ENTRYPOINT do not know use can give me an example?– iLeonardo Carvalho
got a little big... so I wrote a really big reply.
– Lucas dos Santos Abreu