Run a script in Dockerfile












46















I'm trying to run a script during my building process in my Dockerfile.
But it doesn't seems to work.



I tried that way:



FROM php:7-fpm
ADD bootstrap.sh /
ENTRYPOINT ["/bin/bash", "/bootstrap.sh"]


Also this way:



FROM php:7-fpm    
ADD bootstrap.sh /
RUN bash -c "/bootstrap.sh"


And also bu executing my running container:



docker exec symfony /bin/bash -c "/bootstrap.sh"


Nothing seems to work.



Do you know how to do it?










share|improve this question

























  • Does bootstarp.sh have the executable bit set?

    – jwodder
    Dec 31 '15 at 18:38











  • Like that RUN chmod +x /bootstarp.sh ?

    – Kevin
    Dec 31 '15 at 18:54






  • 1





    With "does not work", what exactly is happening? Does it show an error? Is the file present inside the image? If you docker exec -it symfony bash inside the container, can you manually run the script, and check its contents? (cat bootstarp.sh)?

    – thaJeztah
    Jan 1 '16 at 2:37











  • try this: docker exec symfony /bin/bash /bootstarp.sh and let me know the output.

    – Boynux
    Jan 1 '16 at 4:26






  • 7





    Ermahgerd bootstarp!

    – Bruno Bronosky
    Jul 13 '17 at 16:27
















46















I'm trying to run a script during my building process in my Dockerfile.
But it doesn't seems to work.



I tried that way:



FROM php:7-fpm
ADD bootstrap.sh /
ENTRYPOINT ["/bin/bash", "/bootstrap.sh"]


Also this way:



FROM php:7-fpm    
ADD bootstrap.sh /
RUN bash -c "/bootstrap.sh"


And also bu executing my running container:



docker exec symfony /bin/bash -c "/bootstrap.sh"


Nothing seems to work.



Do you know how to do it?










share|improve this question

























  • Does bootstarp.sh have the executable bit set?

    – jwodder
    Dec 31 '15 at 18:38











  • Like that RUN chmod +x /bootstarp.sh ?

    – Kevin
    Dec 31 '15 at 18:54






  • 1





    With "does not work", what exactly is happening? Does it show an error? Is the file present inside the image? If you docker exec -it symfony bash inside the container, can you manually run the script, and check its contents? (cat bootstarp.sh)?

    – thaJeztah
    Jan 1 '16 at 2:37











  • try this: docker exec symfony /bin/bash /bootstarp.sh and let me know the output.

    – Boynux
    Jan 1 '16 at 4:26






  • 7





    Ermahgerd bootstarp!

    – Bruno Bronosky
    Jul 13 '17 at 16:27














46












46








46


17






I'm trying to run a script during my building process in my Dockerfile.
But it doesn't seems to work.



I tried that way:



FROM php:7-fpm
ADD bootstrap.sh /
ENTRYPOINT ["/bin/bash", "/bootstrap.sh"]


Also this way:



FROM php:7-fpm    
ADD bootstrap.sh /
RUN bash -c "/bootstrap.sh"


And also bu executing my running container:



docker exec symfony /bin/bash -c "/bootstrap.sh"


Nothing seems to work.



Do you know how to do it?










share|improve this question
















I'm trying to run a script during my building process in my Dockerfile.
But it doesn't seems to work.



I tried that way:



FROM php:7-fpm
ADD bootstrap.sh /
ENTRYPOINT ["/bin/bash", "/bootstrap.sh"]


Also this way:



FROM php:7-fpm    
ADD bootstrap.sh /
RUN bash -c "/bootstrap.sh"


And also bu executing my running container:



docker exec symfony /bin/bash -c "/bootstrap.sh"


Nothing seems to work.



Do you know how to do it?







docker dockerfile






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 6 '17 at 16:23









steampowered

7,48695278




7,48695278










asked Dec 31 '15 at 17:45









KevinKevin

1,68211439




