Dockerfile - setting ENV variable from output of .sh script
up vote
0
down vote
favorite
I am working to "dockerize" a build process and one of the things I need to figure out is how to set an ENV variable based on the output of a script. Essentially all the script does is check for branch name - if master return foo, else return bar
sort of thing.
I have tried a couple of things: export - which I know does not stick, I need an variable that persists. I have tried something like this: RUN MY_VAR=$(/path/to/my/script/script.sh)
with no success.
This needs to happen during the build stage, not after it the image has been built. These ENV variables need to be set for when the docker image is run.
Thanks for any assistance that you may be able to offer.
docker dockerfile
add a comment |
up vote
0
down vote
favorite
I am working to "dockerize" a build process and one of the things I need to figure out is how to set an ENV variable based on the output of a script. Essentially all the script does is check for branch name - if master return foo, else return bar
sort of thing.
I have tried a couple of things: export - which I know does not stick, I need an variable that persists. I have tried something like this: RUN MY_VAR=$(/path/to/my/script/script.sh)
with no success.
This needs to happen during the build stage, not after it the image has been built. These ENV variables need to be set for when the docker image is run.
Thanks for any assistance that you may be able to offer.
docker dockerfile
Is this script independent? Or does it need to be run from inside Dockerfile?
– Raoslaw Szamszur
Nov 9 at 15:43
It needs to be run from within the dockerfile. Essentially the reason for this is I want to have the image and the proper env variables setup. The way this project was developed is that ifbranch=master
setup variables this way, ifbranch=anything else
then setup the variables another way. and these scripts were written for this exact purpose. I was trying to avoid doing the passing in of the variables when the image is being run. and I may not be able to do what I want.
– Dubl1n
Nov 9 at 21:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am working to "dockerize" a build process and one of the things I need to figure out is how to set an ENV variable based on the output of a script. Essentially all the script does is check for branch name - if master return foo, else return bar
sort of thing.
I have tried a couple of things: export - which I know does not stick, I need an variable that persists. I have tried something like this: RUN MY_VAR=$(/path/to/my/script/script.sh)
with no success.
This needs to happen during the build stage, not after it the image has been built. These ENV variables need to be set for when the docker image is run.
Thanks for any assistance that you may be able to offer.
docker dockerfile
I am working to "dockerize" a build process and one of the things I need to figure out is how to set an ENV variable based on the output of a script. Essentially all the script does is check for branch name - if master return foo, else return bar
sort of thing.
I have tried a couple of things: export - which I know does not stick, I need an variable that persists. I have tried something like this: RUN MY_VAR=$(/path/to/my/script/script.sh)
with no success.
This needs to happen during the build stage, not after it the image has been built. These ENV variables need to be set for when the docker image is run.
Thanks for any assistance that you may be able to offer.
docker dockerfile
docker dockerfile
edited Nov 9 at 16:19
Raoslaw Szamszur
897415
897415
asked Nov 9 at 15:35
Dubl1n
326
326
Is this script independent? Or does it need to be run from inside Dockerfile?
– Raoslaw Szamszur
Nov 9 at 15:43
It needs to be run from within the dockerfile. Essentially the reason for this is I want to have the image and the proper env variables setup. The way this project was developed is that ifbranch=master
setup variables this way, ifbranch=anything else
then setup the variables another way. and these scripts were written for this exact purpose. I was trying to avoid doing the passing in of the variables when the image is being run. and I may not be able to do what I want.
– Dubl1n
Nov 9 at 21:23
add a comment |
Is this script independent? Or does it need to be run from inside Dockerfile?
– Raoslaw Szamszur
Nov 9 at 15:43
It needs to be run from within the dockerfile. Essentially the reason for this is I want to have the image and the proper env variables setup. The way this project was developed is that ifbranch=master
setup variables this way, ifbranch=anything else
then setup the variables another way. and these scripts were written for this exact purpose. I was trying to avoid doing the passing in of the variables when the image is being run. and I may not be able to do what I want.
– Dubl1n
Nov 9 at 21:23
Is this script independent? Or does it need to be run from inside Dockerfile?
– Raoslaw Szamszur
Nov 9 at 15:43
Is this script independent? Or does it need to be run from inside Dockerfile?
– Raoslaw Szamszur
Nov 9 at 15:43
It needs to be run from within the dockerfile. Essentially the reason for this is I want to have the image and the proper env variables setup. The way this project was developed is that if
branch=master
setup variables this way, if branch=anything else
then setup the variables another way. and these scripts were written for this exact purpose. I was trying to avoid doing the passing in of the variables when the image is being run. and I may not be able to do what I want.– Dubl1n
Nov 9 at 21:23
It needs to be run from within the dockerfile. Essentially the reason for this is I want to have the image and the proper env variables setup. The way this project was developed is that if
branch=master
setup variables this way, if branch=anything else
then setup the variables another way. and these scripts were written for this exact purpose. I was trying to avoid doing the passing in of the variables when the image is being run. and I may not be able to do what I want.– Dubl1n
Nov 9 at 21:23
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Directly for your question, there is some kind of workaround.
You can save output of your script anywhere at the filesystem like
RUN /path/to/my/script/script.sh > /opt/myvalue
And in entrypoint or cmd script use like this
export MY_VAR=$(echo /opt/myvalue)
But If you provide more detail why you use script like this and for what need this variable, maybe there will more good solution
So what I ended up doing was something similar:RUN script that gets latest.sh > /tmp/LATEST_VERSION
RUN LATEST_VERSION=$(cat /tmp/LATEST_VERSION); run my other process that is dependent on this variable.sh && do this other thing with that variable.sh
So far it seems to work, just thought there was a more elegant way of doing it.
– Dubl1n
Nov 9 at 21:31
I think more elegant way would be use —build-arg and ARG notation. Set this variable during the build with output of your script and then set it with ENV to permanent usage like describe in this article vsupalov.com/docker-build-pass-environment-variables
– Kealman
Nov 10 at 23:04
Thanks! I will take a look at that. I like the way that looks. Appreciate it!
– Dubl1n
Nov 13 at 15:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228763%2fdockerfile-setting-env-variable-from-output-of-sh-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Directly for your question, there is some kind of workaround.
You can save output of your script anywhere at the filesystem like
RUN /path/to/my/script/script.sh > /opt/myvalue
And in entrypoint or cmd script use like this
export MY_VAR=$(echo /opt/myvalue)
But If you provide more detail why you use script like this and for what need this variable, maybe there will more good solution
So what I ended up doing was something similar:RUN script that gets latest.sh > /tmp/LATEST_VERSION
RUN LATEST_VERSION=$(cat /tmp/LATEST_VERSION); run my other process that is dependent on this variable.sh && do this other thing with that variable.sh
So far it seems to work, just thought there was a more elegant way of doing it.
– Dubl1n
Nov 9 at 21:31
I think more elegant way would be use —build-arg and ARG notation. Set this variable during the build with output of your script and then set it with ENV to permanent usage like describe in this article vsupalov.com/docker-build-pass-environment-variables
– Kealman
Nov 10 at 23:04
Thanks! I will take a look at that. I like the way that looks. Appreciate it!
– Dubl1n
Nov 13 at 15:18
add a comment |
up vote
0
down vote
Directly for your question, there is some kind of workaround.
You can save output of your script anywhere at the filesystem like
RUN /path/to/my/script/script.sh > /opt/myvalue
And in entrypoint or cmd script use like this
export MY_VAR=$(echo /opt/myvalue)
But If you provide more detail why you use script like this and for what need this variable, maybe there will more good solution
So what I ended up doing was something similar:RUN script that gets latest.sh > /tmp/LATEST_VERSION
RUN LATEST_VERSION=$(cat /tmp/LATEST_VERSION); run my other process that is dependent on this variable.sh && do this other thing with that variable.sh
So far it seems to work, just thought there was a more elegant way of doing it.
– Dubl1n
Nov 9 at 21:31
I think more elegant way would be use —build-arg and ARG notation. Set this variable during the build with output of your script and then set it with ENV to permanent usage like describe in this article vsupalov.com/docker-build-pass-environment-variables
– Kealman
Nov 10 at 23:04
Thanks! I will take a look at that. I like the way that looks. Appreciate it!
– Dubl1n
Nov 13 at 15:18
add a comment |
up vote
0
down vote
up vote
0
down vote
Directly for your question, there is some kind of workaround.
You can save output of your script anywhere at the filesystem like
RUN /path/to/my/script/script.sh > /opt/myvalue
And in entrypoint or cmd script use like this
export MY_VAR=$(echo /opt/myvalue)
But If you provide more detail why you use script like this and for what need this variable, maybe there will more good solution
Directly for your question, there is some kind of workaround.
You can save output of your script anywhere at the filesystem like
RUN /path/to/my/script/script.sh > /opt/myvalue
And in entrypoint or cmd script use like this
export MY_VAR=$(echo /opt/myvalue)
But If you provide more detail why you use script like this and for what need this variable, maybe there will more good solution
edited Nov 9 at 16:19
answered Nov 9 at 16:14
Kealman
515
515
So what I ended up doing was something similar:RUN script that gets latest.sh > /tmp/LATEST_VERSION
RUN LATEST_VERSION=$(cat /tmp/LATEST_VERSION); run my other process that is dependent on this variable.sh && do this other thing with that variable.sh
So far it seems to work, just thought there was a more elegant way of doing it.
– Dubl1n
Nov 9 at 21:31
I think more elegant way would be use —build-arg and ARG notation. Set this variable during the build with output of your script and then set it with ENV to permanent usage like describe in this article vsupalov.com/docker-build-pass-environment-variables
– Kealman
Nov 10 at 23:04
Thanks! I will take a look at that. I like the way that looks. Appreciate it!
– Dubl1n
Nov 13 at 15:18
add a comment |
So what I ended up doing was something similar:RUN script that gets latest.sh > /tmp/LATEST_VERSION
RUN LATEST_VERSION=$(cat /tmp/LATEST_VERSION); run my other process that is dependent on this variable.sh && do this other thing with that variable.sh
So far it seems to work, just thought there was a more elegant way of doing it.
– Dubl1n
Nov 9 at 21:31
I think more elegant way would be use —build-arg and ARG notation. Set this variable during the build with output of your script and then set it with ENV to permanent usage like describe in this article vsupalov.com/docker-build-pass-environment-variables
– Kealman
Nov 10 at 23:04
Thanks! I will take a look at that. I like the way that looks. Appreciate it!
– Dubl1n
Nov 13 at 15:18
So what I ended up doing was something similar:
RUN script that gets latest.sh > /tmp/LATEST_VERSION
RUN LATEST_VERSION=$(cat /tmp/LATEST_VERSION); run my other process that is dependent on this variable.sh && do this other thing with that variable.sh
So far it seems to work, just thought there was a more elegant way of doing it.– Dubl1n
Nov 9 at 21:31
So what I ended up doing was something similar:
RUN script that gets latest.sh > /tmp/LATEST_VERSION
RUN LATEST_VERSION=$(cat /tmp/LATEST_VERSION); run my other process that is dependent on this variable.sh && do this other thing with that variable.sh
So far it seems to work, just thought there was a more elegant way of doing it.– Dubl1n
Nov 9 at 21:31
I think more elegant way would be use —build-arg and ARG notation. Set this variable during the build with output of your script and then set it with ENV to permanent usage like describe in this article vsupalov.com/docker-build-pass-environment-variables
– Kealman
Nov 10 at 23:04
I think more elegant way would be use —build-arg and ARG notation. Set this variable during the build with output of your script and then set it with ENV to permanent usage like describe in this article vsupalov.com/docker-build-pass-environment-variables
– Kealman
Nov 10 at 23:04
Thanks! I will take a look at that. I like the way that looks. Appreciate it!
– Dubl1n
Nov 13 at 15:18
Thanks! I will take a look at that. I like the way that looks. Appreciate it!
– Dubl1n
Nov 13 at 15:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228763%2fdockerfile-setting-env-variable-from-output-of-sh-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is this script independent? Or does it need to be run from inside Dockerfile?
– Raoslaw Szamszur
Nov 9 at 15:43
It needs to be run from within the dockerfile. Essentially the reason for this is I want to have the image and the proper env variables setup. The way this project was developed is that if
branch=master
setup variables this way, ifbranch=anything else
then setup the variables another way. and these scripts were written for this exact purpose. I was trying to avoid doing the passing in of the variables when the image is being run. and I may not be able to do what I want.– Dubl1n
Nov 9 at 21:23