1 拉镜像

安装非常简单
docker search gitlab // 搜索镜像
docker pull gitlab/gitlab-ce // 拉取镜像
docker images // 查询镜像

docker pull gitlab/gitlab-runner // 拉取gitlab-runner镜像

2 创建本地映射文件

test-team-server:~$ mkdir gitlab
test-team-server:~$ cd gitlab/
test-team-server:~/gitlab$ mkdir config logs data

3 安装 gitlab 和gitlab-runner

  1. docker run --detach -p 8443:443 -p 8090:8090 -p 2222:22 --name gitlab --restart always \
  2. -v /home/gitlab/config:/etc/gitlab -v /home/gitlab/logs:/var/log/gitlab -v /home/gitlab/data:/var/opt/gitlab \
  3. gitlab/gitlab-ce
  4. // docker run --detach -p 8843:443 -p 8090:80 -p 2222:22 --name gitlab --restart always -v /home/hnscdg/gitlab/config:/etc/gitlab -v /home/hnscdg/gitlab/logs:/var/log/gitlab -v /home/hnscdg/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce
  5. docker ps -a | grep gitlab
  6. docker logs -f gitlab
  • --detach: 设置容器后台运行
  • --hostname: 设置容器的 hostname
  • --publish: 端口转发规则(8080:Http 访问端口映射到Docker80端口,8443:Https 访问端口映射到Docker443端口,2222:主机的 ssh 访问端口,22:Docker 容器中 ssh 访问端口)
  • --name:容器名称
  • --restart always:每次启动容器就重启GitLab
  • --volume: 共享目录挂载,即 docker 容器内外数据共享(/srv/gitlab/data: 应用程序数据,/srv/gitlab/logs:GitLab 的 log,/srv/gitlab/config:GitLab 的配置文件)
  • --e:配置 Gitlab 运行的环境变量

git-runner

  1. sudo docker run -d --name my-gitlab-runner --restart always \
  2. -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  3. -v /var/run/docker.sock:/var/run/docker.sock \
  4. gitlab/gitlab-runner:latest

注册runner

  1. > docker exec -it my-gitlab-runner gitlab-ci-multi-runner register
  2. > Please enter the gitlab-ci coordinator URL:
  3. # 示例:http://gitlab.alibaba-inc.com/ci
  4. > Please enter the gitlab-ci token for this runner:
  5. # xxxxxx
  6. > Please enter the gitlab-ci description for this runner:
  7. # 示例:qd_api_runner
  8. > Please enter the gitlab-ci tags for this runner (comma separated):
  9. # 示例:hwy
  10. > Whether to run untagged builds [true/false]:
  11. # true
  12. > Please enter the executor: docker, parallels, shell, kubernetes, docker-ssh, ssh, virtualbox, docker+machine, docker-ssh+machine:
  13. # docker
  14. > Please enter the default Docker image (e.g. ruby:2.1):
  15. # node:latest

4 配置

访问gitlab http://localhost:8090
(1)设置密码,初始账户为root,设置密码之后就可以登录了
(2)重启、启动、停止、状态

  1. gitlab-ctl restart // 重启
  2. gitlab-ctl stop // 停止
  3. gitlab-ctl start // 启动
  4. gitlab-ctl status //状态

(3)修改hostname
gitlab项目默认是以容器id作为hostname,ssh访问的时候会提示hostname找不到,所以我们需要修改一下配置文件
/home/gitlab/config/gitlab.rb 主要修改以下几个值

  1. external_url 'http://192.168.1.119' #修改为http访问的外部映射的主机ip和port
  2. gitlab_rails['gitlab_ssh_host'] = '192.168.1.119' #修改为ssh访问的外部映射的主机ip或者hostname
  3. gitlab_rails['gitlab_shell_ssh_port'] = 2222 #ssh默认是22端口,改成映射到外部的2222端口

然后执行一下docker restart gitlab
这样在项目中显示的访问gitlab仓库的地址是
ssh://git@192.168.1.119:8090:2222/automation/apitest.git
http://192.168.1.119/automation/apitest.git
(4)下载git.exe 到本地windows

