I'm trying to run a docker container and when I do docker run I need to pass it an environment variable that is defined inside the container.

The docker container in question is consul (https://github.com/hashicorp/docker-consul/blob/9fb940c32b6f46b0a77a640d7161054e00e97bbb/0.X/Dockerfile) and it has a custom script as entrypoint. Therefore the classic sh -c '...$VARIABLE' won't work because all this goes as arguments to the entrypoint and it obviously fails. Directly writing something like:

docker run consul [...] $VARIABLE

also doesn't work because $VARIABLE gets evaluated in the host and not in the container.


In the meantime I have managed to solve my own problem. The answer was quite simple. Since I know that something like:

docker exec -it container sh -c "echo $VARIABLE"

works perfectly, I can use sh -c to execute the entire entrypoint and pass it the parameters required. Therefore, when you have a custom entrypoint and want to pass it variables that are defined in the container itself, do this:

docker run yourimage sh -c "/path/to/entrypoint.sh param1 param2 ... $SOMEVAR ...."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.