docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > docker compose部署gitlab

docker-compose部署gitlab的详细过程

作者:DevOps_node

文章介绍了GitLab作为开源版本控制系统的核心功能(代码托管、CI/CD、项目管理等)及适用场景,详细说明了使用docker-compose部署GitLab 18.2.4-ce的步骤,并指导配置Nginx反向代理实现便捷访问,感兴趣的朋友跟随小编一起看看吧

一、gitlab介绍

GitLab是一个开源的、基于Git的版本控制系统。

1. 核心功能

2. 适用场景

二、gitlab部署

以下是gitlab-18.2.4-ce部署

1. 环境准备

2. 部署gitlab

2.1 创建目录

mkdir -p /data/gitlab/config
mkdir -p /data/gitlab/logs
mkdir -p /data/gitlab/data

2.2 编写docker-compose.yml文件

version: '3.8'
services:
  gitlab:
    image: gitlab/gitlab-ce:18.2.4-ce.0
    container_name: gitlab
    restart: always
    hostname: gitlab.example.com
    ports:
      - "8080:80"    # 给 Nginx 代理
      - "8443:443"   # 如果想用 GitLab 自带 https,可选
      - "2222:22"    # ssh 克隆用
    volumes:
      - /data/gitlab/config:/etc/gitlab
      - /data/gitlab/logs:/var/log/gitlab
      - /data/gitlab/data:/var/opt/gitlab

2.3 启动gitlab容器

docker-compose up -d

3. 调整gitLab 内部配置

vim /data/gitlab/config/gitlab.rb (容器内部是/etc/gitlab/gitlab.rb)

#指定域名   
#注意:为什么这里不写https//gitlab.example.com,如果写了https,gitLab内置nginx和外层nginx冲突,gitLab内部nginx会尝试启用443,而外层Nginx也在管443,结果请求来回绕,容易访问502。
external_url 'http://gitlab.example.com'   
# 如果 ssh 用 2222 端口,需要加上:
gitlab_rails['gitlab_shell_ssh_port'] = 2222
#因为用了Nginx 反向代理,那就 关闭 GitLab 的内置证书申请
letsencrypt['enable'] = false

更改完之后重载配置

docker exec -it gitlab gitlab-ctl reconfigure

4. gitlab其他操作

#杀掉进程(如果出现执行重载配置是卡住可选择操作)
docker exec -it gitlab pkill -9 -f "cinc-client"
docker exec -it gitlab gitlab-ctl reconfigure
#查看组件是否正常(各个组件显示run就说明ok)
docker exec -it gitlab gitlab-ctl status
#GitLab 在 容器第一次初始化时,会生成一个随机的 root 密码,默认的登录方式是:
用户名:root
密码:保存在容器挂载的配置目录里(/etc/gitlab/initial_root_password)
#查看宿主机挂载位置
cat /data/gitlab/config/initial_root_password
#会有类似输出(!!!如果更改过root密码就不会更新这个文件了)
# WARNING: This value is valid only in the following conditions
# ...
Password: AbCdEfGhIjKlMnOpQrSt
# ...
#如果密码忘记,可以进入容器里重置
docker exec -it gitlab gitlab-rails console -e production
#在控制台输入
user = User.find_by_username('root')
user.password = '你的新密码'
user.password_confirmation = '你的新密码'
user.save!

三、配置nginx反向代理

1. 安装nginx

#拉取nginx镜像
docker pull nginx:1.26.2	
#创建持久化目录
mkdir -p /data/nginx/cert
mkdir -p /data/nginx/conf/conf.d
mkdir -p /data/nginx/html
mkdir -p /data/nginx/logs

2. 创建nginx配置文件

创建基础配置文件nginx.confvim /data/nginx/conf/nginx.conf

user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    client_max_body_size 200m;
    client_body_timeout 300;
    include /etc/nginx/conf.d/*.conf;
}

创建gitlab代理nginx配置文件

server {
    listen 80;
    server_name gitlab.example.com; # 替换为你的域名
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl;
    server_name gitlab.example.com; # 替换为你的域名
    ssl_session_timeout 30m;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_certificate /etc/nginx/cert/certificate.pem; # 替换为你的 SSL 证书路径
    ssl_certificate_key /etc/nginx/cert/private.key; # 替换为你的 SSL 私钥路径
    ssl_session_cache shared:SSL:10m;
    location / {
        client_max_body_size 256m;
        proxy_pass http://localhost:8080; # 替换为 GitLab 容器的 IP 或域名
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_intercept_errors on;
    }
    # GitLab WebSocket 支持(例如 CI/CD 终端)
    location /-/ {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

3. 重启nginx

systemctl restart nginx

4.访问gitlab

https://gitlab.example.com  #访问你自己的域名

到此这篇关于docker-compose部署gitlab的文章就介绍到这了,更多相关docker-compose部署gitlab内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文