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 :
2
Answers
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:
to this:
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.: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
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.