-1

How to run sed command and save the result to one new Variable in docker. The sed will replace the last occurrence of '.' and replace with '_'

Example :

JOB_NAME_WITH_VERSION = test_git_0.1 and wanted result is ZIP_FILE_NAME = test_git_0_1

--Dockerfile
    
RUN ZIP_FILE_NAME=$(echo ${JOB_NAME_WITH_VERSION} | sed 's/\(.*\)\./\1_/') && export ZIP_FILE_NAME 
        
RUN echo "Zip file Name found : $ZIP_FILE_NAME"

I tried this in my docker file but the result is empty

Zip file Name found :

Safus09
  • 89
  • 2
  • 12
  • 1
    The environment is reset after each `RUN` line, so `RUN export` has no effect; [docker ENV vs RUN export](https://stackoverflow.com/questions/33379393/docker-env-vs-run-export) has one example of this. But if you combine the two `RUN` lines into one, the variable will remain valid until the end of the `RUN` line. (You don't need to `export` it here.) – David Maze Jan 21 '23 at 18:03
  • Did my answer work for you? – Paolo Jan 24 '23 at 19:38

2 Answers2

1

Each RUN statement in a Dockerfile is run in a separate shell. So once a statement is done, all environment variables are lost. Even if they are exported.

To do what you want to do, you can combine your RUN statements like this

RUN ZIP_FILE_NAME=$(echo ${JOB_NAME_WITH_VERSION} | sed 's/\(.*\)\./\1_/') && \
    export ZIP_FILE_NAME && \
    echo "Zip file Name found : $ZIP_FILE_NAME"

As your variable is lost once the RUN statement is finished, your environment variable won't be available in your container when it runs. To have an environment variable available there, you need to use the ENV statement.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • Thanks for the answer. what is the right way to "change" a variable used in RUN ARG to ENV variable ? – Safus09 Jan 21 '23 at 18:59
  • You can't. ENV variables are meant to be used to configure the container at run-time, so whoever runs it supplies the values needed. If you need to do some work on startup, you can do it in a script that's run using ENTRYPOINT or CMD. – Hans Kilian Jan 21 '23 at 19:15
0

The issue here is that every RUN command results in a new layer, so whatever shell variable was declared in previous layers is subsequently lost.

Compare this:

FROM ubuntu
RUN JOB="FOOBAR"
RUN echo "${JOB}"
$ docker build .
...
Step 3/3 : RUN echo "${JOB}"
 ---> Running in c4b7d1632c7e

...

to this:

FROM ubuntu
RUN JOB="FOOBAR" && echo "${JOB}"
$ docker build .
...
Step 2/2 : RUN JOB="FOOBAR" && echo "${JOB}"
 ---> Running in c11049d1687f
FOOBAR
...

so as a workaround, if using a single RUN command is not an option for whatever reason, write the variable to disk and read it when needed, e.g.:

FROM ubuntu
RUN JOB="FOOBAR" && echo "${JOB}" > /tmp/job_var
RUN cat /tmp/job_var
$ docker build .
...
Step 3/3 : RUN cat /tmp/job_var
 ---> Running in a346c30c2cd5
FOOBAR
...
Paolo
  • 21,270
  • 6
  • 38
  • 69