How to get source code from git repo using DockerFile












0















I have Git and Docker on a remote Linux machine. The source code of my project is in a bare repo. I need a way of making the source code from this repo available to Docker during the build process.



Below is what I have now (which is basically the default template in VS 2017 for a Docker ASP.NET Core project).



Q: How do I make the code from a bare repo available? Is clone the best option here? My attempts probably fail because of auth-issues but since the repo is on the same machine I assume it should be possible to access it straight away without using ssh in this case? Can I make this path visible/accessible to the Docker process somehow?



FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src

RUN git clone ssh://user@gitserver/volume1/git/project // fails

RUN git clone /volume1/git/project // fails

COPY Test.sln ./
COPY Test/Test.csproj Test/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/Test
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Test.dll"]









share|improve this question























  • What error do you get?

    – johnpaton
    Nov 23 '18 at 13:29











  • repository '/volume1/git/project' does not exist

    – Andreas Zita
    Nov 23 '18 at 13:31











  • You are looking for a "volume mount": docs.docker.com/storage/volumes. Basically, you tell docker and your os to make the directory containing the source from your local disk visible inside the container. Then use the files from that directory. But watch out, that means the container will not work on other machines that do not have the volume mount. Can you access the git repo from a server without authentication?

    – Omair Majid
    Nov 23 '18 at 13:32











  • I'm not sure I understand but what I have read is that volumes can't be used during an image build?

    – Andreas Zita
    Nov 23 '18 at 13:43
















0















I have Git and Docker on a remote Linux machine. The source code of my project is in a bare repo. I need a way of making the source code from this repo available to Docker during the build process.



Below is what I have now (which is basically the default template in VS 2017 for a Docker ASP.NET Core project).



Q: How do I make the code from a bare repo available? Is clone the best option here? My attempts probably fail because of auth-issues but since the repo is on the same machine I assume it should be possible to access it straight away without using ssh in this case? Can I make this path visible/accessible to the Docker process somehow?



FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src

RUN git clone ssh://user@gitserver/volume1/git/project // fails

RUN git clone /volume1/git/project // fails

COPY Test.sln ./
COPY Test/Test.csproj Test/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/Test
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Test.dll"]









share|improve this question























  • What error do you get?

    – johnpaton
    Nov 23 '18 at 13:29











  • repository '/volume1/git/project' does not exist

    – Andreas Zita
    Nov 23 '18 at 13:31











  • You are looking for a "volume mount": docs.docker.com/storage/volumes. Basically, you tell docker and your os to make the directory containing the source from your local disk visible inside the container. Then use the files from that directory. But watch out, that means the container will not work on other machines that do not have the volume mount. Can you access the git repo from a server without authentication?

    – Omair Majid
    Nov 23 '18 at 13:32











  • I'm not sure I understand but what I have read is that volumes can't be used during an image build?

    – Andreas Zita
    Nov 23 '18 at 13:43














0












0








0








I have Git and Docker on a remote Linux machine. The source code of my project is in a bare repo. I need a way of making the source code from this repo available to Docker during the build process.



Below is what I have now (which is basically the default template in VS 2017 for a Docker ASP.NET Core project).



Q: How do I make the code from a bare repo available? Is clone the best option here? My attempts probably fail because of auth-issues but since the repo is on the same machine I assume it should be possible to access it straight away without using ssh in this case? Can I make this path visible/accessible to the Docker process somehow?



FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src

RUN git clone ssh://user@gitserver/volume1/git/project // fails

RUN git clone /volume1/git/project // fails

COPY Test.sln ./
COPY Test/Test.csproj Test/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/Test
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Test.dll"]









share|improve this question














I have Git and Docker on a remote Linux machine. The source code of my project is in a bare repo. I need a way of making the source code from this repo available to Docker during the build process.



Below is what I have now (which is basically the default template in VS 2017 for a Docker ASP.NET Core project).



Q: How do I make the code from a bare repo available? Is clone the best option here? My attempts probably fail because of auth-issues but since the repo is on the same machine I assume it should be possible to access it straight away without using ssh in this case? Can I make this path visible/accessible to the Docker process somehow?



FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src

RUN git clone ssh://user@gitserver/volume1/git/project // fails

RUN git clone /volume1/git/project // fails

COPY Test.sln ./
COPY Test/Test.csproj Test/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/Test
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Test.dll"]






linux git docker






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 13:28









Andreas ZitaAndreas Zita

3,09432985




3,09432985













  • What error do you get?

    – johnpaton
    Nov 23 '18 at 13:29











  • repository '/volume1/git/project' does not exist

    – Andreas Zita
    Nov 23 '18 at 13:31











  • You are looking for a "volume mount": docs.docker.com/storage/volumes. Basically, you tell docker and your os to make the directory containing the source from your local disk visible inside the container. Then use the files from that directory. But watch out, that means the container will not work on other machines that do not have the volume mount. Can you access the git repo from a server without authentication?

    – Omair Majid
    Nov 23 '18 at 13:32











  • I'm not sure I understand but what I have read is that volumes can't be used during an image build?

    – Andreas Zita
    Nov 23 '18 at 13:43



















  • What error do you get?

    – johnpaton
    Nov 23 '18 at 13:29











  • repository '/volume1/git/project' does not exist

    – Andreas Zita
    Nov 23 '18 at 13:31











  • You are looking for a "volume mount": docs.docker.com/storage/volumes. Basically, you tell docker and your os to make the directory containing the source from your local disk visible inside the container. Then use the files from that directory. But watch out, that means the container will not work on other machines that do not have the volume mount. Can you access the git repo from a server without authentication?

    – Omair Majid
    Nov 23 '18 at 13:32











  • I'm not sure I understand but what I have read is that volumes can't be used during an image build?

    – Andreas Zita
    Nov 23 '18 at 13:43

















What error do you get?

– johnpaton
Nov 23 '18 at 13:29





What error do you get?

– johnpaton
Nov 23 '18 at 13:29













repository '/volume1/git/project' does not exist

– Andreas Zita
Nov 23 '18 at 13:31





repository '/volume1/git/project' does not exist

– Andreas Zita
Nov 23 '18 at 13:31













You are looking for a "volume mount": docs.docker.com/storage/volumes. Basically, you tell docker and your os to make the directory containing the source from your local disk visible inside the container. Then use the files from that directory. But watch out, that means the container will not work on other machines that do not have the volume mount. Can you access the git repo from a server without authentication?

– Omair Majid
Nov 23 '18 at 13:32





You are looking for a "volume mount": docs.docker.com/storage/volumes. Basically, you tell docker and your os to make the directory containing the source from your local disk visible inside the container. Then use the files from that directory. But watch out, that means the container will not work on other machines that do not have the volume mount. Can you access the git repo from a server without authentication?

– Omair Majid
Nov 23 '18 at 13:32













I'm not sure I understand but what I have read is that volumes can't be used during an image build?

– Andreas Zita
Nov 23 '18 at 13:43





I'm not sure I understand but what I have read is that volumes can't be used during an image build?

– Andreas Zita
Nov 23 '18 at 13:43












2 Answers
2






active

oldest

votes


















0














Check out the Git repository outside the Docker build process; ideally, put the Dockerfile in the root directory of the repository itself. COPY the contents of the repository into the image.



There are two big problems with trying to do git clone inside a Dockerfile:




  1. If you have a private repository (which you often do) you need to get the credentials into Docker space to do the clone, and once you do, it's trivial for anyone to get them back out via docker history or docker run.


  2. docker build will remember that it's already run a step in a previous build cycle, and so it won't want to repeat the git clone step, even if the upstream repository has changed.



It's also helpful for occasional testing to be able to build an image out of something that's not checked in (yet) and having the git clone hard-coded in the Dockerfile keeps you from ever being able to do that.






share|improve this answer































    0














    It depends on how you expose the git repository. If you run gitweb you can run an http request to a raw version of the file you want to have. If you want to get only the latest file, you could do a shallow clone (depth=1) to fetch only the version you're interested in and copy the file from there. But you'll copy everything. So if the bare git repository you may use git show and pipe it to file.



    git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file


    as found here
    https://stackoverflow.com/a/2467629/2955337



    You would still need to have access to the host from the build container, so that can be tricky, I would script around it to first copy the file and then COPY the file. But personally I do a curl to GITWEB to fetch the file anonymously.






    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%2f53447612%2fhow-to-get-source-code-from-git-repo-using-dockerfile%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









      0














      Check out the Git repository outside the Docker build process; ideally, put the Dockerfile in the root directory of the repository itself. COPY the contents of the repository into the image.



      There are two big problems with trying to do git clone inside a Dockerfile:




      1. If you have a private repository (which you often do) you need to get the credentials into Docker space to do the clone, and once you do, it's trivial for anyone to get them back out via docker history or docker run.


      2. docker build will remember that it's already run a step in a previous build cycle, and so it won't want to repeat the git clone step, even if the upstream repository has changed.



      It's also helpful for occasional testing to be able to build an image out of something that's not checked in (yet) and having the git clone hard-coded in the Dockerfile keeps you from ever being able to do that.






      share|improve this answer




























        0














        Check out the Git repository outside the Docker build process; ideally, put the Dockerfile in the root directory of the repository itself. COPY the contents of the repository into the image.



        There are two big problems with trying to do git clone inside a Dockerfile:




        1. If you have a private repository (which you often do) you need to get the credentials into Docker space to do the clone, and once you do, it's trivial for anyone to get them back out via docker history or docker run.


        2. docker build will remember that it's already run a step in a previous build cycle, and so it won't want to repeat the git clone step, even if the upstream repository has changed.



        It's also helpful for occasional testing to be able to build an image out of something that's not checked in (yet) and having the git clone hard-coded in the Dockerfile keeps you from ever being able to do that.






        share|improve this answer


























          0












          0








          0







          Check out the Git repository outside the Docker build process; ideally, put the Dockerfile in the root directory of the repository itself. COPY the contents of the repository into the image.



          There are two big problems with trying to do git clone inside a Dockerfile:




          1. If you have a private repository (which you often do) you need to get the credentials into Docker space to do the clone, and once you do, it's trivial for anyone to get them back out via docker history or docker run.


          2. docker build will remember that it's already run a step in a previous build cycle, and so it won't want to repeat the git clone step, even if the upstream repository has changed.



          It's also helpful for occasional testing to be able to build an image out of something that's not checked in (yet) and having the git clone hard-coded in the Dockerfile keeps you from ever being able to do that.






          share|improve this answer













          Check out the Git repository outside the Docker build process; ideally, put the Dockerfile in the root directory of the repository itself. COPY the contents of the repository into the image.



          There are two big problems with trying to do git clone inside a Dockerfile:




          1. If you have a private repository (which you often do) you need to get the credentials into Docker space to do the clone, and once you do, it's trivial for anyone to get them back out via docker history or docker run.


          2. docker build will remember that it's already run a step in a previous build cycle, and so it won't want to repeat the git clone step, even if the upstream repository has changed.



          It's also helpful for occasional testing to be able to build an image out of something that's not checked in (yet) and having the git clone hard-coded in the Dockerfile keeps you from ever being able to do that.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 14:05









          David MazeDavid Maze

          15.8k31531




          15.8k31531

























              0














              It depends on how you expose the git repository. If you run gitweb you can run an http request to a raw version of the file you want to have. If you want to get only the latest file, you could do a shallow clone (depth=1) to fetch only the version you're interested in and copy the file from there. But you'll copy everything. So if the bare git repository you may use git show and pipe it to file.



              git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file


              as found here
              https://stackoverflow.com/a/2467629/2955337



              You would still need to have access to the host from the build container, so that can be tricky, I would script around it to first copy the file and then COPY the file. But personally I do a curl to GITWEB to fetch the file anonymously.






              share|improve this answer




























                0














                It depends on how you expose the git repository. If you run gitweb you can run an http request to a raw version of the file you want to have. If you want to get only the latest file, you could do a shallow clone (depth=1) to fetch only the version you're interested in and copy the file from there. But you'll copy everything. So if the bare git repository you may use git show and pipe it to file.



                git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file


                as found here
                https://stackoverflow.com/a/2467629/2955337



                You would still need to have access to the host from the build container, so that can be tricky, I would script around it to first copy the file and then COPY the file. But personally I do a curl to GITWEB to fetch the file anonymously.






                share|improve this answer


























                  0












                  0








                  0







                  It depends on how you expose the git repository. If you run gitweb you can run an http request to a raw version of the file you want to have. If you want to get only the latest file, you could do a shallow clone (depth=1) to fetch only the version you're interested in and copy the file from there. But you'll copy everything. So if the bare git repository you may use git show and pipe it to file.



                  git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file


                  as found here
                  https://stackoverflow.com/a/2467629/2955337



                  You would still need to have access to the host from the build container, so that can be tricky, I would script around it to first copy the file and then COPY the file. But personally I do a curl to GITWEB to fetch the file anonymously.






                  share|improve this answer













                  It depends on how you expose the git repository. If you run gitweb you can run an http request to a raw version of the file you want to have. If you want to get only the latest file, you could do a shallow clone (depth=1) to fetch only the version you're interested in and copy the file from there. But you'll copy everything. So if the bare git repository you may use git show and pipe it to file.



                  git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file


                  as found here
                  https://stackoverflow.com/a/2467629/2955337



                  You would still need to have access to the host from the build container, so that can be tricky, I would script around it to first copy the file and then COPY the file. But personally I do a curl to GITWEB to fetch the file anonymously.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 13:51









                  sleepyheadsleepyhead

                  864




                  864






























                      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%2f53447612%2fhow-to-get-source-code-from-git-repo-using-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







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini