I am trying to pass an argument from a file to docker file.
I tried to the copy the file into docker container and use source command.
COPY docker.env .
RUN /bin/bash -c "source docker.env"
BuildMode="release"
export BuildMode="release"
There is no error but when I tried to print ECHO $BuildMode
, it is not printing the value inside it, it is like it did not get the value inside of it.
But if I create a docker container and try the same codes inside docker container, then it is working.
ARG instruction defines a variable that can be passed at build time. Once it is defined in the Dockerfile you can pass with this flag --build-arg while building the image. We can have multiple ARG instruction in the Dockerfile. ARG is the only instruction that can precede the FROM instruction in the Dockerfile.
The ARG directive in Dockerfile defines the parameter name and defines its default value. This default value can be overridden by the --build-arg <parameter name>=<value> in the build command docker build .
#6 Using ENTRYPOINT with CMD Still, they both can be used in your Docker file. There are many such cases where we can use both ENTRYPOINT and CMD. The thing is that you will have to define the executable with the ENTRYPOINT and the default parameters using the CMD command. Maintain them in exec form at all times.
Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.
Then you can use --build-arg
, it will pass parameters with --build-arg key=value
to dockerfile when build, refer to this.
You just need to use sed
to get from your env file & combine them to the format --build-arg key=value
when build the dockerfile, example as next:
Dockefile:
FROM ubuntu:16.04
ARG BuildMode
ENV BuildMode=${BuildMode}
RUN echo $BuildMode
docker.env:
BuildMode="release"
Command:
docker build -t abc:1 $(cat docker.env | sed 's@^@--build-arg @g' | paste -s -d " ") . --no-cache
Output:
shubuntu1@shubuntu1:~/1$ docker build -t abc:1 $(cat docker.env | sed 's@^@--build-arg @g' | paste -s -d " ") . --no-cache
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM ubuntu:16.04
---> 13c9f1285025
Step 2/4 : ARG BuildMode
---> Running in 3bc49fbb0af4
Removing intermediate container 3bc49fbb0af4
Step 3/4 : ENV BuildMode=${BuildMode}
---> Running in 4c253fba0b36
Removing intermediate container 4c253fba0b36
---> c70f7f535d1f
Step 4/4 : RUN echo $BuildMode
---> Running in 5fef72f28975
"release"
Removing intermediate container 5fef72f28975
---> 4b5555223b5b
Successfully built 4b5555223b5b
Successfully tagged abc:1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With