My directory structure looks like this.
|
|
— Dockerfile
| — .env
Content of .env
file looks like this.
VERSION=1.2.0
DATE=2022-05-10
I want to access VERSION
and DATE
as environment variable both during build time and run time. So ENV
is the one I should use. I know that.
How exactly can I do that ?
I tried using RUN
command in Dockerfile
like
RUN export $(cat .env)
But, it can only be accessed during runtime and not build time.
So, how can this be achieved with ENV
?
I can do it manually like
ENV VERSION 1.2.0
ENV DATE 2022-05-10
But, it is inefficient when I have many environment variables.
P.S. I cannot use docker-compose
because the image is going to be used by kubernetes pods, so.
3
Answers
You could firstly export these variables as environmetal variables to your bash
Then use
--build-arg
flag to pass them to your docker buildNext in your dockerfile
As a result, for example you can access the variables in your build phase as
VERSION
and in your container asversion
You can specify the
env_file
in thedocker-compose.dev.yml
file as follows:and you have to have a .env.development file containing all the environment variables you need to pass to the docker image.
e.g.:
What I’ve read from others says that there is no "docker build –env-file…." option/apparatus. As such, this situation makes a good argument for shifting more of the content of the dockerfile to a shell script that the dockerfile simply copies and runs, as you can source the .env file that way.
greetings.sh
variables.env
dockerfile