tipra
tipra

Reputation: 31

Environment variable passed through -e flag in docker container not accessible in entrypoint script

I am trying to run a docker container via the docker run command. I am specifying an environment variable via the -e flag. The strange thing is that I am able to read that environment variable if I exec /bin/bash into the container, but not in the entrypoint script as defined in the Dockerfile.

My docker container has debian:9 as the base and the entrypoint script is a simple bash script that logs some data along with the environment variable.

Dockerfile:

FROM debian:9
RUN apt-get update && apt-get install -y curl sudo supervisor wget
ADD starter-service.sh .
RUN chmod 755 starter-service.sh
ENTRYPOINT ["sudo", "/bin/bash", "-c", "./starter-service.sh"]

starter-service:

#! /bin/bash
license=$LICENSE_KEY
if [ "$license" == "" ]
then
  echo "No License Key: "$LICENSE_KEY" arg:"$arg > /tmp/my-service.log
  printenv >> /tmp/my-service.log
fi
sleep 1000s

The environment variable in question is LICENSE_KEY. Docker run command:

docker run -e LICENSE_KEY=123 <docker image>

Upvotes: 1

Views: 1272

Answers (1)

naveen chandru
naveen chandru

Reputation: 108

You have used sudo in your entry point. To preserve the environment variables, you need to use -E, --preserve-env option. In dockerfile, updating the entrypoint to following should work:

ENTRYPOINT ["sudo", "-E", "/bin/bash", "-c", "./starter-service.sh"]

Upvotes: 1

Related Questions