1

I have a script.sh like that

DATE="R_$(date +%Y_%m_%d__%H_%M_%S)"
export DISTDIR="$BUILDDIR/$DATE"

and i wan't to pass this DISTDIR in my Dockerfile like that

COPY build/$DISTDIR/ "$CATALINA_HOME"/webapps/ws/js/

How i do that, i searched for various modes and none worked

Bruno Luis
  • 25
  • 10
  • Don't work for me – Bruno Luis Dec 19 '18 at 21:28
  • 1
    Why doesn't it work for you? – that other guy Dec 19 '18 at 21:34
  • I do not want to set a variable in docker run, I just want to get the variable set in script.sh and pass as parameter to my Dockerfile – Bruno Luis Dec 20 '18 at 15:46
  • Where does `script.sh` run? Inside Docker or outside? – that other guy Dec 20 '18 at 16:54
  • **script.sh** was an example, but on occasion he would be outside Docker – Bruno Luis Dec 20 '18 at 16:58
  • Then I think you may have skimmed the question too quickly. It's true that their use case is setting a variable, but the question asked and answered is how to *get* a variable defined in a script that invokes `docker build`, and to use its value inside the Dockerfile. – that other guy Dec 20 '18 at 17:46
  • I understand the question, it's exactly what I want but it uses **dotenv** and I do not, because my **DISTDIR** variable will not be static, it will be dynamic ... with each build my it will change, if there is a way I can only change the **DISTDIR** variable From the script.sh to an .env file, yes it would be useful to ask, since my script.sh file contains much more information than the **DISTDIR** variable, but the only one I will pass to the **dockerfile** is it – Bruno Luis Dec 20 '18 at 18:14
  • An environment variable is an environment variable. It doesn't matter if you set it with `export var=value` or `declare -x var=value` or `source myfile.env`. – that other guy Dec 20 '18 at 18:19
  • I tried the solution and it hangs at the time of COPY build / $ DISTDIR / "$ CATALINA_HOME" / webapps / ws / js / until I give a Ctrl + C to stop the build – Bruno Luis Dec 20 '18 at 18:37
  • Does `RUN echo "Copying $DISTDIR"` show the correct value? – that other guy Dec 20 '18 at 19:02

1 Answers1

0

The problem is passing the environment variable, not how you set it (dotenv or not) or how you use it (to set a variable or COPY).

Here's a complete demo with the technique from the duplicate. This is script.sh:

#!/bin/bash
mycustomvar="file.$RANDOM"
touch "$mycustomvar"
echo "I am running outside Docker with mycustomvar=$mycustomvar"
docker build --build-arg mycustomvar="$mycustomvar" .

And a Dockerfile:

FROM alpine
ARG mycustomvar
RUN echo "I am running in docker with mycustomvar=$mycustomvar"
COPY $mycustomvar /tmp/
RUN ls /tmp

Here's what happens when you run it:

$ sudo ./script.sh
I am running outside Docker with mycustomvar=file.10518
Sending build context to Docker daemon  17.41kB
Step 1/5 : FROM alpine
 ---> 3fd9065eaf02
Step 2/5 : ARG mycustomvar
 ---> Using cache
 ---> a6dfa6001164
Step 3/5 : RUN echo "I am running in docker with mycustomvar=$mycustomvar"
 ---> Running in e958044bfd11
I am running in docker with mycustomvar=file.10518
 ---> 95c107e49291
Removing intermediate container e958044bfd11
Step 4/5 : COPY $mycustomvar /tmp/
 ---> d36445b49261
Removing intermediate container e3ac014d1ba9
Step 5/5 : RUN ls /tmp
 ---> Running in 590409a81df5
file.10518
 ---> d734f83cc8ec
Removing intermediate container 590409a81df5
Successfully built d734f83cc8ec

As you can see, script.sh sets a variable, and the RUN and COPY statements both have access to it.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • after many attempts I got ... as I had said the script.sh was an example where there were more scripts involved, and the error was in the passage of variables among these scripts, but finally I did, thank you very much, you helped me very much – Bruno Luis Dec 20 '18 at 19:59