Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export an environment variable to a docker image?

Tags:

docker

I can define "static" environment variables in a Dockerfile with ENV, but is it possible to pass some value at build time to this variable? I'm attempting something like this, which doesn't work:

FROM phusion/baseimage

RUN mkdir -p /foo/2016/bin && \
    FOOPATH=`ls -d /foo/20*/bin` && \
    export FOOPATH

ENV PATH $PATH:$FOOPATH

Of course, in the real use case I'd be running/unpacking something that creates a directory whose name will change with different versions, dates, etc., and I'd like to avoid modifying the Dockerfile every time the directory name changes.


Edit: Since it appears it's not possible, the best workaround so far is using a symlink:

FROM phusion/baseimage

RUN mkdir -p /foo/2016/bin && \
    FOOPATH=`ls -d /foo/20*/bin` && \
    ln -s $FOOPATH /mypath

ENV PATH $PATH:/mypath
like image 323
Jellby Avatar asked Dec 24 '16 18:12

Jellby


People also ask

How do I pass environment variables to docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

Can you export environment variables?

To export a environment variable you run the export command while setting the variable. We can view a complete list of exported environment variables by running the export command without any arguments. To view all exported variables in the current shell you use the -p flag with export.

Do docker images have environment variables?

When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.

How do I pass an environment variable from Docker Compose to Dockerfile?

Pass variables into Dockerfile through Docker Compose during build. If you want to pass variables through the docker-compose process into any of the Dockerfiles present within docker-compose. yml , use the --build-arg parameter for each argument to flow into all of the Dockerfiles.


1 Answers

To pass a value in at build time, use an ARG.

FROM phusion/baseimage

RUN mkdir -p /foo/2016/bin && \
    FOOPATH=`ls -d /foo/20*/bin` && \
    export FOOPATH

ARG FOOPATH
ENV PATH $PATH:${FOOPATH}

Then you can run docker build --build-arg FOOPATH=/dir -t myimage .


Edit: from you comment, my answer above won't solve your issue. There's nothing in the Dockerfile you can update from the output of the run command, the output isn't parsed, only the resulting filesystem is saved. For this, I think you're best off in your run command writing the path to the image and read in from your /etc/profile or a custom entrypoint script. That depends on how you want to launch your container and the base image.

like image 197
BMitch Avatar answered Oct 11 '22 04:10

BMitch