Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass argument to dockerfile from a file

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.

Dockerfile

COPY docker.env .
RUN /bin/bash -c "source docker.env"

Docker.env

BuildMode="release"

another try which also did not work

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.

like image 429
jorence jovin Avatar asked Jul 18 '19 09:07

jorence jovin


People also ask

How do I pass ARG in Dockerfile?

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.

What is ARG in 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 .

Can I have ENTRYPOINT and CMD in Dockerfile?

#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.

Can I use ENV variable in Dockerfile?

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.


1 Answers

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
like image 188
atline Avatar answered Sep 28 '22 05:09

atline