How to replace binary file in /bin inside a Linux container












1















I want to make some weird container, one of my tasks were to replace the uname binary.

I coded it:



#include <stdio.h>
int main() {
printf("This is a fake unamen");
return 0;
}


Compiled it:



gcc uname.c -o uname


it works fine when I run it on my ubuntu.



I created a Dockerfile and copied it to the image:



cat > Dockerfile <<EOF
FROM alpine:latest
RUN rm /bin/uname
COPY uname /bin/
ENTRYPOINT sh;
WORKDIR /home
EOF


And build it docker build -t myimage -f Dockerfile .



when I run the image:



docker run -it --rm myimage  


The file exist but when I try to run it, it writes it doesn't exist:



/home # uname
sh: uname: not found
/home # ls -lia uname
ls: uname: No such file or directory
/home # ls -lia /bin/uname
35 -rwxr-xr-x 1 root root 8600 Jan 29 13:27 /bin/uname









share|improve this question



























    1















    I want to make some weird container, one of my tasks were to replace the uname binary.

    I coded it:



    #include <stdio.h>
    int main() {
    printf("This is a fake unamen");
    return 0;
    }


    Compiled it:



    gcc uname.c -o uname


    it works fine when I run it on my ubuntu.



    I created a Dockerfile and copied it to the image:



    cat > Dockerfile <<EOF
    FROM alpine:latest
    RUN rm /bin/uname
    COPY uname /bin/
    ENTRYPOINT sh;
    WORKDIR /home
    EOF


    And build it docker build -t myimage -f Dockerfile .



    when I run the image:



    docker run -it --rm myimage  


    The file exist but when I try to run it, it writes it doesn't exist:



    /home # uname
    sh: uname: not found
    /home # ls -lia uname
    ls: uname: No such file or directory
    /home # ls -lia /bin/uname
    35 -rwxr-xr-x 1 root root 8600 Jan 29 13:27 /bin/uname









    share|improve this question

























      1












      1








      1








      I want to make some weird container, one of my tasks were to replace the uname binary.

      I coded it:



      #include <stdio.h>
      int main() {
      printf("This is a fake unamen");
      return 0;
      }


      Compiled it:



      gcc uname.c -o uname


      it works fine when I run it on my ubuntu.



      I created a Dockerfile and copied it to the image:



      cat > Dockerfile <<EOF
      FROM alpine:latest
      RUN rm /bin/uname
      COPY uname /bin/
      ENTRYPOINT sh;
      WORKDIR /home
      EOF


      And build it docker build -t myimage -f Dockerfile .



      when I run the image:



      docker run -it --rm myimage  


      The file exist but when I try to run it, it writes it doesn't exist:



      /home # uname
      sh: uname: not found
      /home # ls -lia uname
      ls: uname: No such file or directory
      /home # ls -lia /bin/uname
      35 -rwxr-xr-x 1 root root 8600 Jan 29 13:27 /bin/uname









      share|improve this question














      I want to make some weird container, one of my tasks were to replace the uname binary.

      I coded it:



      #include <stdio.h>
      int main() {
      printf("This is a fake unamen");
      return 0;
      }


      Compiled it:



      gcc uname.c -o uname


      it works fine when I run it on my ubuntu.



      I created a Dockerfile and copied it to the image:



      cat > Dockerfile <<EOF
      FROM alpine:latest
      RUN rm /bin/uname
      COPY uname /bin/
      ENTRYPOINT sh;
      WORKDIR /home
      EOF


      And build it docker build -t myimage -f Dockerfile .



      when I run the image:



      docker run -it --rm myimage  


      The file exist but when I try to run it, it writes it doesn't exist:



      /home # uname
      sh: uname: not found
      /home # ls -lia uname
      ls: uname: No such file or directory
      /home # ls -lia /bin/uname
      35 -rwxr-xr-x 1 root root 8600 Jan 29 13:27 /bin/uname






      docker container






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 29 at 13:51









      E235E235

      1274




      1274






















          1 Answer
          1






          active

          oldest

          votes


















          5














          The problem is that you build your binary against glibc (as you are using Ubuntu), but then run it on Alpine (which uses musl instead).



          When you build the binary, some metadata is hardcoded into it, specifically, the interpreter (dynamic linker) location. During the binary execution the kernel uses this interpreter to load the binary and all of its runtime dependencies. When you build against glibc, the binary requests the glibc's interpreter (something like /lib64/ld-linux-x86-64.so.2):



          $ readelf -l /bin/uname
          <...>
          [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]


          but there is no such interpreter under Alpine. Alpine has /lib/ld-musl-x86_64.so.1 instead, and all its binaries are built so that they request exactly this interpreter:



          # readelf -l /bin/busybox
          <...>
          [Requesting program interpreter: /lib/ld-musl-x86_64.so.1]


          So, when you build your uname in Ubuntu and then run it in Alpine, the kernel is unable to find the interpreter, thus it returns "not found".



          The interesting point is that you may invoke the interpreter manually and this may work if the problem is only in the interpreter:



          # /lib/ld-musl-x86_64.so.1 /bin/uname 
          This is a fake uname


          But this, of course, is only for troubleshooting.



          So, what to do?



          Your options are either statically link your binary (thus making the interpreter and all shared libraries unneeded so that the kernel may load and run the binary on its own):



          gcc uname.c -static -o uname


          or build it under Alpine (maybe try Docker's multistage builds? I would recommend this approach as in this case you explicitly declare your intention and show that you build your custom binary in Alpine to replace the original Alpine's one).






          share|improve this answer


























          • The -static solved it. Thanks you for your informative answer !

            – E235
            Jan 29 at 16:05











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f497455%2fhow-to-replace-binary-file-in-bin-inside-a-linux-container%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









          5














          The problem is that you build your binary against glibc (as you are using Ubuntu), but then run it on Alpine (which uses musl instead).



          When you build the binary, some metadata is hardcoded into it, specifically, the interpreter (dynamic linker) location. During the binary execution the kernel uses this interpreter to load the binary and all of its runtime dependencies. When you build against glibc, the binary requests the glibc's interpreter (something like /lib64/ld-linux-x86-64.so.2):



          $ readelf -l /bin/uname
          <...>
          [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]


          but there is no such interpreter under Alpine. Alpine has /lib/ld-musl-x86_64.so.1 instead, and all its binaries are built so that they request exactly this interpreter:



          # readelf -l /bin/busybox
          <...>
          [Requesting program interpreter: /lib/ld-musl-x86_64.so.1]


          So, when you build your uname in Ubuntu and then run it in Alpine, the kernel is unable to find the interpreter, thus it returns "not found".



          The interesting point is that you may invoke the interpreter manually and this may work if the problem is only in the interpreter:



          # /lib/ld-musl-x86_64.so.1 /bin/uname 
          This is a fake uname


          But this, of course, is only for troubleshooting.



          So, what to do?



          Your options are either statically link your binary (thus making the interpreter and all shared libraries unneeded so that the kernel may load and run the binary on its own):



          gcc uname.c -static -o uname


          or build it under Alpine (maybe try Docker's multistage builds? I would recommend this approach as in this case you explicitly declare your intention and show that you build your custom binary in Alpine to replace the original Alpine's one).






          share|improve this answer


























          • The -static solved it. Thanks you for your informative answer !

            – E235
            Jan 29 at 16:05
















          5














          The problem is that you build your binary against glibc (as you are using Ubuntu), but then run it on Alpine (which uses musl instead).



          When you build the binary, some metadata is hardcoded into it, specifically, the interpreter (dynamic linker) location. During the binary execution the kernel uses this interpreter to load the binary and all of its runtime dependencies. When you build against glibc, the binary requests the glibc's interpreter (something like /lib64/ld-linux-x86-64.so.2):



          $ readelf -l /bin/uname
          <...>
          [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]


          but there is no such interpreter under Alpine. Alpine has /lib/ld-musl-x86_64.so.1 instead, and all its binaries are built so that they request exactly this interpreter:



          # readelf -l /bin/busybox
          <...>
          [Requesting program interpreter: /lib/ld-musl-x86_64.so.1]


          So, when you build your uname in Ubuntu and then run it in Alpine, the kernel is unable to find the interpreter, thus it returns "not found".



          The interesting point is that you may invoke the interpreter manually and this may work if the problem is only in the interpreter:



          # /lib/ld-musl-x86_64.so.1 /bin/uname 
          This is a fake uname


          But this, of course, is only for troubleshooting.



          So, what to do?



          Your options are either statically link your binary (thus making the interpreter and all shared libraries unneeded so that the kernel may load and run the binary on its own):



          gcc uname.c -static -o uname


          or build it under Alpine (maybe try Docker's multistage builds? I would recommend this approach as in this case you explicitly declare your intention and show that you build your custom binary in Alpine to replace the original Alpine's one).






          share|improve this answer


























          • The -static solved it. Thanks you for your informative answer !

            – E235
            Jan 29 at 16:05














          5












          5








          5







          The problem is that you build your binary against glibc (as you are using Ubuntu), but then run it on Alpine (which uses musl instead).



          When you build the binary, some metadata is hardcoded into it, specifically, the interpreter (dynamic linker) location. During the binary execution the kernel uses this interpreter to load the binary and all of its runtime dependencies. When you build against glibc, the binary requests the glibc's interpreter (something like /lib64/ld-linux-x86-64.so.2):



          $ readelf -l /bin/uname
          <...>
          [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]


          but there is no such interpreter under Alpine. Alpine has /lib/ld-musl-x86_64.so.1 instead, and all its binaries are built so that they request exactly this interpreter:



          # readelf -l /bin/busybox
          <...>
          [Requesting program interpreter: /lib/ld-musl-x86_64.so.1]


          So, when you build your uname in Ubuntu and then run it in Alpine, the kernel is unable to find the interpreter, thus it returns "not found".



          The interesting point is that you may invoke the interpreter manually and this may work if the problem is only in the interpreter:



          # /lib/ld-musl-x86_64.so.1 /bin/uname 
          This is a fake uname


          But this, of course, is only for troubleshooting.



          So, what to do?



          Your options are either statically link your binary (thus making the interpreter and all shared libraries unneeded so that the kernel may load and run the binary on its own):



          gcc uname.c -static -o uname


          or build it under Alpine (maybe try Docker's multistage builds? I would recommend this approach as in this case you explicitly declare your intention and show that you build your custom binary in Alpine to replace the original Alpine's one).






          share|improve this answer















          The problem is that you build your binary against glibc (as you are using Ubuntu), but then run it on Alpine (which uses musl instead).



          When you build the binary, some metadata is hardcoded into it, specifically, the interpreter (dynamic linker) location. During the binary execution the kernel uses this interpreter to load the binary and all of its runtime dependencies. When you build against glibc, the binary requests the glibc's interpreter (something like /lib64/ld-linux-x86-64.so.2):



          $ readelf -l /bin/uname
          <...>
          [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]


          but there is no such interpreter under Alpine. Alpine has /lib/ld-musl-x86_64.so.1 instead, and all its binaries are built so that they request exactly this interpreter:



          # readelf -l /bin/busybox
          <...>
          [Requesting program interpreter: /lib/ld-musl-x86_64.so.1]


          So, when you build your uname in Ubuntu and then run it in Alpine, the kernel is unable to find the interpreter, thus it returns "not found".



          The interesting point is that you may invoke the interpreter manually and this may work if the problem is only in the interpreter:



          # /lib/ld-musl-x86_64.so.1 /bin/uname 
          This is a fake uname


          But this, of course, is only for troubleshooting.



          So, what to do?



          Your options are either statically link your binary (thus making the interpreter and all shared libraries unneeded so that the kernel may load and run the binary on its own):



          gcc uname.c -static -o uname


          or build it under Alpine (maybe try Docker's multistage builds? I would recommend this approach as in this case you explicitly declare your intention and show that you build your custom binary in Alpine to replace the original Alpine's one).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 29 at 17:55

























          answered Jan 29 at 15:56









          Danila KiverDanila Kiver

          443212




          443212













          • The -static solved it. Thanks you for your informative answer !

            – E235
            Jan 29 at 16:05



















          • The -static solved it. Thanks you for your informative answer !

            – E235
            Jan 29 at 16:05

















          The -static solved it. Thanks you for your informative answer !

          – E235
          Jan 29 at 16:05





          The -static solved it. Thanks you for your informative answer !

          – E235
          Jan 29 at 16:05


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • 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%2funix.stackexchange.com%2fquestions%2f497455%2fhow-to-replace-binary-file-in-bin-inside-a-linux-container%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

          How to make a Squid Proxy server?

          Is this a new Fibonacci Identity?

          19世紀