ssh-keygen -o -t rsa -b 4096 -C “xxxxxxxxx@xxxxx.com” 然后一直回车

  • 复制~/.ssh/id_rsa.pub内容,

~表示用户目录,比如windows就是C:\Users\Administrator

  1. Administrator@ERIC MINGW64 ~/.ssh
  2. $ cat id_rsa.pub
  3. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCrPgBRiWV7d6yRTdkLkP0ae+6P571uMDtxAL1x2fsNGWvmzFZTHUrHS49UJgPpaGu0ZtOv4X7bX4mmEHlmSXo5v6lZ5YC+oEAWXgg2c+ES9jf4llfOggCYEIrek//xF6OTOrZxpvzrTkAroiTdeRtivnFnIMgaNz3rO53ryH0M5L9vzWFKm7ufuKyqY/910X73+9pP9Rf1txWgY8cd3iVGSjooKPND0rY34sVm7JQ16llx49MjIKTX79e2V3p+wy6LOuSawwH9XtQrakj01aXmhQHKIRIqTn0M0FYtEitEQxuagBdzeyMF46rMWrk8k87FnJaPsIm/WNRMVimiL4UlSjp3iSg4p/+GebzdBr8S3oXT6lWglQBVXLEYHZgXRQXNhTCOoeOWEdW1wtKYPpGY+LtMkSEGbt0/v68Fj3oDXllVle3SjlI4i9KF86snfQPd3jDvBu7padvDmwtl3bLWKA38/DLe+cTUSHgymTTmfW6fDT5VpcAlKkyh6Dvcyb0hlUGdNVzasQ4nqtQtODpGhb5pfdtJlhZN2P0QlI7n0KAEPkuQUnpNMKf94fmUxiH6QQLOeVrWuZw4CW/B+bnYHoe85vySNhUI+H2qat5+3/p09AD+mxffKbSgmR6QWE9DXFTIGjtJ01dMFy699Qi8YOVsXwViOiu9Lab9ymxqLQ== hailiangyou@dingtalk.com

(4)网页上配置ssh-key

http://192.168.1.119:8090/profile/keys

docker 安装gitlab - 图1
把上面cat出来的内容复制到gitlab页面上
docker 安装gitlab - 图2

5 拉取项目,git常规使用

1,创建一个文件夹作为仓库repo D盘下的repo文件夹
打开git bash, cd /d/repo
2,设置用户名和邮箱

  1. git config --global user.name "eric"
  2. git config --global user.email "xxxx@xxx.com"

3,克隆项目,默认拉取的是master分支

  1. git clone ssh://git@192.168.1.119:2222/automation/apitest.git
  2. Administrator@ERIC MINGW64 /d/repo
  3. $ git clone ssh://git@192.168.1.119:2222/automation/apitest.git
  4. Cloning into 'apitest-framework'...
  5. The authenticity of host '[192.168.1.119]:2222 ([192.168.1.119]:2222)' can't be established.
  6. ECDSA key fingerprint is SHA256:IoMGBM4jlCN20ynwVKKrvwUFb8pKP5BjfZy5QG0/A2c.
  7. Are you sure you want to continue connecting (yes/no)? yes
  8. Warning: Permanently added '[192.168.1.119]:2222' (ECDSA) to the list of known hosts.
  9. remote: Enumerating objects: 3, done.
  10. remote: Counting objects: 100% (3/3), done.
  11. remote: Compressing objects: 100% (2/2), done.
  12. remote: Total 3 (delta 0), reused 0 (delta 0)
  13. Receiving objects: 100% (3/3), done.
  14. Checking connectivity... done.

4,git使用
在本地创建一个待push的测试文件,test.md
touch test,md
git status #可以看到红色提示该分支有需要更新的文档 test.md
docker 安装gitlab - 图3

git add test.md #添加文件到待上传的缓冲区里
git commit -m “test push test.md” #提交并解释
docker 安装gitlab - 图4

git push origin master #提交到仓库主分支
docker 安装gitlab - 图5

然后就可以在gitlab网页上看到刚刚提交的文件