3

THE PROBLEM
I have a git configuration that uses both access token and client certificates. While building docker image I need to use the same git configuration (in order to reach all required sources).


WHAT I DID for now is something like this in Dockerfile and it works for me:

(...)
ARG SRC="/src"
RUN mkdir ${SRC}

# copying cert files inside docker image
COPY certificate.crt ${SRC}
COPY certificate.key ${SRC}

# configuring the same git settings inside
RUN git config --global user.name "${USER_NAME}"
RUN git config --global user.email "${EMAIL}"
RUN git config --global url."https://${ACCESS_TOKEN}@git_domain.com/".insteadOf "ssh://git@git_domain.com/"
RUN git config --global http."https://git_domain.com/".sslCert "/src/certificate.crt"
RUN git config --global http."https://git_domain.com/".sslKey "/src/certificate.key"
(...)

THE QUESTIONS ARE
Maybe is there some more "laconic" or simple way?
Is it possible to use existing local file .gitconfig? Somehow to specify path to it?

I found this question Copying local git config into docker container, but docker run doesn't fit. I need to do the similar thing, but using docker build.

heshagrade
  • 31
  • 4
  • Git configuration is stored in pretty simple (ini-like) files that can easily be produced manually. For example global configuration is stored in `~/.gitconfig` (see [this section for details](https://git-scm.com/docs/git-config#FILES)). – Joachim Sauer Nov 11 '21 at 10:30
  • 1
    Also remember that you can very easily read the `docker history` of an image, or copy the `.gitconfig` file out, and get these credentials back. Similarly with the certificate files. I'd recommend pulling the application source before you run `docker build`, and injecting TLS keys at run time. – David Maze Nov 11 '21 at 11:19
  • @JoachimSauer okay, I know, but what should I do next? How to pass git configuration from `~/.gitconfig` right inside docker image? – heshagrade Nov 11 '21 at 12:58

1 Answers1

3

I need to do the similar thing, but using docker build.

I would generate the right gitconfig file locally first, then, once generated, COPY it from the Dockerfile, during a docker build.

COPY .gitconfig .
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250