Getting git to work in a Windows docker container
Using ssh authentication to clone dependencies.

Our Docker containers may need to download external dependencies from private repositories at build time. In one of my recent projects, I needed to write a Dockerfile to do precisely this. I had used pipenv as my dependency manager and one of the entries in my Pipfile looked like this:
[packages]
sdk_kit = {ref = "commit#", git = "ssh://[email protected]:799/sdk_project/sdk_kit.git"}If you are looking to write a Dockerfile for a Windows container that can effectively clone dependencies from your private repositories, this article will serve as a good reference.
Prerequisites
Windows server (or the desktop version) with Docker installed.
Git in Docker
As you probably have guessed, I did all my development on a Windows server. The problem was that external downloads were only permitted from pypi.org
(How convenient it would have been to get my project working with say Chocolatey! Oh well,...)
The solution was to use a pre-downloaded Git installer to build my docker images.
If web access isn’t a problem for you, you may include the following lines in your Dockerfile, and jump to the Git authentication in Docker section.
# Install Chocolatey
RUN powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"RUN setx PATH "%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
# Install git
RUN choco install git.install -yI downloaded 7zip and portable Git installer, and stored these applications as follows:
|
|_git_install_kit
| |_7z1514-x64.exe
| |_PortableGit-2.26.0-64-bit.7z.exe
|_Dockerfile
|_application_code
|_Pipfile
|_Pipfile.lockWith this setup, I included the following code in my Dockerfile:
WORKDIR /build# setting up git
COPY git_install_kit/7z1514-x64.exe .COPY git_install_kit/PortableGit-2.26.0-64-bit.7z.exe .RUN setx Path 'C:\7zip;C:\git\bin;C:\git\usr\bin;C:\git\mingw64\bin;%Path%'RUN C:\build\7z1514-x64.exe -ArgumentList '/D=C:/7zip', '/S'RUN 7z x PortableGit-2.26.0-64-bit.7z.exe -oC:\gitAfter the last RUN command was successfully executed, git was setup in my Docker container’s environment.
Git authentication in Docker
In applications where git authentication is required, 2 more steps are needed — setting up an ssh config file and an rsa key.
Since I knew that my Docker containers would access a certain internal URL at build time, my config file was as follows:
Host bitbucket.org
StrictHostKeyChecking noI also needed to copy my private rsa key into the project directory before running docker build. If you don’t want to move your keys around, you have the option of using a --build-arg SSH_KEY="$(cat path/to/.ssh/id_rsa)" and using SSH_KEY in your Dockerfile.
My project directory now looked like this:
|
|_git_install_kit
|_Dockerfile
|_application_code
|_Pipfile
|_Pipfile.lock
|
|_id_rsa
|_configLet’s take a look at the modifications I made to my Dockerfile:
# setting up ssh keysRUN mkdir -p $home/.sshCOPY id_rsa .RUN cp id_rsa $home/.ssh/COPY config .
RUN cp config $home/.ssh/If you have chosen to supply your ssh private key via a build argument, consider doing the following instead:
ARG SSH_KEYRUN echo $SSH_KEY > $home/.ssh/configWith these modifications in place, git clone was able to successfully run inside my Docker container at build time.
Thanks for reading. If you liked this article, please leave (up to 50) claps and consider subscribing to get the best of my articles delivered straight to you.






