Dockerfile capture output of a command










0















I have the following line in my Dockerfile which is supposed to capture the display number of the host:



RUN DISPLAY_NUMBER="$(echo $DISPLAY | cut -d. -f1 | cut -d: -f2)" && echo $DISPLAY_NUMBER


When I tried to build the Dockerfile, the DISPLAY_NUMBER is empty. But however when I run the same command directly in the terminal I get the see the result. Is there anything that I'm doing wrong here?










share|improve this question






















  • Is the $DISPLAY variable empty when you run docker build?

    – Ricardo Branco
    Nov 13 '18 at 19:34











  • Yes! The DISPLAY_NUMBER is empty!

    – sparkr
    Nov 13 '18 at 19:41











  • You can’t put that in a Dockerfile. What would happen if you ran the image from a different host, or (given your other questions) from a different ssh session with a different $DISPLAY?

    – David Maze
    Nov 13 '18 at 19:48











  • What do you want to achieve with this command?

    – mbuechmann
    Nov 13 '18 at 19:50















0















I have the following line in my Dockerfile which is supposed to capture the display number of the host:



RUN DISPLAY_NUMBER="$(echo $DISPLAY | cut -d. -f1 | cut -d: -f2)" && echo $DISPLAY_NUMBER


When I tried to build the Dockerfile, the DISPLAY_NUMBER is empty. But however when I run the same command directly in the terminal I get the see the result. Is there anything that I'm doing wrong here?










share|improve this question






















  • Is the $DISPLAY variable empty when you run docker build?

    – Ricardo Branco
    Nov 13 '18 at 19:34











  • Yes! The DISPLAY_NUMBER is empty!

    – sparkr
    Nov 13 '18 at 19:41











  • You can’t put that in a Dockerfile. What would happen if you ran the image from a different host, or (given your other questions) from a different ssh session with a different $DISPLAY?

    – David Maze
    Nov 13 '18 at 19:48











  • What do you want to achieve with this command?

    – mbuechmann
    Nov 13 '18 at 19:50













0












0








0








I have the following line in my Dockerfile which is supposed to capture the display number of the host:



RUN DISPLAY_NUMBER="$(echo $DISPLAY | cut -d. -f1 | cut -d: -f2)" && echo $DISPLAY_NUMBER


When I tried to build the Dockerfile, the DISPLAY_NUMBER is empty. But however when I run the same command directly in the terminal I get the see the result. Is there anything that I'm doing wrong here?










share|improve this question














I have the following line in my Dockerfile which is supposed to capture the display number of the host:



RUN DISPLAY_NUMBER="$(echo $DISPLAY | cut -d. -f1 | cut -d: -f2)" && echo $DISPLAY_NUMBER


When I tried to build the Dockerfile, the DISPLAY_NUMBER is empty. But however when I run the same command directly in the terminal I get the see the result. Is there anything that I'm doing wrong here?







docker






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 19:26









sparkrsparkr

5,0731243108




5,0731243108












  • Is the $DISPLAY variable empty when you run docker build?

    – Ricardo Branco
    Nov 13 '18 at 19:34











  • Yes! The DISPLAY_NUMBER is empty!

    – sparkr
    Nov 13 '18 at 19:41











  • You can’t put that in a Dockerfile. What would happen if you ran the image from a different host, or (given your other questions) from a different ssh session with a different $DISPLAY?

    – David Maze
    Nov 13 '18 at 19:48











  • What do you want to achieve with this command?

    – mbuechmann
    Nov 13 '18 at 19:50

















  • Is the $DISPLAY variable empty when you run docker build?

    – Ricardo Branco
    Nov 13 '18 at 19:34











  • Yes! The DISPLAY_NUMBER is empty!

    – sparkr
    Nov 13 '18 at 19:41











  • You can’t put that in a Dockerfile. What would happen if you ran the image from a different host, or (given your other questions) from a different ssh session with a different $DISPLAY?

    – David Maze
    Nov 13 '18 at 19:48











  • What do you want to achieve with this command?

    – mbuechmann
    Nov 13 '18 at 19:50
















Is the $DISPLAY variable empty when you run docker build?

– Ricardo Branco
Nov 13 '18 at 19:34





Is the $DISPLAY variable empty when you run docker build?

– Ricardo Branco
Nov 13 '18 at 19:34













Yes! The DISPLAY_NUMBER is empty!

– sparkr
Nov 13 '18 at 19:41





Yes! The DISPLAY_NUMBER is empty!

– sparkr
Nov 13 '18 at 19:41













You can’t put that in a Dockerfile. What would happen if you ran the image from a different host, or (given your other questions) from a different ssh session with a different $DISPLAY?

– David Maze
Nov 13 '18 at 19:48





You can’t put that in a Dockerfile. What would happen if you ran the image from a different host, or (given your other questions) from a different ssh session with a different $DISPLAY?

– David Maze
Nov 13 '18 at 19:48













What do you want to achieve with this command?

– mbuechmann
Nov 13 '18 at 19:50





What do you want to achieve with this command?

– mbuechmann
Nov 13 '18 at 19:50












2 Answers
2






active

oldest

votes


















1














Host environment variables cannot be passed during build, only at run-time.



Only build args can be specified by:



first "declaring the arg"



ARG DISPLAY_NUMBER


and then running

docker build . --no-cache -t disp --build-arg DISPLAY_NUMBER=$DISPLAY_NUMBER



You can work around this issue using the envsubst trick



RUN echo $DISPLAY_NUMBER


And on the command line:



envsubst < Dockerfile | docker build . -f -


Which will rewrite the Dockerfile in memory and pass it to Docker with the environment variable changed.



Edit: Note that this solution is pretty useless though, because you probably
want to do this during run-time anyways, because this value should depend on not on where the image is built, but rather where it is run.



I would personally move that logic into your ENTRYPOINT or CMD script.






share|improve this answer

























  • That will pass the display number of the machine where the image is built and print it out during build. I doubt that this makes any sense. If I wanted to know the display number of my current machine, I would just run the command there.

    – mbuechmann
    Nov 13 '18 at 19:49











  • I did not really consider the OP's intent, so in that sense your answer is probably more useful from a practical perspective. Although your answer has a small inaccuracy though, regarding as to why the variable is blank.

    – Uku Loskit
    Nov 13 '18 at 19:53


















1














Commands specified with RUN are executed when the image is built. There is no display during build hence the output is empty.



You can exchange RUN with ENTRYPOINT then the command is executed when the docker starts.



But how to forward the hosts display to the container is another matter entirely.






share|improve this answer
























    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',
    autoActivateHeartbeat: false,
    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
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288189%2fdockerfile-capture-output-of-a-command%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Host environment variables cannot be passed during build, only at run-time.



    Only build args can be specified by:



    first "declaring the arg"



    ARG DISPLAY_NUMBER


    and then running

    docker build . --no-cache -t disp --build-arg DISPLAY_NUMBER=$DISPLAY_NUMBER



    You can work around this issue using the envsubst trick



    RUN echo $DISPLAY_NUMBER


    And on the command line:



    envsubst < Dockerfile | docker build . -f -


    Which will rewrite the Dockerfile in memory and pass it to Docker with the environment variable changed.



    Edit: Note that this solution is pretty useless though, because you probably
    want to do this during run-time anyways, because this value should depend on not on where the image is built, but rather where it is run.



    I would personally move that logic into your ENTRYPOINT or CMD script.






    share|improve this answer

























    • That will pass the display number of the machine where the image is built and print it out during build. I doubt that this makes any sense. If I wanted to know the display number of my current machine, I would just run the command there.

      – mbuechmann
      Nov 13 '18 at 19:49











    • I did not really consider the OP's intent, so in that sense your answer is probably more useful from a practical perspective. Although your answer has a small inaccuracy though, regarding as to why the variable is blank.

      – Uku Loskit
      Nov 13 '18 at 19:53















    1














    Host environment variables cannot be passed during build, only at run-time.



    Only build args can be specified by:



    first "declaring the arg"



    ARG DISPLAY_NUMBER


    and then running

    docker build . --no-cache -t disp --build-arg DISPLAY_NUMBER=$DISPLAY_NUMBER



    You can work around this issue using the envsubst trick



    RUN echo $DISPLAY_NUMBER


    And on the command line:



    envsubst < Dockerfile | docker build . -f -


    Which will rewrite the Dockerfile in memory and pass it to Docker with the environment variable changed.



    Edit: Note that this solution is pretty useless though, because you probably
    want to do this during run-time anyways, because this value should depend on not on where the image is built, but rather where it is run.



    I would personally move that logic into your ENTRYPOINT or CMD script.






    share|improve this answer

























    • That will pass the display number of the machine where the image is built and print it out during build. I doubt that this makes any sense. If I wanted to know the display number of my current machine, I would just run the command there.

      – mbuechmann
      Nov 13 '18 at 19:49











    • I did not really consider the OP's intent, so in that sense your answer is probably more useful from a practical perspective. Although your answer has a small inaccuracy though, regarding as to why the variable is blank.

      – Uku Loskit
      Nov 13 '18 at 19:53













    1












    1








    1







    Host environment variables cannot be passed during build, only at run-time.



    Only build args can be specified by:



    first "declaring the arg"



    ARG DISPLAY_NUMBER


    and then running

    docker build . --no-cache -t disp --build-arg DISPLAY_NUMBER=$DISPLAY_NUMBER



    You can work around this issue using the envsubst trick



    RUN echo $DISPLAY_NUMBER


    And on the command line:



    envsubst < Dockerfile | docker build . -f -


    Which will rewrite the Dockerfile in memory and pass it to Docker with the environment variable changed.



    Edit: Note that this solution is pretty useless though, because you probably
    want to do this during run-time anyways, because this value should depend on not on where the image is built, but rather where it is run.



    I would personally move that logic into your ENTRYPOINT or CMD script.






    share|improve this answer















    Host environment variables cannot be passed during build, only at run-time.



    Only build args can be specified by:



    first "declaring the arg"



    ARG DISPLAY_NUMBER


    and then running

    docker build . --no-cache -t disp --build-arg DISPLAY_NUMBER=$DISPLAY_NUMBER



    You can work around this issue using the envsubst trick



    RUN echo $DISPLAY_NUMBER


    And on the command line:



    envsubst < Dockerfile | docker build . -f -


    Which will rewrite the Dockerfile in memory and pass it to Docker with the environment variable changed.



    Edit: Note that this solution is pretty useless though, because you probably
    want to do this during run-time anyways, because this value should depend on not on where the image is built, but rather where it is run.



    I would personally move that logic into your ENTRYPOINT or CMD script.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 19:59

























    answered Nov 13 '18 at 19:44









    Uku LoskitUku Loskit

    30.3k86879




    30.3k86879












    • That will pass the display number of the machine where the image is built and print it out during build. I doubt that this makes any sense. If I wanted to know the display number of my current machine, I would just run the command there.

      – mbuechmann
      Nov 13 '18 at 19:49











    • I did not really consider the OP's intent, so in that sense your answer is probably more useful from a practical perspective. Although your answer has a small inaccuracy though, regarding as to why the variable is blank.

      – Uku Loskit
      Nov 13 '18 at 19:53

















    • That will pass the display number of the machine where the image is built and print it out during build. I doubt that this makes any sense. If I wanted to know the display number of my current machine, I would just run the command there.

      – mbuechmann
      Nov 13 '18 at 19:49











    • I did not really consider the OP's intent, so in that sense your answer is probably more useful from a practical perspective. Although your answer has a small inaccuracy though, regarding as to why the variable is blank.

      – Uku Loskit
      Nov 13 '18 at 19:53
















    That will pass the display number of the machine where the image is built and print it out during build. I doubt that this makes any sense. If I wanted to know the display number of my current machine, I would just run the command there.

    – mbuechmann
    Nov 13 '18 at 19:49





    That will pass the display number of the machine where the image is built and print it out during build. I doubt that this makes any sense. If I wanted to know the display number of my current machine, I would just run the command there.

    – mbuechmann
    Nov 13 '18 at 19:49













    I did not really consider the OP's intent, so in that sense your answer is probably more useful from a practical perspective. Although your answer has a small inaccuracy though, regarding as to why the variable is blank.

    – Uku Loskit
    Nov 13 '18 at 19:53





    I did not really consider the OP's intent, so in that sense your answer is probably more useful from a practical perspective. Although your answer has a small inaccuracy though, regarding as to why the variable is blank.

    – Uku Loskit
    Nov 13 '18 at 19:53













    1














    Commands specified with RUN are executed when the image is built. There is no display during build hence the output is empty.



    You can exchange RUN with ENTRYPOINT then the command is executed when the docker starts.



    But how to forward the hosts display to the container is another matter entirely.






    share|improve this answer





























      1














      Commands specified with RUN are executed when the image is built. There is no display during build hence the output is empty.



      You can exchange RUN with ENTRYPOINT then the command is executed when the docker starts.



      But how to forward the hosts display to the container is another matter entirely.






      share|improve this answer



























        1












        1








        1







        Commands specified with RUN are executed when the image is built. There is no display during build hence the output is empty.



        You can exchange RUN with ENTRYPOINT then the command is executed when the docker starts.



        But how to forward the hosts display to the container is another matter entirely.






        share|improve this answer















        Commands specified with RUN are executed when the image is built. There is no display during build hence the output is empty.



        You can exchange RUN with ENTRYPOINT then the command is executed when the docker starts.



        But how to forward the hosts display to the container is another matter entirely.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 19:44

























        answered Nov 13 '18 at 19:35









        mbuechmannmbuechmann

        2,82121325




        2,82121325



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288189%2fdockerfile-capture-output-of-a-command%23new-answer', 'question_page');

            );

            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







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric