Skip to content
Advertisement

How to get source code from git repo using DockerFile

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"]

Advertisement

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement