DEV Community

Cover image for Windows 上,本機用 Docker 安裝 GitLab
Let's Write
Let's Write

Posted on • Originally published at letswrite.tw

Windows 上,本機用 Docker 安裝 GitLab

本篇要解決的問題

有時公司會使用自架的 Git Server,而不是直接使用現有的 GitHub、GitLab,本篇是使用 Docker,在本機安裝 GitLab,再加上 GitLab Runner,這樣就可以有自己的版控功能,也可以寫檔案做自動化測試。

跟一般教學文不同的是,大部份自架 GitLab,都會講到怎麼裝上 SSL。

但 August 實測後發現,本機產生的憑證會一直有問題,即便用的是 OpenSSL,也沒有辦法推拉專案,所以本篇不會有裝 SSL 的部份。

如果有知道怎麼在本機 Docker 裝 SSL 的棒油,歡迎留言提供方式。


準備好 docker-compose.yml

新增一個名為「gitlab」的資料,裡面新增三個資料夾,以及一個 yml 檔,如下:

gitlab
  ├─config
  ├─data
  ├─logs
  └─docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

docker-compose.yml 的內容:

services:
  gitlab:
    image: gitlab/gitlab-ce
    container_name: gitlab-docker
    hostname: "gitlab.enrf"
    restart: always
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url "http://192.168.11.111:8807"
        letsencrypt['enable'] = false
    volumes:
      - "C:/Docker/gitlab/gitlab/config:/etc/gitlab"
      - "C:/Docker/gitlab/gitlab/logs:/var/log/gitlab"
      - "C:/Docker/gitlab/gitlab/data:/var/opt/gitlab"
    ports:
      - "8807:8807"
      - "8808:22"
Enter fullscreen mode Exit fullscreen mode

external_url 'https://192.168.11.111:8807',這邊的 IP 請改成自己的 IP,因為目標是建起來後,同網域的大家都可以使用,所以填寫的是 192.168 開頭的。

Port 的部份,因為 August 本機的 80、22 都被別的程式佔用了,所以改用別的 Port。

開啟終端機,執行:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

執行完後,當看到 Container 被建起來了,大概還要再等 5 分鐘,網站才打得開。

打開瀏覽器,網址輸入我們寫在 docker-compose.yml 上的,像這邊的範例就是:http://192.168.11.127:8807

GitLab 登入頁面.png


找出管理員預設密碼

看到了 GitLab 架好後,問題來了,GitLab 預設會提供一組管理員的帳號,登入這個帳號的帳密是什麼?

預設帳號是:root。

密碼的部份,我們的資料夾裡,剛有開了一個「config」的空資料夾,當 GitLab 安裝好,會看到裡面多了很多檔案,其中有一個叫:

initial_root_password

打開這個檔案,就會看到預設密碼:

預設密碼.png


安裝 GitLab Runner

GitLab Runner 要另外安裝,不直接裝在 GitLab 的專案裡。

新開名為「gitlab-runner」的資料夾,裡面放一個 docker-compose.yml 的檔案。

docker-compose.yml 的內容:

services:
  gitlab-runner:
    image: "gitlab/gitlab-runner:latest"
    container_name: "gitlab-runner"
    restart: always
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "D:/Docker/gitlab-runner/gitlab-runner/config:/etc/gitlab-runner"
    environment:
      - "CI_SERVER_URL={GitLab URL}"L
      - "REGISTRATION_TOKEN={Runner 註冊 token}"
Enter fullscreen mode Exit fullscreen mode

有二個地方要修改。

第一個,CI_SERVER_URL 要填寫的是我們 GitLab 的網址,像本篇的範例就是填:http://192.168.11.111:8807。

第二個,REGISTRATION_TOKEN,這要從 GitLab 後台建立 Runner 時取得,步驟如下。

1 登入管理員帳號後,照下圖的順序點擊:

新增runner.png

接著會開始填寫 Runner 的資料,都選好、填好後,點擊「創建執行器」,下一個畫面,就會看到 token:

token.png

把 token 貼上 docker-compose.yml 上,然後開啟終端機,執行:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

執行完後,Container 上就會看到 gitlab-runner 正在運行。

接著終端機上繼續輸入以下並執行:

docker-compose exec gitlab-runner gitlab-runner register
Enter fullscreen mode Exit fullscreen mode

執行後,會需要回答幾個問題。

URL、token,因為我們在運行 docker-compose 時都先填了,所以可以直接按下 Enter 確認。

後續的題目,因為 August 這邊只是為了測試 Runner 能不能執行,所以都先簡單回答:

Enter an executor: docker
Enter the default Docker image: alpine:latest
Enter fullscreen mode Exit fullscreen mode

建立成功時,會顯示一個訊息:

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
Enter fullscreen mode Exit fullscreen mode

意思就是 Runner 的 config 檔已經建立在 /etc/gitlab-runner/config.toml 裡。

我們所在的 gitlab-runner 資料夾,也會看到這個檔案,打開來會看到內容。


測試 Runner

為了測試 Runner 有沒有活跳跳,我們建一個專案來測試。

建專案時可以直接用 GitLab 提供的範本,這邊 August 選用「Pages/Plain HTML」這個。

建好後,把專案 clone 到我們本機,因為是範本建立的,所以本身就會有一個「.gitlab-ci.yml」,這個檔就是觸發我們 Runner 用的。

範本裡的這個檔案,原本的寫法是把專案架成靜態頁面,因為我們沒有做其他配置,所以執行起來一定會失敗。

幾年前 August 有寫一篇,是怎麼用 GitLab 來架靜態頁的方法,有興趣的朋友再自行研究囉:

GitLab Pages:3 + 3 個步驟讓 GitLab 專案產生靜態頁

這邊是為了測試,我們簡單的用個 tidy 來測試 html,把 .gitlab-ci.yml 的內容改成以下:

stages:
  - test

test_job:
  image: alpine:latest
  stage: test
  before_script:
    - apk update
    - apk add tidyhtml
  script:
    - echo "Running basic test..."
    - find . -name "*.html" | xargs tidy -q -e
  tags:
    - runner
Enter fullscreen mode Exit fullscreen mode

tags 寫 runner,是 August 在建立 Runner 時,有寫要認這個 tag 才會執行 Runner。

修改好後,推上 GitLab。

進到 GitLab 後台的專案裡,左側導覽列點擊「建置」>「流水線」,專案通過、失敗,都會看到圖示:

pipelnie.png

以上,便是怎麼在 Windows 的本機,用 Docker 安裝 GitLab,以及 GitLab Runner。

最後祝大家母親節快樂~

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay