# 使用 Docker 自架 GitLab

在本文中，我們將介紹如何使用 Docker 構建和運行自己的 GitLab 實例。GitLab 是一個開源的 DevOps 平台，提供了代碼管理、CI/CD、項目管理等功能。通過使用 Docker，我們可以快速輕鬆地部署 GitLab，並且可以輕鬆地對其進行擴展和定製。

### 步驟一：安裝 Docker 和 Docker Compose

首先，確保您的系統上安裝了 Docker 和 Docker Compose。您可以按照官方文檔的指引進行安裝：

* [Docker 官方文檔](https://docs.docker.com/get-docker/)
* [Docker Compose 官方文檔](https://docs.docker.com/compose/install/)

### 步驟二：創建 Docker Compose 文件

在您的項目目錄中創建一個名為 `docker-compose.yml` 的文件，並將以下內容添加到該文件中：

```yaml
version: '3'

services:
  gitlab:
    image: gitlab/gitlab-ce:15.8.0-ce.0
    container_name: gitlab
    restart: always
    privileged: true
    hostname: '172.20.160.120'
    ports:
      - '10777:80'
      - '10443:443'
      - '10022:22'
    volumes:
      - './gitlab/data:/var/opt/gitlab'
      - './gitlab/logs:/var/log/gitlab'
      - './gitlab/config:/etc/gitlab'
    networks:
      - local_net

networks:
  local_net:
    name: local-net
    driver: bridge
```

在這個 Docker Compose 文件中，我們定義了一個名為 `gitlab` 的服務，使用了 `gitlab/gitlab-ce` 映像。我們將 GitLab 的配置文件、日志文件和數據存儲到主機上的相應目錄中。這邊我使用的版本是`15.8.0-ce.0` 後續在測試時也都是使用這個版本的結果，所以如果讀者想要使用別的版本，遇到問題還是先以官方文件內容為主。

### 步驟三：啟動 GitLab 服務

在項目目錄中運行以下命令來啟動 GitLab 服務：

```bash
docker-compose up -d
```

這將在後台啟動 GitLab 服務。首次運行此命令時，Docker 將從 Docker Hub 下載 GitLab 映像，這可能需要一些時間。

### 步驟四：訪問 GitLab

一旦 GitLab 服務啟動完成，您就可以通過瀏覽器訪問 GitLab。默認情況下，GitLab 將在 `http://localhost` 上運行。首次訪問 GitLab 時，您需要設置 root 用戶的密碼。

完成這些步驟後，您就可以開始使用自己的 GitLab 服務了！您可以註冊其他用戶、創建項目、推送代碼等。

### 自定義和擴展

通過 Docker Compose 文件，您可以輕鬆地對 GitLab 進行自定義和擴展。例如，您可以配置反向代理、添加額外的插件或定製 GitLab 的配置。

## Gitlab 設定參數(gitlab.rb)

**Path:** /etc/gitlab/

主要是修改 clone url 問題，這邊有一些隱藏邏輯。

* `external_url` 如果有帶 port 則會用這個 port 去監聽，但因為我是要在 Docker container 自己，所以這邊使用要特別注意搭配 Nginx 的 listen port 設定調整回去。
* `nginx['listen_port'] = 80`

### 自架Server重設管理員密碼

首先就先連進 GitLab container 根據以下內容執行重設管理員密碼

```sh
root@localhost:/etc/init.d# cd ..
root@localhost:/etc# gitlab-rails console -e production
--------------------------------------------------------------------------------
Ruby:         ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5) [x86_64-linux]
GitLab:       15.8.0 (c052f86b6b4) FOSS
GitLab Shell: 14.15.0
PostgreSQL:   13.8
------------------------------------------------------------[ booted in 15.88s ]
Loading production environment (Rails 6.1.6.1)
irb(main):001:0> user = User.where(id:1).first
=> #<User id:1 @root>
irb(main):002:0> user.password = '1qaz2wsx'
=> "1qaz2wsx"
irb(main):003:0> user.password_confirmation = '1qaz2wsx'
=> "1qaz2wsx"
irb(main):004:0> user.save
=> false
irb(main):005:0> user.password = 'Caster123456'
=> "Caster123456"
irb(main):006:0> user.password_confirmation = 'Caster123456'
=> "Caster123456"
irb(main):007:0> user.save
=> true
irb(main):008:0>
```

成功重設使用者密碼，會再 `user.save` 回傳 `true`，這時就可以再開啟 <http://localhost:10777> 登入

### 結論

通過使用 Docker，我們可以快速輕鬆地構建和運行自己的 GitLab 實例。這使得 GitLab 的部署和管理變得非常簡單，同時也提供了豐富的自定義和擴展功能。現在您可以開始在自己的項目中使用 GitLab 了！

希望本文能夠對您有所幫助。祝您使用 GitLab 愉快！


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xu-min-chang.gitbook.io/caster-develop-note/gitlab/shi-yong-docker-zi-jia-gitlab.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
