使用docker快速搭建gitlab服务器
对于想自己搭建一套属于自己(公司)的git服务的小伙伴有服了,我们使用gitlab的社区版,可以快速搭建一套gitlab服务。但是,gitlab的官方配置又比较麻烦,怎么办呢?使用docker吧!
环境:
Ubuntu 16.04LTS
Docker 1.11.2
gitlab 8.13.6
认识docker
Docker官网:www.docker.com
Docker简介:
PACKAGE YOUR APPLICATION INTO A STANDARDIZED UNIT FOR SOFTWARE DEVELOPMENT——Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.
方便开发,把你的应用打包到容器中。——把碎片化的软件放到一个完整的文件系统中,这个系统包括所有的代码、运行环境、系统工具、系统依赖包,即所有安装在一台服务器上的环境。这样,保证了该软件一直能够在同样的环境下进行运行。
简单来说,容器技术提供了跨平台、轻量、稳定的运行软件的统一环境。
安装Docker
这里有详细的介绍:
【1】Ubuntu、Debian 系列安装 Docker(中文)
【2】Install Docker on Linux distributions(官方英文)
1.查看内核:Docker 需要安装在 64 位的 x86 平台或 ARM 平台上(如树莓派),并且要求内核版本不低于 3.10。
uname -a
2.更新包并且添加GPG密钥
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates
$ sudo apt-key adv \
--keyserver hkp://ha.pool.sks-keyservers.net:80 \
--recv-keys 58118E89F3A912897C070ADBF76221572C52609D
3.对于不同版本的ubuntu在/etc/apt/sources.list.d/
目录下,新建docker.list
文件
vi /etc/apt/sources.list.d/docker.list
添加内容如下:(提示:针对你自己的系统版本进行选择一行)
Ubuntu version Repository
Precise 12.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-precise main
Trusty 14.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-trusty main
Wily 15.10 deb https://apt.dockerproject.org/repo ubuntu-wily main
Xenial 16.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-xenial main
如:Ubutun 16.04LTS /etc/apt/sources.list.d/docker.list
添加如下内容,:wq
保存退出。
deb https://apt.dockerproject.org/repo ubuntu-xenial main
4.更新依赖包并安装Docker-engine
sudo apt-get update
sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual
sudo apt-get install docker-engine
5.启动服务并运行例子docker镜像Hello world.
sudo service docker start
sudo docker run hello-world
这样就大功告成了。
docker加速器
对于中国的用户,国外的镜像不是被墙就是下载很慢,提供大家两个好去处:阿里镜像加速、DaoCloud加速
个人试用之后,推荐使用DaoCloud的;PS:如果你的服务器是阿里的,那就使用阿里的吧,毕竟自家的加速还是快点儿,方法在这:docker使用阿里云Docker镜像库加速。
DaoCloud注册之后,登陆,选择顶部的加速器:https://www.daocloud.io/mirror,可以看到三个系统下的加速方法。在Linux下的加速方法:
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://xxxxxxxx.m.daocloud.io
使用DaoCloud的官方脚本进行加速就Ok了,或者手动添加:
vi /etc/default/docker
DOCKER_OPTS="--registry-mirror=http://xxxxxxxx.m.daocloud.io"
保存后,使用service docker restart
重启Docker,就可以在国内愉快的Download Docker镜像了。
配置gitlab
国内外有很多很优秀的Docker镜像源。
Docker官方:
DaoCloud:
阿里Docker镜像:
https://dev.aliyun.com/search.html
===分割线===
进入正文:
1.下载gitlab的docker镜像并启动:
命令:
docker pull sameersbn/gitlab:8.14.0
docker pull sameersbn/postgresql:9.5-3
docker pull sameersbn/redis:latest
启动postgresql数据库:
docker run --name gitlab-postgresql -d \
--env 'DB_NAME=gitlabhq_production' \
--env 'DB_USER=gitlab' --env 'DB_PASS=password' \
--env 'DB_EXTENSION=pg_trgm' \
--volume /srv/docker/gitlab/postgresql:/var/lib/postgresql \
sameersbn/postgresql:9.5-3
启动Redis:
docker run --name gitlab-redis -d \
--volume /srv/docker/gitlab/redis:/var/lib/redis \
sameersbn/redis:latest
启动gitlab服务:
docker run --name gitlab -d \
--link gitlab-postgresql:postgresql --link gitlab-redis:redisio \
--publish 10022:22 --publish 10080:80 \
--env 'GITLAB_PORT=10080' --env 'GITLAB_SSH_PORT=10022' \
--env 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string' \
--env 'GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string' \
--env 'GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string' \
--volume /srv/docker/gitlab/gitlab:/home/git/data \
sameersbn/gitlab:8.14.0
上面的命令一大堆,如果是程序员,应该很好懂这些参数的意义:env
环境变量,volume
相当于把容器内的文件挂载到宿主里面来,就像U盘插到了系统中,link
连接容器,让他们通信,publish
发布,做一个端口映射。
2.Docker-compose
快速启动容器(推荐)
使用方法,下载Docker-compose管理脚本:
curl -L "https://github.com/docker/compose/releases/download/1.8.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
权限赋予:
chmod +x /usr/local/bin/docker-compose
查看:
docker-compose --version
docker-compose version: 1.8.1
PS:或者使用pip
安装:
pip install docker-compose
安装gitlab的docker-compose配置文件:
wget https://raw.githubusercontent.com/sameersbn/docker-gitlab/master/docker-compose.yml
需要配置的地方:
(1)配置gitlab的密钥:
https://github.com/sameersbn/docker-gitlab/blob/master/docker-compose.yml#L57-L59
- GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alphanumeric-string
- GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alphanumeric-string
- GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alphanumeric-string
在long-and-random-alphanumeric-string
配置一长串密钥。
(2)配置Gitlab_host:
https://github.com/sameersbn/docker-gitlab/blob/master/docker-compose.yml#L53
其他的,如端口的映射、数据库密码,大家按需配置。
(3)关于邮件配置:
- SMTP_ENABLED=true
- SMTP_DOMAIN=email-smtp.region-1.amazonaws.com
- SMTP_HOST=email-smtp.region-1.amazonaws.com
- SMTP_PORT=587
- SMTP_USER=IAMmailerKey
- SMTP_PASS=IAMmailerSecret
- SMTP_STARTTLS=true
- SMTP_AUTHENTICATION=login
建议使用AWS,因为mailgun国内已经被墙了。而163,QQ这些公共邮箱,每天都有发件限制。https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/smtp.md
(4)关于时区设置:(选择北京时区)
- TZ=Asia/Beijing
- GITLAB_TIMEZONE=Beijing
(5)关于备份:
- GITLAB_BACKUP_SCHEDULE=daily
- GITLAB_BACKUP_TIME=02:00
- GITLAB_BACKUP_DIR=/home/git/data/backups
PS:每天2点备份,备份地址:/home/git/data/backups
启动:
docker-compose up -d
容器相关命令
1.使用docker ps
查看正在运行的容器
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df2be7411e4e sameersbn/gitlab:8.14.0 "/sbin/entrypoint.sh " 12 minutes ago Up 12 minutes 443/tcp, 0.0.0.0:10022->22/tcp, 0.0.0.0:10080->80/tcp mygitlab
fe43f6fd82d6 sameersbn/redis:latest "/sbin/entrypoint.sh " 5 weeks ago Up 15 hours 6379/tcp myredis
1fee910ccc15 sameersbn/postgresql:9.5-3 "/sbin/entrypoint.sh" 5 weeks ago Up 15 hours 5432/tcp mypostgresql
2.使用docker logs <containerId>
查看docker日志输出情况
root@ubuntu:/home/liwei# docker logs mygitlab
Initializing logdir...
Initializing datadir...
Updating CA certificates...
Installing configuration templates...
Configuring gitlab...
Configuring gitlab::database
Configuring gitlab::redis
Configuring gitlab::secrets...
Configuring gitlab::sidekiq...
Configuring gitlab::gitlab-workhorse...
Configuring gitlab::unicorn...
Configuring gitlab::timezone...
Configuring gitlab::rack_attack...
Configuring gitlab::ci...
Configuring gitlab::artifacts...
Configuring gitlab::lfs...
Configuring gitlab::project_features...
Configuring gitlab::smtp_settings...
Configuring gitlab::incoming_email...
Configuring gitlab::oauth...
Configuring gitlab::oauth::github...
Configuring gitlab::ldap...
Configuring gitlab::backups...
Configuring gitlab::backups::schedule...
Configuring gitlab::registry...
Configuring gitlab-shell...
Configuring nginx...
Configuring nginx::gitlab...
...
3.使用docker images
查看已经下载的docker容器
root@ubuntu:/home/liwei# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sameersbn/gitlab 8.14.0 3b000ac37c08 21 hours ago 777.1 MB
sameersbn/gitlab 8.13.5 085ba74f4375 2 weeks ago 770.3 MB
sameersbn/gitlab 8.13.1 986efbb1be0f 4 weeks ago 769.7 MB
jenkins latest 9bc67dd3e379 5 weeks ago 712 MB
sameersbn/postgresql 9.5-3 5d428ccd0ca8 5 weeks ago 232.5 MB
sameersbn/redis latest d98e69c03abe 5 weeks ago 196.5 MB
dordoka/tomcat latest 79eaa61ac94f 8 weeks ago 779.6 MB
hello-world latest c54a2cc56cbb 4 months ago 1.848 kB
busybox latest 2b8fd9751c4c 5 months ago 1.093 MB
phpmyadmin/phpmyadmin latest 8d7d99c9cd5a 5 months ago 57.02 MB
4.使用docker rmi <containerID>
删除镜像
root@ubuntu:/home/liwei# docker rmi 085ba74f4375
Untagged: sameersbn/gitlab:8.13.5
Deleted: sha256:085ba74f4375683370a4186c3e78784dd146b04a38f601c5d33c05e9495980a7
Deleted: sha256:8332e8dea2fb01961cb2f4302ea99070d9b41620521e5db5f1cdbbeae6af38d6
Deleted: sha256:3e343fbfe86b82a34a9f4367577d42fcfb2e64f35d247a63ad6ff088d617a5d5
Deleted: sha256:ad22df0171a53e6b5e5174470b8a351c65cf98eb81f775686419de4f5bf3181c
Deleted: sha256:251592c53bb0b387f8f28c9aca2a95f9578d295327a30a1ccde637ab956f7412
Deleted: sha256:0aacd95dcfe0f3e7ea152cc235d52c67eca5a68153a182147b0f0abf953ca69d
gitlab升级、备份恢复
1.升级。
按照https://github.com/sameersbn/docker-gitlab/blob/master/docker-compose.yml修改gitlab的版本、redis版本、postgresql的版本即可。
docker-compose up -d
2.备份恢复:
docker-compose run gitlab app:rake gitlab:backup:restore
root@ubuntu:/home/liwei# docker-compose run gitlab app:rake gitlab:backup:restore
Initializing logdir...
Initializing datadir...
Updating CA certificates...
Installing configuration templates...
Configuring gitlab...
Configuring gitlab::database
Configuring gitlab::redis
Configuring gitlab::secrets...
Configuring gitlab::sidekiq...
Configuring gitlab::gitlab-workhorse...
Configuring gitlab::unicorn...
Configuring gitlab::timezone...
Configuring gitlab::rack_attack...
Configuring gitlab::ci...
Configuring gitlab::artifacts...
Configuring gitlab::lfs...
Configuring gitlab::project_features...
Configuring gitlab::smtp_settings...
Configuring gitlab::incoming_email...
Configuring gitlab::oauth...
Configuring gitlab::oauth::github...
Configuring gitlab::ldap...
Configuring gitlab::backups...
Configuring gitlab::backups::schedule...
Configuring gitlab::registry...
Configuring gitlab-shell...
Configuring nginx...
Configuring nginx::gitlab...
‣ 1480212149_gitlab_backup.tar (created at 27 Nov, 2016 - 02:02:29 Asia)
‣ 1480125698_gitlab_backup.tar (created at 26 Nov, 2016 - 02:01:38 Asia)
‣ 1480039281_gitlab_backup.tar (created at 25 Nov, 2016 - 02:01:21 Asia)
‣ 1479866501_gitlab_backup.tar (created at 23 Nov, 2016 - 02:01:41 Asia)
‣ 1479780085_gitlab_backup.tar (created at 22 Nov, 2016 - 02:01:25 Asia)
‣ 1479693688_gitlab_backup.tar (created at 21 Nov, 2016 - 02:01:28 Asia)
Select a backup to restore:
选择指定的备份号进行恢复:1480212149_gitlab_backup.tar
回车。
3.容器重启:
docker-compose restart