21

I'm using both docker and docker-compose to host what for the most part is a LAMP stack. I'd like to be able to use git inside one of my containers without it asking for my user.email and user.name on push after I build. Along with other things such as my push.default and branch settings. Is there any good way to have docker or docker-compose copy the results of git config --list to a file in the container, which I can then use with my entrypoint to setup the git config.

Shardj
  • 1,800
  • 2
  • 17
  • 43

3 Answers3

32

Is there any good way to have docker or docker-compose copy the results of git config --list to a file in the container, which I can then use with my entrypoint to setup the git config.

You really needn't do that to reach your aims, there is a outbox solution:

For your host machine which run git, all the contents of git config --list is stored in files:

  • If use git config --system to configure them, they are stored in /etc/gitconfig
  • If use git config --global to configure them, they are stored in ~/.gitconfig

So, you just need to mount the files to containers, then can reuse the git configure on host machine.

Something like follows, FYI.

  • If host use --global to configure git:

    docker run --rm -it -v ~/.gitconfig:/etc/gitconfig your_image_with_git git config --list
    

    output: user.name=xxx

  • If host use --system to configure git:

    docker run --rm -it -v /etc/gitconfig:/etc/gitconfig your_image_with_git git config --list
    

    output: user.name=yyy

For docker-compose, you can just configure volumes to define the mount.

atline
  • 28,355
  • 16
  • 77
  • 113
  • Hi, thanks for the reply, this is great. I'll use docker-compose to set up a volume from ~/.gitconfig to dockers ~/.gitconfig unless you think there's a reason to not do that. – Shardj Oct 16 '18 at 09:07
  • 1
    I'm not sure if the second `~/.gitconfig` will be explained in container, maybe in host machine. Maybe had to use absolute path for container part, something like `~/.gitconfig:/root/.gitconfig`, not `~/.gitconfig:~/.gitconfig`, anyway, you could have a try, I'm not quite sure for compose behavior about this. – atline Oct 16 '18 at 09:45
  • I have a user setup to use when attached to the container anyway, so they have a home directory from /username/home I can use. Thanks – Shardj Oct 16 '18 at 09:52
  • 1
    In the end @lagom I went with `- type: bind source: $HOME/.gitconfig target: /home/developer/.gitconfig` in my docker-compose.yml and it works a treat – Shardj Oct 16 '18 at 13:20
  • I found that mounting my gitconfig as a volume in docker-compose didn't work for me as I had individual build instructions for my containers. This triggered the build instructions in the Dockerfile which is where the call to git happened (via a dependency in yarn.lock). I found that setting the gitconfig in the Dockerfile before the yarn instruction `RUN git config --global url."https://github.com/".insteadOf git@github.com:` `RUN git config --global url."https://".insteadOf git://` did the trick - https://stackoverflow.com/questions/38545852/fetching-git-behind-proxy-in-docker-container – whatapalaver Jul 04 '19 at 14:19
  • @whatapalaver, of course, that question you mentioned is totally different with this one. For this one, the OP said: `entrypoint to setup the git config`, this means it intends to use git in running container, volume works there. While in the question you mentioned, it use git in `RUN git clone https://github.com/sofetch/slack-standup-bot.git`, `RUN` happened in build time, volume defintely not work. – atline Jul 04 '19 at 14:25
  • 1
    Sorry, wasn't meaning to suggest your answer wasn't correct. Just signposting for others like myself, who came to the question and answer for slightly different situations. Your answer helped me get to a final solution. – whatapalaver Jul 04 '19 at 14:30
  • Can someone add a sample `docker-compose` to the answer. :) would be of much help. – Rakmo Jul 26 '20 at 18:11
  • `volumes: - "~/.gitconfig:/etc/gitconfig"` – Slava.In Jan 24 '22 at 16:36
0

For the docker-compose, this worked well for me (based on @atline's answer)

    volumes:
  - "~/.gitconfig:/etc/gitconfig"
Slava.In
  • 597
  • 1
  • 9
  • 22
-5

I am assuming you want to be able to use git inside a container but at the same time you do not want to type your username and password again and again?

The method that I am able to come up with is simple add the git config command in the Dockerfile.

The reason is you will be supplying the password in plain-text anyways one way or the other.

So, my suggestion is adding these lines to the Dockerfile.

RUN git config --global user.name "your username"
RUN git config --global user.password "your password"
scipsycho
  • 527
  • 2
  • 4
  • 13
  • 3
    Forgot to reply, this wouldn't work as a solution for me for multiple reasons: * I'm not the only user. * this restricts production usage. * I'm definitely not going to commit my git password to anywhere, and personally I wouldn't ever store it in my git config either. * bad practice, hard coding details rather than taking from where they're kept updated. – Shardj Oct 24 '18 at 09:24
  • Dockerfiles can read environment variables, which would let this be an option without committing the actual values to your codebase. – Liam Hammett Jun 03 '19 at 09:16
  • You may as well just use `COPY` though, bind volumes are the real solution as atline mentions in his answer – Shardj Mar 12 '20 at 10:40