1,68211439













  • Does bootstarp.sh have the executable bit set?

    – jwodder
    Dec 31 '15 at 18:38











  • Like that RUN chmod +x /bootstarp.sh ?

    – Kevin
    Dec 31 '15 at 18:54






  • 1





    With "does not work", what exactly is happening? Does it show an error? Is the file present inside the image? If you docker exec -it symfony bash inside the container, can you manually run the script, and check its contents? (cat bootstarp.sh)?

    – thaJeztah
    Jan 1 '16 at 2:37











  • try this: docker exec symfony /bin/bash /bootstarp.sh and let me know the output.

    – Boynux
    Jan 1 '16 at 4:26






  • 7





    Ermahgerd bootstarp!

    – Bruno Bronosky
    Jul 13 '17 at 16:27



















  • Does bootstarp.sh have the executable bit set?

    – jwodder
    Dec 31 '15 at 18:38











  • Like that RUN chmod +x /bootstarp.sh ?

    – Kevin
    Dec 31 '15 at 18:54






  • 1





    With "does not work", what exactly is happening? Does it show an error? Is the file present inside the image? If you docker exec -it symfony bash inside the container, can you manually run the script, and check its contents? (cat bootstarp.sh)?

    – thaJeztah
    Jan 1 '16 at 2:37











  • try this: docker exec symfony /bin/bash /bootstarp.sh and let me know the output.

    – Boynux
    Jan 1 '16 at 4:26






  • 7





    Ermahgerd bootstarp!

    – Bruno Bronosky
    Jul 13 '17 at 16:27

















Does bootstarp.sh have the executable bit set?

– jwodder
Dec 31 '15 at 18:38





Does bootstarp.sh have the executable bit set?

– jwodder
Dec 31 '15 at 18:38













Like that RUN chmod +x /bootstarp.sh ?

– Kevin
Dec 31 '15 at 18:54





Like that RUN chmod +x /bootstarp.sh ?

– Kevin
Dec 31 '15 at 18:54




1




1





With "does not work", what exactly is happening? Does it show an error? Is the file present inside the image? If you docker exec -it symfony bash inside the container, can you manually run the script, and check its contents? (cat bootstarp.sh)?

– thaJeztah
Jan 1 '16 at 2:37





With "does not work", what exactly is happening? Does it show an error? Is the file present inside the image? If you docker exec -it symfony bash inside the container, can you manually run the script, and check its contents? (cat bootstarp.sh)?

– thaJeztah
Jan 1 '16 at 2:37













try this: docker exec symfony /bin/bash /bootstarp.sh and let me know the output.

– Boynux
Jan 1 '16 at 4:26





try this: docker exec symfony /bin/bash /bootstarp.sh and let me know the output.

– Boynux
Jan 1 '16 at 4:26




7




7





Ermahgerd bootstarp!

– Bruno Bronosky
Jul 13 '17 at 16:27





Ermahgerd bootstarp!

– Bruno Bronosky
Jul 13 '17 at 16:27












3 Answers
3






active

oldest

votes


















62














RUN and ENTRYPOINT are two different way to execute a script.



RUN means it creates an intermediate container, runs the script and freeze the new state of that container in a new intermediate image. The script won't be run after that: your final image is supposed to reflect the result of that script.



ENTRYPOINT means your image (which has not executed the script yet) will create a container, and runs that script.



In both cases, the script needs to be added, and a RUN chmod +x /bootstrap.sh is a good idea.



It should also start with a shebang (like #!/bin/sh)



Considering your script (KevinRaimbaud/docker-symfony/docker/php/bootstarp.sh: a couple of git config --global commands), it would be best to RUN that script once in your Dockerfile, but making sure to use the right user (the global git config file is %HOME%/.gitconfig, which by default is the /root one)



Add to your Dockerfile:



RUN /bootstart.sh


Then, when running a container, check the content of /root/.gitconfig to confirm the script was run.






share|improve this answer





















  • 4





    Also, you might have meant bootstrap.sh instead of bootstarp.sh: see stackoverflow.com/a/1254561/6309

    – VonC
    Jan 1 '16 at 8:08











  • Cool! Works very well with RUN /bootstrap.sh

    – Kevin
    Jan 1 '16 at 9:10













  • The shebang on the first line did it for me, thanks! :)

    – GDICommander
    Feb 9 '17 at 19:50



















20














