|
1 | 1 | # git-server-docker
|
2 |
| -A lightweight Git Server Docker image built with Alpine Linux. Available on [GitHub](https://github.com/jkarlosb/git-server-docker) and [Docker Hub](https://hub.docker.com/r/jkarlos/git-server-docker/) |
| 2 | +A lightweight Git Server Docker image built with Alpine Linux. Available on [GitHub](https://github.com/fr123k/git-server-docker) and [Docker Hub](https://hub.docker.com/r/fr123k/git-server-docker/) |
3 | 3 |
|
4 |
| - |
| 4 | +## Use Case |
5 | 5 |
|
6 |
| -### Basic Usage |
| 6 | +### Local Jenkins use Local Git Repository |
7 | 7 |
|
8 |
| -How to run the container in port 2222 with two volumes: keys volume for public keys and repos volume for git repositories: |
| 8 | +The major motivation for the local git server docker container is to provide a way for a local jenkins running in docker to use local git repositories instead of github for example. |
9 | 9 |
|
10 |
| - $ docker run -d -p 2222:22 -v ~/git-server/keys:/git-server/keys -v ~/git-server/repos:/git-server/repos jkarlos/git-server-docker |
| 10 | +The following steps describe a way to use the local git server in jenkins without changing the github url of the jobs in jenkins. They can still point to the original github respositories. |
11 | 11 |
|
12 |
| -How to use a public key: |
| 12 | +#### Local DNS |
13 | 13 |
|
14 |
| - Copy them to keys folder: |
15 |
| - - From host: $ cp ~/.ssh/id_rsa.pub ~/git-server/keys |
16 |
| - - From remote: $ scp ~/.ssh/id_rsa.pub user@host:~/git-server/keys |
17 |
| - You need restart the container when keys are updated: |
18 |
| - $ docker restart <container-id> |
19 |
| - |
20 |
| -How to check that container works (you must to have a key): |
| 14 | +Setup a domain like `local.github.com` that is then used by jenkins. |
21 | 15 |
|
22 |
| - $ ssh git@<ip-docker-server> -p 2222 |
23 |
| - ... |
24 |
| - Welcome to git-server-docker! |
25 |
| - You've successfully authenticated, but I do not |
26 |
| - provide interactive shell access. |
27 |
| - ... |
| 16 | +```bash |
| 17 | +echo "192.168.65.2 local.github.com" >> /etc/hosts |
| 18 | +``` |
28 | 19 |
|
29 |
| -How to create a new repo: |
| 20 | +The ip address `192.168.65.2` is specific to your operating system and only works from within a docker container. |
| 21 | +**This ip address work on MacOS.** |
30 | 22 |
|
31 |
| - $ cd myrepo |
32 |
| - $ git init --shared=true |
33 |
| - $ git add . |
34 |
| - $ git commit -m "my first commit" |
35 |
| - $ cd .. |
36 |
| - $ git clone --bare myrepo myrepo.git |
| 23 | +#### Git Config |
37 | 24 |
|
38 |
| -How to upload a repo: |
| 25 | +Add or change the `%{JENKINS_HOME}/.gitconfig` with the following setting. |
| 26 | +``` |
| 27 | + |
| 28 | + insteadOf = https://github.com/ |
39 | 29 |
|
40 |
| - From host: |
41 |
| - $ mv myrepo.git ~/git-server/repos |
42 |
| - From remote: |
43 |
| - $ scp -r myrepo.git user@host:~/git-server/repos |
| 30 | + |
| 31 | + |
| 32 | +``` |
44 | 33 |
|
45 |
| -How clone a repository: |
| 34 | +#### Local Github Server |
| 35 | + |
| 36 | +Run the docker git server container on the port 22 (sshd) and specify the github account like `fr123k`. |
| 37 | + |
| 38 | +`docker run -p 22:22 -it -v $(PWD)/../:/git-server `**`-e ACCOUNT=fr123k`**` --name github --rm fr123k/git-server-docker` |
46 | 39 |
|
47 |
| - $ git clone ssh://git@<ip-docker-server>:2222/git-server/repos/myrepo.git |
| 40 | +If something is not as expected check the [Troubleshooting](#Troubleshooting) section. |
| 41 | + |
| 42 | +## Basic Usage |
48 | 43 |
|
49 | 44 | ### Arguments
|
50 | 45 |
|
51 | 46 | * **Expose ports**: 22
|
52 | 47 | * **Volumes**:
|
53 |
| - * */git-server/keys*: Volume to store the users public keys |
54 |
| - * */git-server/repos*: Volume to store the repositories |
| 48 | + * **/git-server/**: Volume to store the repositories |
| 49 | +* **Environment Variables**: |
| 50 | + * **ACCOUNT**: Name of the git account |
| 51 | + * **DEBUG**: If exits enable debug logging of the sshd to the file `/var/log/auth.log`. Useful for troubleshooting |
| 52 | + |
| 53 | +### Git Repository Volume |
| 54 | + |
| 55 | +The volume has to be mounted to /git-server mount point. |
| 56 | +It has to contain the `.keys` folder with all the public keys |
| 57 | +for the ssh authentication. |
| 58 | + |
| 59 | +* -v (local_git_repository):/git-server/ |
| 60 | + |
| 61 | +Example mount directory that is above the current one as a git repository. |
| 62 | + |
| 63 | +`docker run -p 2222:22 -it `**`-v $(PWD)/../:/git-server`**` --name github --rm fr123k/git-server-docker` |
| 64 | + |
| 65 | +### Git Account Name |
| 66 | + |
| 67 | +The name of the git repository. |
| 68 | +* -e ACCOUNT=(name of the git account) default: helmet |
| 69 | + |
| 70 | +For example |
| 71 | +`docker run -p 2222:22 -it -v $(PWD)/../:/git-server `**`-e ACCOUNT=fr123k`**` --name github --rm fr123k/git-server-docker` |
| 72 | + |
| 73 | +### Git Account Name |
| 74 | + |
| 75 | +The name of the git repository. |
| 76 | +* -e DEBUG=true |
| 77 | + |
| 78 | +For example |
| 79 | +`docker run -p 2222:22 -it -v $(PWD)/../:/git-server `**`-e DEBUG=true`**` --name github --rm fr123k/git-server-docker` |
| 80 | + |
| 81 | +### Local SSH Git Server |
| 82 | + |
| 83 | +How to run the container in port 22 (sshd). |
| 84 | + |
| 85 | +`docker run -d -p `**`22:22`**` -v ~/git-server/repos:/git-server/ fr123k/git-server-docker/` |
| 86 | + |
| 87 | +### Local Git Repositories |
| 88 | + |
| 89 | +**After adding git repository described below the docker container has to be always restarted.** |
| 90 | +How to create a new repo: |
| 91 | + |
| 92 | +```bash |
| 93 | +mkdir local-git-repo |
| 94 | +cd local-git-repo/ |
| 95 | +git init --shared=true |
| 96 | +git add . |
| 97 | +git commit -m "my first commit" |
| 98 | +``` |
| 99 | + |
| 100 | +How to upload a repo: |
| 101 | + |
| 102 | +From host: |
| 103 | +```bash |
| 104 | +mv local-git-repo ~/git-server/ |
| 105 | +``` |
| 106 | +From remote: |
| 107 | +```bash |
| 108 | +scp -r local-git-repo user@host:~/git-server/ |
| 109 | +``` |
| 110 | + |
| 111 | +How clone a repository: |
| 112 | + |
| 113 | +```bash |
| 114 | +git clone ssh:// [email protected]:22/helmet/local-git-repo.git |
| 115 | +``` |
| 116 | + |
| 117 | +## Troubleshooting |
| 118 | + |
| 119 | +### Validate Local SSH Git Server |
| 120 | + |
| 121 | +How to check that container and the authentication keys works. |
| 122 | + |
| 123 | +The expected output looks like this. |
| 124 | +``` |
| 125 | +Welcome to git-server-docker! |
| 126 | +
|
| 127 | +Provided to you from |
| 128 | +
|
| 129 | +https://hub.docker.com/r/fr123k/git-server-docker/ |
| 130 | +https://github.com/fr123k/git-server-docker |
| 131 | +
|
| 132 | +You've successfully authenticated, but I do not |
| 133 | +provide interactive shell access. |
| 134 | +Connection to 127.0.0.1 closed. |
| 135 | +``` |
55 | 136 |
|
56 | 137 | ### SSH Keys
|
57 | 138 |
|
58 | 139 | How generate a pair keys in client machine:
|
59 | 140 |
|
60 |
| - $ ssh-keygen -t rsa |
| 141 | +```bash |
| 142 | +ssh-keygen -t rsa |
| 143 | +``` |
61 | 144 |
|
62 | 145 | How upload quickly a public key to host volume:
|
63 | 146 |
|
64 |
| - $ scp ~/.ssh/id_rsa.pub user@host:~/git-server/keys |
| 147 | +```bash |
| 148 | +scp ~/.ssh/id_rsa.pub user@host:~/git-server/.keys |
| 149 | +``` |
| 150 | + |
| 151 | +## Docker Image |
| 152 | + |
| 153 | +All `make` commands can only be from the folder where the Makefile is located. |
| 154 | + |
| 155 | +### Build |
65 | 156 |
|
66 |
| -### Build Image |
| 157 | +How to build the docker image: |
67 | 158 |
|
68 |
| -How to make the image: |
| 159 | +```bash |
| 160 | +make build |
| 161 | +``` |
| 162 | +or |
| 163 | +```bash |
| 164 | +docker build -t git-server-docker . |
| 165 | +``` |
69 | 166 |
|
70 |
| - $ docker build -t git-server-docker . |
71 |
| - |
72 |
| -### Docker-Compose |
| 167 | +### Run |
73 | 168 |
|
74 |
| -You can edit docker-compose.yml and run this container with docker-compose: |
| 169 | +How to run the image: |
75 | 170 |
|
76 |
| - $ docker-compose up -d |
| 171 | +```bash |
| 172 | +make REPOSITORIES=$(PWD)/../ ACCOUNT=fr123k git-server |
| 173 | +``` |
| 174 | +or |
| 175 | +```bash |
| 176 | +docker run -p 22:22 -it -v $(PWD)/../ :/git-server -e ACCOUNT=fr123k --name github --rm "fr123k/git-server-docker" |
| 177 | +``` |
0 commit comments