Skip to content

Commit 61ddce6

Browse files
committed
Support mount bound volumes by populating them
For volumes that are provided via a mount bind of a local directory, the contents are not automatically copied over from the directory that is getting mounted over. So the entrypoint script will generally copy all files from a backup data directory into the data directories that do not yet exist. This way all files should be there but edited ones not be overwritten. The defaults.properties file is always copied, since it could contain new settings upon an upgrade, and it should never be edited by a user. If running as root, also file ownership is changed to the gitblit user so that gitblit can read and write data. Closes #4
1 parent 6f93475 commit 61ddce6

File tree

4 files changed

+178
-32
lines changed

4 files changed

+178
-32
lines changed

Dockerfile

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ ENV GITBLIT_VAR /var/opt/gitblit
4242

4343
# Move the data files to a separate directory and set some defaults
4444
RUN set -eux ; \
45+
mkdir -p -m 0775 $GITBLIT_VAR ; \
4546
gbetc=$GITBLIT_VAR/etc ; \
4647
gbsrv=$GITBLIT_VAR/srv ; \
47-
mkdir -p -m 0750 $gbsrv ; \
48+
mkdir -p -m 0775 $gbsrv ; \
4849
mv /opt/gitblit/data/git $gbsrv ; \
4950
ln -s $gbsrv/git /opt/gitblit/data/git ; \
5051
mv /opt/gitblit/data $gbetc ; \
@@ -146,13 +147,17 @@ include = gitblit-docker.properties\n\
146147
chown -R gitblit:gitblit $GITBLIT_VAR ; \
147148
# Set file permissions so that gitblit can read all and others cannot mess up
148149
# or read private data
149-
chmod -R o-rwx $gbsrv ; \
150-
chmod -R u+rwxs $gbsrv $gbsrv/git ; \
151-
chmod -R u+rwxs $gbetc ; \
152-
chmod -R o-rwx $gbetc ; \
153-
chmod ug=r $gbetc/defaults.properties ; \
154-
chmod g-w $gbetc/gitblit-docker.properties ; \
155-
chmod 0664 $gbetc/gitblit.properties ;
150+
chmod ug+rwxs $gbsrv $gbsrv/git ; \
151+
chmod ug+rwxs $gbetc $gbetc/certs ; \
152+
chmod go=r $gbetc/defaults.properties ; \
153+
chmod 0664 $gbetc/gitblit-docker.properties ; \
154+
chmod 0664 $gbetc/gitblit.properties ; \
155+
\
156+
# Now we make a backup of the etc files, so that we can copy them to mount bound
157+
# volumes to make sure all needed files are present in them.
158+
cp -a $gbetc /opt/gitblit/vog-etc ; \
159+
cp -a $gbsrv/git/project.mkd /opt/gitblit/srv-project.mkd ;
160+
156161

157162

158163

Dockerfile.alpine

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ ENV GITBLIT_VAR /var/opt/gitblit
4242

4343
# Move the data files to a separate directory and set some defaults
4444
RUN set -eux ; \
45+
mkdir -p -m 0775 $GITBLIT_VAR ; \
4546
gbetc=$GITBLIT_VAR/etc ; \
4647
gbsrv=$GITBLIT_VAR/srv ; \
47-
mkdir -p -m 0750 $gbsrv ; \
48+
mkdir -p -m 0775 $gbsrv ; \
4849
mv /opt/gitblit/data/git $gbsrv ; \
4950
ln -s $gbsrv/git /opt/gitblit/data/git ; \
5051
mv /opt/gitblit/data $gbetc ; \
@@ -146,13 +147,17 @@ include = gitblit-docker.properties\n\
146147
chown -R gitblit:gitblit $GITBLIT_VAR ; \
147148
# Set file permissions so that gitblit can read all and others cannot mess up
148149
# or read private data
149-
chmod -R o-rwx $gbsrv ; \
150-
chmod -R u+rwxs $gbsrv $gbsrv/git ; \
151-
chmod -R u+rwxs $gbetc ; \
152-
chmod -R o-rwx $gbetc ; \
153-
chmod ug=r $gbetc/defaults.properties ; \
154-
chmod g-w $gbetc/gitblit-docker.properties ; \
155-
chmod 0644 $gbetc/gitblit.properties ;
150+
chmod ug+rwxs $gbsrv $gbsrv/git ; \
151+
chmod ug+rwxs $gbetc $gbetc/certs ; \
152+
chmod go=r $gbetc/defaults.properties ; \
153+
chmod 0664 $gbetc/gitblit-docker.properties ; \
154+
chmod 0664 $gbetc/gitblit.properties ; \
155+
\
156+
# Now we make a backup of the etc files, so that we can copy them to mount bound
157+
# volumes to make sure all needed files are present in them.
158+
cp -a $gbetc /opt/gitblit/vog-etc ; \
159+
cp -a $gbsrv/git/project.mkd /opt/gitblit/srv-project.mkd ;
160+
156161

157162

158163

docker-entrypoint.sh

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ if [ -z "$JAVA_OPTS" ] ; then
88
fi
99

1010

11-
gitblit_docker=$GITBLIT_VAR/etc/gitblit-docker.properties
11+
gitblit_etc=$GITBLIT_VAR/etc
12+
gitblit_docker=$gitblit_etc/gitblit-docker.properties
1213
gitblit_path=/opt/gitblit
1314
gitblit="java -server $JAVA_OPTS -Djava.awt.headless=true -cp ${gitblit_path}/gitblit.jar:${gitblit_path}/ext/* com.gitblit.GitBlitServer"
1415

@@ -62,6 +63,69 @@ set_rpc ()
6263
}
6364

6465

66+
# Make sure the data volume is populated and writeable.
67+
fill_volume ()
68+
{
69+
# Copy config files etc.
70+
if [ ! -e ${gitblit_etc} ] ; then
71+
mkdir -p ${gitblit_etc}
72+
[ "$(id -u)" = '0' ] && chown -f gitblit:gitblit ${gitblit_etc}
73+
fi
74+
75+
for entry in $(ls /opt/gitblit/vog-etc) ; do
76+
if [ -L /opt/gitblit/vog-etc/$entry ] || [ -f /opt/gitblit/vog-etc/$entry ]; then
77+
if [ ! -e ${gitblit_etc}/$entry ] ; then
78+
cp -dp /opt/gitblit/vog-etc/$entry $gitblit_etc/
79+
fi
80+
elif [ -d /opt/gitblit/vog-etc/$entry ] ; then
81+
if [ ! -e ${gitblit_etc}/$entry ] ; then
82+
mkdir -p $gitblit_etc/$entry
83+
[ "$(id -u)" = '0' ] && chown -f gitblit:gitblit ${gitblit_etc}/$entry
84+
fi
85+
86+
for file in $(ls /opt/gitblit/vog-etc/${entry}) ; do
87+
if [ ! -e ${gitblit_etc}/${entry}/$file ] ; then
88+
cp -a /opt/gitblit/vog-etc/${entry}/$file $gitblit_etc/$entry/
89+
fi
90+
done
91+
fi
92+
done
93+
94+
# Unconditionally copy over the defaults.properties file, so that new settings
95+
# are in there when Gitblit is upgraded. This file is not to be edited by the
96+
# user and only serves a s a reference, so verwriting it is okay. =)
97+
cp -dp /opt/gitblit/vog-etc/defaults.properties $gitblit_etc/ || true
98+
99+
100+
101+
# Copy the project.mkd template
102+
if [ ! -e ${GITBLIT_VAR}/srv/git ] ; then
103+
mkdir -p ${GITBLIT_VAR}/srv/git
104+
[ "$(id -u)" = '0' ] && chown -f gitblit:gitblit ${GITBLIT_VAR}/srv/git
105+
fi
106+
107+
if [ ! -e ${GITBLIT_VAR}/srv/git/project.mkd ] ; then
108+
cp -dp /opt/gitblit/srv-project.mkd ${GITBLIT_VAR}/srv/git/project.mkd
109+
fi
110+
111+
112+
# Our default for temporary files should exist
113+
if [ ! -e ${GITBLIT_VAR}/temp ] ; then
114+
mkdir -p ${GITBLIT_VAR}/temp
115+
[ "$(id -u)" = '0' ] && chown -f gitblit:gitblit ${GITBLIT_VAR}/temp
116+
fi
117+
118+
119+
120+
121+
# If we are running as root, ensure that gitblit owns everything and fix permissions
122+
if [ "$(id -u)" = '0' ] ; then
123+
find ${GITBLIT_VAR} \! -user gitblit -exec chown gitblit '{}' +
124+
find ${GITBLIT_VAR} -type d \! -perm -0700 -exec chmod u+rwxs '{}' +
125+
find ${GITBLIT_VAR} -type f \! -perm -0600 -exec chmod u+rw '{}' +
126+
fi
127+
}
128+
65129

66130
# use gosu or su-exec to step down from root
67131
runas ()
@@ -88,10 +152,14 @@ if [ "$1" = 'gitblit' ]; then
88152
shift
89153
# if no base folder is given, set the one in our docker default
90154
baseFolder=
91-
echo "$*" | grep -q -- "--baseFolder" || baseFolder="--baseFolder $GITBLIT_VAR/etc"
155+
echo "$*" | grep -q -- "--baseFolder" || baseFolder="--baseFolder $gitblit_etc"
92156
set -- $gitblit $baseFolder "$@"
93157

94158

159+
# populate volume and adjust permissions
160+
fill_volume
161+
162+
95163
# set RPC settings
96164
set_rpc
97165

hub-readme.md

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,45 @@ $ sudo docker stop gitblit
6666

6767
Gitblit stores two types of data, configuration data and Git repository data. While configuration data is
6868
relatively static, once the server is configured and has started, the repository data is what you use
69-
Gitblit for and is written often (unless you use Gitblit only as a repository browser). The docker image
70-
uses `/var/opt/gitblit` as the base folder for data storage.
69+
Gitblit for and is written often (unless you use Gitblit only as a repository browser).
70+
71+
The docker image uses `/var/opt/gitblit` as the base folder for data storage. Under the base folder, configuration and repository data are separated into two different directories. Configuration data is under `/var/opt/gitblit/etc` and repository data under `/var/opt/gitblit/srv`.
72+
73+
```console
74+
$ docker run -it --rm gitblit/gitblit ls -l /var/opt/gitblit
75+
total 8
76+
drwsrws--- 5 gitblit gitblit 4096 Mar 8 16:39 etc
77+
drwsrws--- 3 gitblit gitblit 4096 Mar 8 16:39 srv
78+
```
7179

7280
To make this data persistent and operation on it more performant, a Docker [volume](https://docs.docker.com/engine/reference/builder/#volume)
73-
is defined for this path. [Docker manages this volume](https://docs.docker.com/storage/volumes/) automatically
74-
for you. This is the default and the easiest configuration. The only downside is that the files may be hard to
75-
locate for you or tools running outside the container. You can make it a little easier by defining a name for
76-
the volume, when creating the container.
81+
is defined for the path `/var/opt/gitblit` for the image.
82+
83+
84+
Important note: There are several ways to store data used by applications that run in Docker containers. We encourage users of the gitblit images to familiarize themselves with the options available, including:
85+
86+
* Let Docker manage the storage of your server data [by writing the files to disk on the host system using its own internal volume management](https://docs.docker.com/engine/tutorials/dockervolumes/#adding-a-data-volume). This is the default and is easy and fairly transparent to the user. The downside is that the files may be hard to locate for tools and applications that run directly on the host system, i.e. outside containers.
87+
* Create a data directory on the host system (outside the container) and [mount this to a directory visible from inside the container](https://docs.docker.com/engine/tutorials/dockervolumes/#mount-a-host-directory-as-a-data-volume). This places the server files in a known location on the host system, and makes it easy for tools and applications on the host system to access the files. The downside is that the user needs to make sure that the directory exists, and that e.g. directory permissions and other security mechanisms on the host system are set up correctly.
88+
89+
The Docker documentation is a good starting point for understanding the different storage options and variations, and there are multiple blogs and forum postings that discuss and give advice in this area.
90+
91+
#### Data volumes
92+
[Docker manages this volume](https://docs.docker.com/storage/volumes/) automatically
93+
for you. This is the default and the easiest configuration. You can make the volume files a little easier to locate by defining a name for the volume, when creating the container (or, even, when creating a volume beforehand).
7794

7895
```console
7996
$ sudo docker run -d --name gitblit -v gitblit-data:/var/opt/gitblit -p 8443:8443 -p 29418:29418 gitblit/gitblit
8097
```
8198

82-
Under the base directory, configuration and repository data are separated into two different directories.
83-
Configuration data is under `/var/opt/gitblit/etc` and repository data under `/var/opt/gitblit/srv`. If, for
84-
some reason, you want to use different volumes for either, e.g. for different kinds of backup, you can attach
85-
two volumes to these directories.
99+
If, for some reason, you want to use different volumes for `etc` and `srv`, e.g. for different kinds of backup, you can attach two different volumes to these directories.
86100

87101
```console
88-
$ sudo docker run -d --name gitblit -v gitblit-config:/var/opt/gitblit/etc -v gitblit-repos:/var/opt/gitblit/srv -p 8443:8443 -p 29418:29418 gitblit/gitblit
102+
$ sudo docker run -d --name gitblit -v gitblit-config:/var/opt/gitblit/etc \
103+
-v gitblit-repos:/var/opt/gitblit/srv \
104+
-p 8443:8443 -p 29418:29418 gitblit/gitblit
89105
```
90106

91-
Naming a volume makes it more discoverable with [Docker's tools](https://docs.docker.com/engine/reference/commandline/volume_ls/):
107+
Giving a volume a name also makes it more discoverable with [Docker's tools](https://docs.docker.com/engine/reference/commandline/volume_ls/):
92108

93109
```console
94110
$ sudo docker volume ls --format 'table {{.Name}}\t{{.Mountpoint}}\t{{.Driver}}'
@@ -97,7 +113,7 @@ gitblit-config /var/lib/docker/volumes/gitblit-config/_data local
97113
gitblit-repos /var/lib/docker/volumes/gitblit-repos/_data local
98114
```
99115

100-
It also makes updates easier. Simply provide the same named volumes to the new version of the container:
116+
It also makes upgrades of Gitblit easier. Simply provide the same named volume to the new version of the container:
101117

102118
```console
103119
$ sudo docker pull gitblit/gitblit:rpc
@@ -108,8 +124,23 @@ $ sudo docker run -d --name gitblit -v gitblit-data:/var/opt/gitblit -p 8443:844
108124

109125
Updating with anonymous volumes (no name provided for it) requires you to either find out the volume id from the current running container and reusing that id for the new container, or to use the `--volumes-from` parameter, which requires the old container to still be around.
110126

127+
#### Mount bind directories
128+
129+
The second option is to mount a local directory on the host into the container via a bind mount. Again, you can choose if you want all of the data in the host directory, or maybe just the configuration data, for easier editing, while the git data is stored in a docker data volume. (Or, vice versa, of course. Or, something completely different.)
130+
131+
The container will copy the necessary configuration files, that Gitblit needs to run, into the directory. (While this is done automatically by docker for data volumes, it has to be done explicitly by the container for a bind mount volume.) Existing data is not overwritten (except for the `defaults.properties`file, use this only for reference). The start script will also change ownership of the directory and files to the `gitblit`user because the server process will need to be able to read them and write to some.
132+
133+
```console
134+
$ sudo docker run -d --name gitblit -v /some/path/data:/var/opt/gitblit -p 8443:8443 gitblit/gitblit
135+
```
136+
137+
Or, when only storing the configuration data in a local host directory, e.g. `/etc/gitblit`:
138+
139+
```console
140+
$ sudo docker run -d --name gitblit -v /etc/gitblit:/var/opt/gitblit/etc -p 29418:29418 gitblit/gitblit
141+
```
111142

112-
### Temporary webapp data
143+
#### Temporary webapp data
113144

114145
For advanced usage under Linux, you may be able to improve performance by moving Gitblit's `temp` folder
115146
to RAM. Gitblit unpacks web application data on each start into a temporary folder. The default for that
@@ -122,6 +153,43 @@ $ sudo docker run -d --name gitblit --tmpfs /var/opt/gitblit/temp -p 8443:8443 g
122153
```
123154

124155

156+
## Running as non-root with `--user`
157+
158+
The gitblit images will drop root privileges in the start up script and run the Gitblit server process under the unprivileged user `gitblit` with user and group id `8117`. Still, the image allows to directly start a container as a non-root user with the `--user` command line parameter, albeit with some restrictions.
159+
160+
If you simply don't want any part to run with root privileges, you can directly start the container as the user `8117`:
161+
162+
```console
163+
$ sudo docker run -d --name gitblit --user 8117:8117 -p 8443:8443 -p 29418:29418 gitblit/gitblit
164+
```
165+
166+
What does not work, is to use a different user id. This is because that user id will not have the permissions to write to the files and directories in the container. If you want to run the container as an arbitrary user, you need to provide a bind mount volume and make sure that the ownership and permissions allow the server process to write files. For example, to run under the user `picard`:
167+
168+
```console
169+
$ ls -ls
170+
total88
171+
drwxr-x--- 2 picard picard 64 Mar 8 18:07 gitblit-data
172+
-rwxr-xr-x 2 picard picard 88 Mar 8 18:07 somefile
173+
174+
$ sudo docker run -d -v $PWD/gitblit-data:/var/opt/gitblit --user $(id -u picard) -p 8443:8443 gitblit/gitblit
175+
```
176+
177+
Another use case is, if you want to use Gitblit only as an attractive repository browser for your local git projects. In that case you can bind mount only your directory with your git projects to `/var/opt/gitblit/srv/git` and run gitblit under your user id. In this case you also need to run it under the gitblit *group* id `8117`, so that the process has access to the other data volumes containing the configuration data.
178+
179+
```console
180+
$ ls -l
181+
total 0
182+
drwxr-xr-x 29 anthony staff 928 Feb 28 20:00 gitblit/
183+
drwxr-xr-x 10 anthony staff 320 Mar 8 18:16 gitblit-docker/
184+
drwxr-xr-x 12 anthony staff 384 Feb 16 15:26 gitblit-maven/
185+
drwxr-xr-x 13 anthony staff 416 Feb 16 15:36 ok.sh/
186+
187+
$ sudo docker run --rm --user $(id -u):8117 -v $PWD:/var/opt/gitblit/srv/git -p 8080:8080 gitblit/gitblit --httpsPort=0
188+
```
189+
190+
You can then direct your browser to [http://localhost:8080](http://localhost:8080) and directly start browsing your repositories.
191+
192+
125193
## Configuration
126194

127195
Configure the gitblit instance by adding your custom settings to the file `gitblit.properties` in the directory `/var/opt/gitblit/etc` in the container. Some options can be controlled by providing environment variables to the container.

0 commit comments

Comments
 (0)