Skip to content

Instantly share code, notes, and snippets.

@uetchy
Last active November 29, 2018 16:30
Show Gist options
  • Save uetchy/065766a85bd08fa95b07 to your computer and use it in GitHub Desktop.
Save uetchy/065766a85bd08fa95b07 to your computer and use it in GitHub Desktop.
The build script for Gitlab and Jenkins on Docker wtih docker-gen(+ Dokku env).

Specification

  • Docker on Ubuntu
  • Gitlab container with Mailgun mailing support
  • nginx reverse-proxy with docker-gen

Prepare

Setup automatic reverse-proxy with docker-gen

from http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/

# Install docker-gen command-line tool
wget https://github.com/jwilder/docker-gen/releases/download/0.3.1/docker-gen-linux-amd64-0.3.1.tar.gz
tar xvzf docker-gen-linux-amd64-0.3.1.tar.gz
mv ./docker-gen /usr/bin/

# Fetch docker-gen related config files
curl -L https://gist.githubusercontent.com/uetchy/065766a85bd08fa95b07/raw/e57720805961b4f60a7db583ca14df7003af09c1/docker-gen.conf -o /etc/init/docker-gen.conf
curl -L https://gist.githubusercontent.com/uetchy/065766a85bd08fa95b07/raw/f893cb431b4a3353712dcdb9fad25188f55e8bee/docker-gen.cfg -o /etc/docker-gen.cfg
curl -L https://gist.githubusercontent.com/uetchy/065766a85bd08fa95b07/raw/985ca0e234843964116867dc63e325edce08cc0d/docker-gen.tmpl -o /etc/nginx/docker-gen.tmpl

# Start docker-gen by Upstart
start docker-gen

If it was given VIRTUAL_HOST when launching Docker container, docker-gen provides reverse-proxy configration for the container exposes ip and port into /etc/nginx/conf.d/docker-gen.conf

Configure tools for automatic job file creation

$ mkdir -p /etc/init/docker
env container_name="gitlab"

description "Docker container-name"
author "uetchy"
start on filesystem and started docker
stop on runlevel [!2345]
respawn
script
  /usr/bin/docker start -a $container_name
end script

Install Gitlab

Default Gitlab credentials

username: admin@local.host
password: 5iveL!fe

Setup

# Pulling Gitlab image
docker pull sameersbn/gitlab:latest

# Launch Gitlab container with following options
DOCKER_GITLAB_HOST=git.example.com
docker run --name gitlab -d \
  -v /opt/gitlab/data:/home/git/data \
  -v /opt/gitlab/mysql:/var/lib/mysql \
  -p 22:22 \
  -h $DOCKER_GITLAB_HOST \
  -e "VIRTUAL_HOST=$DOCKER_GITLAB_HOST" \
  -e "GITLAB_HOST=$DOCKER_GITLAB_HOST" \
  -e "GITLAB_EMAIL=gitlab@$DOCKER_GITLAB_HOST" \
  -e "GITLAB_SUPPORT=support@$DOCKER_GITLAB_HOST" \
  -e "SMTP_DOMAIN=$DOCKER_GITLAB_HOST" \
  -e "SMTP_HOST=smtp.mailgun.org" \
  -e "SMTP_USER=postmaster@$DOCKER_GITLAB_HOST" \
  -e "SMTP_PASS=password" \
  sameersbn/gitlab:latest

Install Jenkins

Setup

from https://github.com/uetchy/docker-jenkins

# Pulling Jenkins imaage
docker pull uetchy/jenkins

# Install Jenkins plugins into /opt/jenkins first
docker run -it \
  -v /opt/jenkins:/jenkins \
  -e "JENKINS_PLUGINS=simple-theme-plugin greenballs credentials ssh-credentials ssh-agent git-client git scm-api postbuild-task gitlab-hook gitlab-merge-request-jenkins" \
  uetchy/jenkins plugins:install

# Launch Jenkins container with following options
docker run -d --name=jenkins \
  -v /opt/jenkins:/jenkins \
  -e "VIRTUAL_HOST=ci.example.com" \
  uetchy/jenkins

Install ngIRCd

Setup

docker pull uetchy/ngircd
docker run --name ngircd -d \
  -p 6667:6667 \
  -e "NGIRCD_NAME=irc.example.com" \
  -e "NGIRCD_INFO=The internal irc server" \
  -e "NGIRCD_PASSWORD=password" \
  -e "NGIRCD_MOTD_PHRASE='Welcome home!'" \
  uetchy/ngircd
[[config]]
template = "/etc/nginx/docker-gen.tmpl"
dest = "/etc/nginx/conf.d/docker-gen.conf"
onlyexposed = true
watch = true
notifycmd = "/etc/init.d/nginx reload"
#!upstart
description "docker-gen upstart script"
start on filesystem and started docker/*
stop on shutdown
respawn
exec start-stop-daemon --start --exec /usr/bin/docker-gen -- -config /etc/docker-gen.cfg
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
upstream {{ $host }} {
{{ range $index, $value := $containers }}
{{ $addrLen := len $value.Addresses }}
{{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }}
{{ with $address := index $value.Addresses 0 }}
# {{$value.Name}}
server {{ $address.IP }}:{{ $address.Port }};
{{ end }}
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}}
{{ else if $value.Env.VIRTUAL_PORT }}
{{ range $i, $address := $value.Addresses }}
{{ if eq $address.Port $value.Env.VIRTUAL_PORT }}
# {{$value.Name}}
server {{ $address.IP }}:{{ $address.Port }};
{{ end }}
{{ end }}
{{/* Else default to standard web port 80 */}}
{{ else }}
{{ range $i, $address := $value.Addresses }}
{{ if eq $address.Port "80" }}
# {{$value.Name}}
server {{ $address.IP }}:{{ $address.Port }};
{{ end }}
{{ end }}
{{ end }}
{{ end }}
}
server {
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
server_name {{ $host }};
proxy_buffering off;
#error_log /proc/self/fd/2;
#access_log /proc/self/fd/1;
location / {
proxy_pass http://{{ $host }};
include /etc/nginx/proxy_params;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment