Docker Compose 部署 GitLab

Docker Compose 部署 GitLab(含默认密码+外部Nginx反向代理+禁用SSH)

一套直接可用、无坑、生产环境可用的完整配置,全程不使用 SSH,只走 HTTP/HTTPS,外部 Nginx 反向代理,安装时直接配置默认管理员账号密码。

一、整体架构

  • GitLab 容器:只暴露 HTTP(80) 端口,完全禁用 SSH
  • 外部 Nginx:负责域名、SSL、反向代理到 GitLab 容器
  • Docker Compose:一键启动 GitLab
  • 预设管理员账号:root / 你自定义的密码

二、目录结构(提前建好)

bash 复制代码
mkdir -p /opt/gitlab/{config,logs,data}
chmod -R 777 /opt/gitlab  # 避免权限问题

三、docker-compose.yml(核心配置)

yaml 复制代码
version: '3.8'

services:
  gitlab:
    image: gitlab/gitlab-ce:latest
    container_name: gitlab
    restart: always
    hostname: gitlab.yourdomain.com  # 改成你的域名
    environment:
      # 外部访问地址(必须是你的最终域名)
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://gitlab.yourdomain.com'
        
        # ======================
        # 禁用 SSH 功能
        # ======================
        gitlab_rails['gitlab_shell_ssh_port'] = nil
        sshd['enable'] = false
        
        # ======================
        # 配置默认管理员密码
        # ======================
        gitlab_rails['initial_root_password'] = 'YourStrongPassword123!'

        # ======================
        # 关闭容器自带 Nginx(使用外部 Nginx)
        # ======================
        nginx['enable'] = false
        puma['enable'] = true
        puma['listen'] = '0.0.0.0'
        puma['port'] = 80
        
        # 信任外部 Nginx 反向代理
        gitlab_rails['trusted_proxies'] = ['127.0.0.1', '172.16.0.0/12', '192.168.0.0/16']
        
    ports:
      - "127.0.0.1:8929:80"  # 只监听本地,外部 Nginx 反代,不直接暴露公网
    volumes:
      - /opt/gitlab/config:/etc/gitlab
      - /opt/gitlab/logs:/var/log/gitlab
      - /opt/gitlab/data:/var/opt/gitlab
    shm_size: '256m'

关键配置说明

  1. 禁用 SSH

    • sshd['enable'] = false 关闭容器内 SSH 服务
    • gitlab_shell_ssh_port = nil 取消 SSH 克隆展示
    • 页面完全不显示 SSH 克隆地址
  2. 默认 root 密码

    • initial_root_password 第一次启动自动设置
    • 只在首次初始化生效
  3. 关闭容器内 Nginx

    • nginx['enable'] = false
    • 使用外部独立 Nginx 代理,更灵活
  4. 端口安全

    • 只绑定本地 127.0.0.1:8929
    • 公网无法直接访问,必须走 Nginx

四、启动 GitLab

bash 复制代码
cd /opt/gitlab
docker compose up -d

启动时间约 2~5 分钟,查看状态:

bash 复制代码
docker compose logs -f gitlab

看到 gitlab Reconfigured! 就是启动完成。


五、外部 Nginx 反向代理配置

创建 Nginx 配置文件

/etc/nginx/conf.d/gitlab.conf

nginx 复制代码
server {
    listen 80;
    server_name gitlab.yourdomain.com;

    # 强制跳转 HTTPS(可选,推荐)
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name gitlab.yourdomain.com;

    # SSL 证书(改成你自己的路径)
    ssl_certificate /etc/nginx/ssl/gitlab.crt;
    ssl_certificate_key /etc/nginx/ssl/gitlab.key;

    # SSL 优化
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256;

    # 反向代理到 GitLab
    location / {
        proxy_pass http://127.0.0.1:8929;
        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_read_timeout 3600;
    }

    # GitLab 大文件上传限制
    client_max_body_size 1024m;
    proxy_request_buffering off;
}

重载 Nginx

bash 复制代码
nginx -t
systemctl reload nginx

六、登录使用


七、常用维护命令

bash 复制代码
# 重启 GitLab
docker compose restart gitlab

# 停止 GitLab
docker compose down

# 查看日志
docker compose logs -f --tail=200 gitlab

# 修改配置后重新加载
docker compose up -d

八、注意事项

  1. 首次启动慢是正常的,GitLab 组件很多
  2. 服务器内存建议 ≥ 4GB,否则会卡顿
  3. 密码必须复杂度足够,否则初始化失败
  4. 全程无 SSH、无 22 端口、完全走 HTTP/HTTPS

总结

  1. 一键 docker-compose up -d 即可部署
  2. 内置 默认 root 密码,无需找自动生成密码
  3. 禁用 SSH,只走 HTTP/HTTPS
  4. 使用 外部 Nginx 代理,安全、灵活、易维护
  5. 端口只监听本地,公网无法直接访问,更安全

需要我帮你生成无 SSL 版本 (纯 HTTP)、或自动申请 Let's Encrypt 证书的脚本吗?

相关推荐
草青工作室1 小时前
Spring Boot 环境变量配置详解:从 IDEA 到 Docker 部署
spring boot·docker·intellij-idea
白鸽梦游指南1 小时前
docker部署和常规使用方法
运维·docker·容器
l1t10 小时前
用docker安装测试crate数据库
数据库·docker·容器·cratedb
枕书11 小时前
实战记录:如何使用 Docker 一键部署长亭 PandaWiki 智能知识库
运维·docker·容器
Irene199112 小时前
什么是 DevOps
gitlab·devops
Cyber4K13 小时前
【妙招系列】Harbor 镜像私有仓库搭建手册
linux·云原生·容器
IT199515 小时前
Docker笔记-对docker-compose.yml基本认识
笔记·docker·容器
supersolon17 小时前
WSL2(Linux)升级docker
linux·运维·docker·wsl·升级
一殊酒17 小时前
【Docker】常用命令大全及解析
docker·容器·eureka