Thu 07 April 2016


Dockerized Gogs git server and alpine postgres in 20 minutes or less

I've babysat gitlab omnibus before and it wasn't any fun. So when a group of volunteer developers decide to rewrite a community website "with devops" I did what is becoming an annual exercise looking for a free many-user private git repo.

Unsatisfied with bitbucket and friends I decided to try docker + gogs.

$ docker pull gogs/gogs
$ docker images gogs/gogs
gogs/gogs      latest    40ce39eb7c4b   2 weeks ago    81.17 MB

The image is about 81MB and based on Alpine Linux which is perfect, it saves me from forking the repo and doing that myself.

$ docker run --rm --name gogs gogs/gogs
Apr  8 00:03:03 syslogd started: BusyBox v1.24.1
2016/04/08 00:03:03 [W] Custom config '/data/gogs/conf/app.ini' not found, ignore this if you're running first time
2016/04/08 00:03:03 [T] Custom path: /data/gogs
2016/04/08 00:03:03 [T] Log path: /app/gogs/log
2016/04/08 00:03:03 [I] Gogs: Go Git Service
2016/04/08 00:03:03 [I] Log Mode: Console(Trace)
2016/04/08 00:03:03 [I] Cache Service Enabled
2016/04/08 00:03:03 [I] Session Service Enabled
2016/04/08 00:03:03 [I] SQLite3 Supported
2016/04/08 00:03:03 [I] Run Mode: Development
2016/04/08 00:03:03 [I] Listen:
Apr  8 00:03:03 sshd[31]: Server listening on port 22.

Note that this docker image listens on two ports: gogs on 3000 and ssh on 22. Usually running ssh in a container is considered bad form but in this case we are supporting git-over-ssh. Let's kill this container and restart it properly.

$ docker stop gogs; docker rm gogs
$ mkdir -p /srv/lxc/gogs/data
$ docker run -d --name gogs \
  -p 8300:3000 -p 8322:22 -v /srv/lxc/gogs/data:/data gogs/gogs

Let's take a look at that web service: open http://vault:8300 gogs start page

Things are going so well at this point that I feel emboldened - what is it like running postgres in docker nowadays?

$ docker pull postgres
... starts downloading two hundred something MB ...

200MB for just a database manager? Not in my digital backyard. A search for docker alpine postgres turns up kiasaki/alpine-postgres in not one but practically all of the results.

$ docker pull kiasaki/alpine-postgress
$ docker images kiasaki/alpine-postgres
REPOSITORY                TAG       IMAGE ID       CREATED        SIZE
kiasaki/alpine-postgres   latest    b58856735da8   5 weeks ago    22.69 MB

The docker hub page for kiasaki/alpine-postgres shows how to start the image:

$ mkdir -p /srv/lxc/postgres/data
$ docker run -d --name postgres \
    -e POSTGRES_PASSWORD=supersecret \
    -v /srv/lxc/postgres/data:/var/lib/postgresql/data \
    -p 5432:5432 kiasaki/alpine-postgres
$ docker ps -s | grep postgres
b33cf374af70 kiasaki/alpine-postgres "/docker-entrypoint.s" 7 minutes ago
Up 7 minutes>5432/tcp postgres 48 B (virtual 22.69 MB)

22 MB < 200 MB and stack-smashing protection for postgres to boot.

At this point we could spin up the gogs container and use the postgres superuser but lets see how we go about administrating postgres without installing psql.

$ docker exec -it postgres psql -U postgres
psql (9.4.6, server 9.5.1)
WARNING: psql major version 9.4, server major version 9.5.
         Some psql features might not work.
Type "help" for help.

postgres=# CREATE USER gogs WITH PASSWORD '^go4git&docker';
postgres=# CREATE DATABASE gogs OWNER gogs;
postgres=# \q

Great now we spin up gogs this time linking it to postgres and data volumes:

$ docker stop gogs; docker rm gogs
$ docker run --link postgres -d --name gogs \
  -p 8300:3000 -p 8322:22 -v /srv/lxc/gogs/data:/data gogs/gogs
$ open https://vault:8300

Back at the start page select [[postgres]] with user:gogs pass:^go4git&docker I found a hiccup here - after filling in the credentials it created the DB but forwarded my client to localhost:3000 and not vault:8300 (in my case).

From here I made an account dang and it was nice enough to pull in my gravatar. I made an empty repo called 'blog' then added http://vault:8300/dang/blog.git as a remote to my existing repo:

$ git remote add gogs http://dang@vault:8300/dang/blog.git
$ git push gogs master
Password for 'http://dang@vault:8300':

Voila! I can make and push branches then turn those into pull requests in gogs. Remember you can peak at the logs of either container:

$ docker logs -f gogs
$ docker logs -f postgres

Suggestions on how better to explore and learn docker are always welcome in the comments.

Credits: Thanks to Manuel Carlos Rodriguez for some corrections.

Go Top
comments powered by Disqus