用Docker部署GitLab

下载镜像

gitlab 运行需要依赖 redis 和 mySQL ,首先需要用 docker 拉取三个镜像:

  1. redis
  2. mySQL
  3. gitlab
1
2
3
docker pull sameersbn/gitlab:latest
docker pull sameersbn/mysql:latest 
docker pull sameersbn/redis:latest

启动 Redis

1
2
3
4
docker run \
  --name=redis \
  -tid \
  sameersbn/redis:latest

启动 mySQL

由于docker 中的数据保存在容器中,如果 mysql 容器被删除,那么它里面的数据也会消失。为了避免这种情况出现,我们需要新建一个卷来存储容器中的/var/lib/mysql文件夹

mkdir -p /opt/gitlab/mysql

启动 mysql 容器

docker run \
  --name=gitlab_mysql \
  -tid \
  -e 'DB_NAME=gitlabhq_production' \
  -e 'DB_USER=gitlab' \
  -e 'DB_PASS=password' \
  -v /opt/gitlab/mysql:/var/lib/mysql \
  sameersbn/mysql:latest
  • -e 后面跟的是 gitlab 指定的参数,不能随意更改
  • -v 将容器中/var/lib/mysql挂在到/opt/gitlab/mysql

启动 GitLab

  1. 创建 gitlab 中数据需要挂在的路径

     mkdir -p /opt/gitlab/data /opt/gitlab/log
    
  2. 启动 gitlab 容器

     docker run \
       --name='gitlab' \
       -itd \
       --link gitlab_mysql:mysql \
       --link redis:redisio \
       -e 'GITLAB_PORT=80' \
       -e 'GITLAB_SSH_PORT=22' \
       -p 22:22 -p 80:80 \
       -v /var/run/docker.sock:/run/docker.sock \
       -v $(which docker):/bin/docker \
       -v /opt/gitlab/data:/home/git/data \
       -v /opt/gitlab/log:/var/log/gitlab \
       -e 'GITLAB_HOST=192.168.199.100' \
       -e 'GITLAB_BACKUPS=daily' \
       -e 'GITLAB_BACKUP_TIME=20:00' \
       -e 'GITLAB_TIMEZONE=Beijing' \
       -e 'GITLAB_EMAIL=gitlab@helmsman.im' \
       -e 'SMTP_ENABLED=true' \
       -e 'SMTP_DOMAIN=mxhichina.com' \
       -e 'SMTP_HOST=smtp.mxhichina.com' \
       -e 'SMTP_PORT=25' \
       -e 'SMTP_STARTTLS=false' \
       -e 'SMTP_USER=gitlab@helmsman.im' \
       -e 'SMTP_PASS=nHypEMf3Hd8' \
       -e 'SMTP_AUTHENTICATION=login' \
       -e 'GITLAB_SECRETS_DB_KEY_BASE="jcKcRhhVxmk3bmwTcdh3wxbphrTdCsXsRC3N4hcNjcpwTtxJ7HjkXfNqPbXfRwqp"' \
       sameersbn/gitlab:latest
    
  3. --link: 需要连接的容器,上面说过 gitlab 需要依赖redismySQL

  4. -e:配置 GitLab
  5. -p:映射 gitlab 容器内部端口到服务器端口:

    • 80 -> 10080: 访问 gitlab 页面
    • 22 -> 10022: SSH 端口
  6. 等待几分钟后,访问 http://ipaddress:10080 访问即可

加入开机启动

加入 /etc/rc.local

docker start redis
docker start gitlab_mysql
docker start gitlab

备份和恢复 gitlab

备份 gitlab

docker stop gitlab && docker rm gitlab

docker run \
      --name='gitlab' -it --rm -p 22:22 -p 80:80 \
      --link gitlab_mysql:mysql \
      --link redis:redisio \
      -v /var/run/docker.sock:/run/docker.sock \
      -v $(which docker):/bin/docker \
      -v /opt/gitlab/data:/home/git/data \
      -v /opt/gitlab/log:/var/log/gitlab \
      -e 'GITLAB_PORT=80' \
      -e 'GITLAB_SSH_PORT=22' \
      -e 'GITLAB_HOST=192.168.199.100' \
      -e 'GITLAB_BACKUPS=daily' \
      -e 'GITLAB_BACKUP_TIME=20:00' \
      -e 'GITLAB_TIMEZONE=Beijing' \
      -e 'GITLAB_EMAIL=gitlab@helmsman.im' \
      -e 'SMTP_ENABLED=true' \
      -e 'SMTP_DOMAIN=mxhichina.com' \
      -e 'SMTP_HOST=smtp.mxhichina.com' \
      -e 'SMTP_PORT=25' \
      -e 'SMTP_STARTTLS=false' \
      -e 'SMTP_USER=gitlab@helmsman.im' \
      -e 'SMTP_PASS=nHypEMf3Hd8' \
      -e 'SMTP_AUTHENTICATION=login' \
      -e 'GITLAB_SECRETS_DB_KEY_BASE="jcKcRhhVxmk3bmwTcdh3wxbphrTdCsXsRC3N4hcNjcpwTtxJ7HjkXfNqPbXfRwqp"' \
      sameersbn/gitlab:latest app:rake gitlab:backup:create

恢复 gitlab

  1. 用以下命令恢复备份,其中 BUCKUP = 1452850850 为备份前缀

     docker stop gitlab && docker rm gitlab
    
     docker run \
           --name='gitlab' -it --rm -p 22:22 -p 80:80 \
           --link gitlab_mysql:mysql \
           --link redis:redisio \
           -v /var/run/docker.sock:/run/docker.sock \
           -v $(which docker):/bin/docker \
           -v /opt/gitlab/data:/home/git/data \
           -v /opt/gitlab/log:/var/log/gitlab \
           -e 'GITLAB_PORT=80' \
           -e 'GITLAB_SSH_PORT=22' \
           -e 'GITLAB_HOST=192.168.199.100' \
           -e 'GITLAB_BACKUPS=daily' \
           -e 'GITLAB_BACKUP_TIME=20:00' \
           -e 'GITLAB_TIMEZONE=Beijing' \
           -e 'GITLAB_EMAIL=gitlab@helmsman.im' \
           -e 'SMTP_ENABLED=true' \
           -e 'SMTP_DOMAIN=mxhichina.com' \
           -e 'SMTP_HOST=smtp.mxhichina.com' \
           -e 'SMTP_PORT=25' \
           -e 'SMTP_STARTTLS=false' \
           -e 'SMTP_USER=gtilab@helmsman.im' \
           -e 'SMTP_PASS=nHypEMf3Hd8' \
           -e 'SMTP_AUTHENTICATION=login' \
           -e 'GITLAB_SECRETS_DB_KEY_BASE="jcKcRhhVxmk3bmwTcdh3wxbphrTdCsXsRC3N4hcNjcpwTtxJ7HjkXfNqPbXfRwqp"' \
           sameersbn/gitlab:latest app:rake gitlab:backup:restore BACKUP=1452850850
    
  2. 恢复之后,按照第一步启动 gitlab 即可

更多操作

其他备忘

  • gitlab 镜像备份地址:/opt/gitlab/data/backups (按以上设置,每天清晨4点自动备份)
  • gitlab 数据卷挂载地址:/opt/gitlab/data
  • gitlab 的 mysql 挂载地址:/opt/gitlab/mysql
  • gitlab 的 log 挂载地址:/opt/gitlab/log

参考

Jul 21st, 2016

Comments