Try to create script with ADD command and specification of working directory
Like this("script" is the name of script and /root/script.sh is where you want it in the container, it can be different path:



ADD script.sh /root/script.sh


In this case ADD has to come before CMD, if you have one
BTW it's cool way to import scripts to any location in container from host machine



In CMD place [./script]



It should automatically execute your script



You can also specify WORKDIR as /root, then you'l be automatically placed in root, upon starting a container






share|improve this answer

































    6














    In addition to the answers above:



    If you created/edited your .sh script file in Windows, make sure it was saved with line ending in Unix format. By default many editors in Windows will convert Unix line endings to Windows format and Linux will not recognize shebang (#!/bin/sh) at the beginning of the file. So Linux will produce the error message like if there is no shebang.



    Tips:




    • If you use Notepad++, you need to click "Edit/EOL Conversion/UNIX (LF)"

    • If you use Visual Studio, I would suggest installing "End Of Line" plugin.
      Then you can make line endings visible by pressing Ctrl-R, Ctrl-W. And to set Linux style endings you can press Ctrl-R, Ctrl-L. For Windows style, press Ctrl-R, Ctrl-C.






    share|improve this answer


























    • Wow, u saved my day. Thank you for this.

      – m.w.
      Nov 2 '18 at 9:43











    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%2f34549859%2frun-a-script-in-dockerfile%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    62














    RUN and ENTRYPOINT are two different way to execute a script.



    RUN means it creates an intermediate container, runs the script and freeze the new state of that container in a new intermediate image. The script won't be run after that: your final image is supposed to reflect the result of that script.



    ENTRYPOINT means your image (which has not executed the script yet) will create a container, and runs that script.



    In both cases, the script needs to be added, and a RUN chmod +x /bootstrap.sh is a good idea.



    It should also start with a shebang (like #!/bin/sh)



    Considering your script (KevinRaimbaud/docker-symfony/docker/php/bootstarp.sh: a couple of git config --global commands), it would be best to RUN that script once in your Dockerfile, but making sure to use the right user (the global git config file is %HOME%/.gitconfig, which by default is the /root one)



    Add to your Dockerfile:



    RUN /bootstart.sh


    Then, when running a container, check the content of /root/.gitconfig to confirm the script was run.






    share|improve this answer





















    • 4





      Also, you might have meant bootstrap.sh instead of bootstarp.sh: see stackoverflow.com/a/1254561/6309

      – VonC
      Jan 1 '16 at 8:08











    • Cool! Works very well with RUN /bootstrap.sh

      – Kevin
      Jan 1 '16 at 9:10













    • The shebang on the first line did it for me, thanks! :)

      – GDICommander
      Feb 9 '17 at 19:50
















    62














    RUN and ENTRYPOINT are two different way to execute a script.



    RUN means it creates an intermediate container, runs the script and freeze the new state of that container in a new intermediate image. The script won't be run after that: your final image is supposed to reflect the result of that script.



    ENTRYPOINT means your image (which has not executed the script yet) will create a container, and runs that script.



    In both cases, the script needs to be added, and a RUN chmod +x /bootstrap.sh is a good idea.



    It should also start with a shebang (like #!/bin/sh)



    Considering your script (KevinRaimbaud/docker-symfony/docker/php/bootstarp.sh: a couple of git config --global commands), it would be best to RUN that script once in your Dockerfile, but making sure to use the right user (the global git config file is %HOME%/.gitconfig, which by default is the /root one)



    Add to your Dockerfile:



    RUN /bootstart.sh


    Then, when running a container, check the content of /root/.gitconfig to confirm the script was run.






    share|improve this answer





















    • 4





      Also, you might have meant bootstrap.sh instead of bootstarp.sh: see stackoverflow.com/a/1254561/6309

      – VonC
      Jan 1 '16 at 8:08











    • Cool! Works very well with RUN /bootstrap.sh

      – Kevin
      Jan 1 '16 at 9:10













    • The shebang on the first line did it for me, thanks! :)

      – GDICommander
      Feb 9 '17 at 19:50














    62












    62








    62







    RUN and ENTRYPOINT are two different way to execute a script.



    RUN means it creates an intermediate container, runs the script and freeze the new state of that container in a new intermediate image. The script won't be run after that: your final image is supposed to reflect the result of that script.



    ENTRYPOINT means your image (which has not executed the script yet) will create a container, and runs that script.



    In both cases, the script needs to be added, and a RUN chmod +x /bootstrap.sh is a good idea.



    It should also start with a shebang (like #!/bin/sh)



    Considering your script (KevinRaimbaud/docker-symfony/docker/php/bootstarp.sh: a couple of git config --global commands), it would be best to RUN that script once in your Dockerfile, but making sure to use the right user (the global git config file is %HOME%/.gitconfig, which by default is the /root one)



    Add to your Dockerfile:



    RUN /bootstart.sh


    Then, when running a container, check the content of /root/.gitconfig to confirm the script was run.






    share|improve this answer















    RUN and ENTRYPOINT are two different way to execute a script.



    RUN means it creates an intermediate container, runs the script and freeze the new state of that container in a new intermediate image. The script won't be run after that: your final image is supposed to reflect the result of that script.



    ENTRYPOINT means your image (which has not executed the script yet) will create a container, and runs that script.



    In both cases, the script needs to be added, and a RUN chmod +x /bootstrap.sh is a good idea.



    It should also start with a shebang (like #!/bin/sh)



    Considering your script (KevinRaimbaud/docker-symfony/docker/php/bootstarp.sh: a couple of git config --global commands), it would be best to RUN that script once in your Dockerfile, but making sure to use the right user (the global git config file is %HOME%/.gitconfig, which by default is the /root one)



    Add to your Dockerfile:



    RUN /bootstart.sh


    Then, when running a container, check the content of /root/.gitconfig to confirm the script was run.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 11 '18 at 13:16









    Christian

    20.9k1291142




    20.9k1291142










    answered Jan 1 '16 at 7:56









    VonCVonC

    849k29926993258




    849k29926993258








    • 4





      Also, you might have meant bootstrap.sh instead of bootstarp.sh: see stackoverflow.com/a/1254561/6309

      – VonC
      Jan 1 '16 at 8:08











    • Cool! Works very well with RUN /bootstrap.sh

      – Kevin
      Jan 1 '16 at 9:10













    • The shebang on the first line did it for me, thanks! :)

      – GDICommander
      Feb 9 '17 at 19:50














    • 4





      Also, you might have meant bootstrap.sh instead of bootstarp.sh: see stackoverflow.com/a/1254561/6309

      – VonC
      Jan 1 '16 at 8:08











    • Cool! Works very well with RUN /bootstrap.sh

      – Kevin
      Jan 1 '16 at 9:10













    • The shebang on the first line did it for me, thanks! :)

      – GDICommander
      Feb 9 '17 at 19:50








    4




    4





    Also, you might have meant bootstrap.sh instead of bootstarp.sh: see stackoverflow.com/a/1254561/6309

    – VonC
    Jan 1 '16 at 8:08





    Also, you might have meant bootstrap.sh instead of bootstarp.sh: see stackoverflow.com/a/1254561/6309

    – VonC
    Jan 1 '16 at 8:08













    Cool! Works very well with RUN /bootstrap.sh

    – Kevin
    Jan 1 '16 at 9:10







    Cool! Works very well with RUN /bootstrap.sh

    – Kevin
    Jan 1 '16 at 9:10















    The shebang on the first line did it for me, thanks! :)

    – GDICommander
    Feb 9 '17 at 19:50





    The shebang on the first line did it for me, thanks! :)

    – GDICommander
    Feb 9 '17 at 19:50













    20














    Try to create script with ADD command and specification of working directory
    Like this("script" is the name of script and /root/script.sh is where you want it in the container, it can be different path:



    ADD script.sh /root/script.sh


    In this case ADD has to come before CMD, if you have one
    BTW it's cool way to import scripts to any location in container from host machine



    In CMD place [./script]



    It should automatically execute your script



    You can also specify WORKDIR as /root, then you'l be automatically placed in root, upon starting a container






    share|improve this answer






























      20














      Try to create script with ADD command and specification of working directory
      Like this("script" is the name of script and /root/script.sh is where you want it in the container, it can be different path:



      ADD script.sh /root/script.sh


      In this case ADD has to come before CMD, if you have one
      BTW it's cool way to import scripts to any location in container from host machine



      In CMD place [./script]



      It should automatically execute your script



      You can also specify WORKDIR as /root, then you'l be automatically placed in root, upon starting a container






      share|improve this answer




























        20












        20








        20







        Try to create script with ADD command and specification of working directory
        Like this("script" is the name of script and /root/script.sh is where you want it in the container, it can be different path:



        ADD script.sh /root/script.sh


        In this case ADD has to come before CMD, if you have one
        BTW it's cool way to import scripts to any location in container from host machine



        In CMD place [./script]



        It should automatically execute your script



        You can also specify WORKDIR as /root, then you'l be automatically placed in root, upon starting a container






        share|improve this answer















        Try to create script with ADD command and specification of working directory
        Like this("script" is the name of script and /root/script.sh is where you want it in the container, it can be different path:



        ADD script.sh /root/script.sh


        In this case ADD has to come before CMD, if you have one
        BTW it's cool way to import scripts to any location in container from host machine



        In CMD place [./script]



        It should automatically execute your script



        You can also specify WORKDIR as /root, then you'l be automatically placed in root, upon starting a container







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 28 '16 at 13:33

























        answered May 28 '16 at 13:06









        MichaelMichael

        322311




        322311























            6














            In addition to the answers above:



            If you created/edited your .sh script file in Windows, make sure it was saved with line ending in Unix format. By default many editors in Windows will convert Unix line endings to Windows format and Linux will not recognize shebang (#!/bin/sh) at the beginning of the file. So Linux will produce the error message like if there is no shebang.



            Tips:




            • If you use Notepad++, you need to click "Edit/EOL Conversion/UNIX (LF)"

            • If you use Visual Studio, I would suggest installing "End Of Line" plugin.
              Then you can make line endings visible by pressing Ctrl-R, Ctrl-W. And to set Linux style endings you can press Ctrl-R, Ctrl-L. For Windows style, press Ctrl-R, Ctrl-C.






            share|improve this answer


























            • Wow, u saved my day. Thank you for this.

              – m.w.
              Nov 2 '18 at 9:43
















            6














            In addition to the answers above:



            If you created/edited your .sh script file in Windows, make sure it was saved with line ending in Unix format. By default many editors in Windows will convert Unix line endings to Windows format and Linux will not recognize shebang (#!/bin/sh) at the beginning of the file. So Linux will produce the error message like if there is no shebang.



            Tips:




            • If you use Notepad++, you need to click "Edit/EOL Conversion/UNIX (LF)"

            • If you use Visual Studio, I would suggest installing "End Of Line" plugin.
              Then you can make line endings visible by pressing Ctrl-R, Ctrl-W. And to set Linux style endings you can press Ctrl-R, Ctrl-L. For Windows style, press Ctrl-R, Ctrl-C.






            share|improve this answer


























            • Wow, u saved my day. Thank you for this.

              – m.w.
              Nov 2 '18 at 9:43














            6












            6








            6







            In addition to the answers above:



            If you created/edited your .sh script file in Windows, make sure it was saved with line ending in Unix format. By default many editors in Windows will convert Unix line endings to Windows format and Linux will not recognize shebang (#!/bin/sh) at the beginning of the file. So Linux will produce the error message like if there is no shebang.



            Tips:




            • If you use Notepad++, you need to click "Edit/EOL Conversion/UNIX (LF)"

            • If you use Visual Studio, I would suggest installing "End Of Line" plugin.
              Then you can make line endings visible by pressing Ctrl-R, Ctrl-W. And to set Linux style endings you can press Ctrl-R, Ctrl-L. For Windows style, press Ctrl-R, Ctrl-C.






            share|improve this answer















            In addition to the answers above:



            If you created/edited your .sh script file in Windows, make sure it was saved with line ending in Unix format. By default many editors in Windows will convert Unix line endings to Windows format and Linux will not recognize shebang (#!/bin/sh) at the beginning of the file. So Linux will produce the error message like if there is no shebang.



            Tips:




            • If you use Notepad++, you need to click "Edit/EOL Conversion/UNIX (LF)"

            • If you use Visual Studio, I would suggest installing "End Of Line" plugin.
              Then you can make line endings visible by pressing Ctrl-R, Ctrl-W. And to set Linux style endings you can press Ctrl-R, Ctrl-L. For Windows style, press Ctrl-R, Ctrl-C.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 1:18

























            answered Jul 12 '18 at 6:11









            VeganHunterVeganHunter

            1,82921110




            1,82921110













            • Wow, u saved my day. Thank you for this.

              – m.w.
              Nov 2 '18 at 9:43



















            • Wow, u saved my day. Thank you for this.

              – m.w.
              Nov 2 '18 at 9:43

















            Wow, u saved my day. Thank you for this.

            – m.w.
            Nov 2 '18 at 9:43





            Wow, u saved my day. Thank you for this.

            – m.w.
            Nov 2 '18 at 9:43


















            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%2f34549859%2frun-a-script-in-dockerfile%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

            Bressuire

            Vorschmack

            Quarantine