Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying local git config into docker container

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.

like image 828
Shardj Avatar asked Oct 15 '18 15:10

Shardj


People also ask

Can I use Git in a Docker container?

Even if you are running your project on Docker, you can still access your git account inside Docker Containers. All you need to do is just install Git inside your Docker Container.

Can you use localhost in Docker?

Docker provides a host network which lets containers share your host's networking stack. This approach means localhost inside a container resolves to the physical host, instead of the container itself.


1 Answers

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.

like image 85
atline Avatar answered Sep 20 '22 07:09

atline