2

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

10
  • Don't work for me Commented Dec 19, 2018 at 21:28
  • 1
    Why doesn't it work for you? Commented Dec 19, 2018 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 Commented Dec 20, 2018 at 15:46
  • Where does script.sh run? Inside Docker or outside? Commented Dec 20, 2018 at 16:54
  • script.sh was an example, but on occasion he would be outside Docker Commented Dec 20, 2018 at 16:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.