You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法从自定义端口6022的自建GitLab实例克隆仓库求助

自定义端口6022的自建GitLab仓库克隆失败解决方法

问题描述

尝试从运行在自定义端口6022的自建GitLab实例克隆仓库失败,已执行以下操作但均无效:

  • 通过lsof、netstat确认GitLab实例在6022端口正常运行
  • 创建SSH密钥并添加至GitLab账户
  • 使用GitLab提供的SSH URL克隆:
git clone git@mygitlabinstance.com:6022/my-username/my-repo.git
  • 使用-p参数指定端口克隆:
git clone -p 6022 git@mygitlabinstance.com:my-username/my-repo.git

执行后均出现错误:

Cloning into 'my-repo'...
ssh: connect to host mygitlabinstance.com port 6022: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Docker Compose配置如下:

version: '3.7'
services:
  web:
    image: 'gitlab/gitlab-ee:latest'
    restart: always
    hostname: 'gitlab.example.com'
    container_name: gitlab-ee
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        #external_url 'https://gitlab.example.com'
        #external_url 'http://localhost:80'
        gitlab_rails['smtp_enable'] = true
        gitlab_rails['smtp_address'] = "***"
        gitlab_rails['smtp_port'] = 587
        gitlab_rails['smtp_user_name'] = "developer@*.com"
        gitlab_rails['smtp_password'] = "**"
        gitlab_rails['smtp_domain'] = "**.com"
        gitlab_rails['smtp_authentication'] = "login"
        gitlab_rails['smtp_enable_starttls_auto'] = true
        gitlab_rails['smtp_tls'] = false
        gitlab_rails['smtp_openssl_verify_mode'] = 'peer'
        gitlab_rails['gitlab_shell_ssh_port'] = 6022
        nginx['redirect_http_to_https'] = true
        nginx['ssl_certificate'] = "/etc/gitlab/ssl/fullchain.pem"
        nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/privkey.pem"
        nginx['ssl_dhparam'] = "/etc/gitlab/ssl/dhparams.pem"
        # HTTPS Setup
        #letsencrypt['enable'] = false
        #external_url 'https://gitlab.example.com'
        #gitlab_rails['gitlab_https'] = true
        #gitlab_rails['gitlab_port'] = 443
    ports:
      - '8081:80'
      - '8443:443'
      - '6022:22'
      - '587:587'
    volumes:
      - '$GITLAB_HOME/config:/etc/gitlab'
      - '$GITLAB_HOME/logs:/var/log/gitlab'
      - '$GITLAB_HOME/data:/var/opt/gitlab'
      - '$GITLAB_HOME/config/ssl:/etc/gitlab/ssl'
        #networks:

解决方案

方法1:使用正确的SSH端口克隆格式

Git SSH克隆命令中,冒号默认用于分隔主机和仓库路径,不能直接用来指定端口。正确的端口指定格式是通过ssh://协议前缀:

git clone ssh://git@mygitlabinstance.com:6022/my-username/my-repo.git

方法2:配置SSH全局默认端口

在本地~/.ssh/config文件中添加以下配置,后续克隆该GitLab实例的仓库时无需手动指定端口:

Host mygitlabinstance.com
  Port 6022
  User git

保存配置后,直接使用标准克隆命令即可:

git clone git@mygitlabinstance.com:my-username/my-repo.git

方法3:修复GitLab外部URL配置

你的Docker Compose中external_url被注释,这会导致GitLab生成的SSH URL格式错误。取消注释并配置包含端口的正确外部URL:

external_url 'https://mygitlabinstance.com:8443'

配置完成后重启GitLab容器:

docker-compose restart web

此时GitLab界面显示的SSH URL会自动适配端口格式,直接复制使用即可。

关键说明

  • Docker映射的6022:22是将宿主机6022端口转发到容器内的默认SSH端口22,外部访问必须使用6022端口。
  • Git克隆命令的-p参数是指定--progress(显示克隆进度),并非用于指定SSH端口,之前的用法错误。

内容的提问来源于stack exchange,提问作者Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 16:06:28