Hemanth S. Vaddi
Hemanth S. Vaddi

Reputation: 463

Access $HOME in Dockerfile ENV instruction

I'm trying to set an environment variable with ENV instruction dynamically using $HOME variable (i think its a system environment variable).

But ENV is not able to access $HOME. Its blank. Although i'm able to echo $HOME.

FROM somebaseimage

....
....

USER 5051

RUN echo $HOME
# prints /home/myuser

ENV MY_JSON_FILEPATH="${HOME}/my_file.json"

RUN echo $MY_JSON_FILEPATH
# prints /my_file.json

I have tried "${HOME}/my_file.json", "$HOME/my_file.json"; both don't work.

What would be the best way to set such environment variable?

Upvotes: 2

Views: 1397

Answers (2)

Slava
Slava

Reputation: 743

In case you need that variable just during the build

FROM ubuntu

SHELL ["/bin/bash", "-c"]

ENV BASH_ENV="/etc/bash_env"

USER root

RUN echo "export MY_JSON_FILEPATH=$HOME/MY_JSON_FILEPATH" >>$BASH_ENV

RUN echo $MY_JSON_FILEPATH
#6 [3/3] RUN echo $MY_JSON_FILEPATH
#6 0.246 /root/MY_JSON_FILEPATH
#6 DONE 0.3s

Reference

Upvotes: 0

Aleksey Vaganov
Aleksey Vaganov

Reputation: 501

You should add the line ARG HOME after the FROM ... line and pass build option --build-arg or add build:args parameter in your docker-compose file.

Upvotes: 3

Related Questions