[WSL2対応]gitlabをdocker composeで動かす(gitlab container registry, gitlab pagesあり)

### 



### 事前準備

docker compose(V2)が必要なので、wslの場合は、下記の通り準備する。

```powershell
# wsl-ubuntu
wsl --install --distribution Ubuntu-22.04
wsl --set-default Ubuntu-22.04

# Ubuntu-22.04がインストールできない場合、代わりに下記手順でインストールする
wsl --install
wsl --update
winget install 9PN20MSR04DW
wsl --set-default Ubuntu-22.04

# docker-desktop
winget install Docker.DockerDesktop
```

### docker-compose

collaborageをベースに、下記の改良を行いました。

- GitLabバージョンを14.10にアップし、maven/コンテナレジストリに対応する
    - postgresも13.3に変更
    - GitLab-pagesを使用できるようにする
    - nexusを削除する
    - 使う見込みのない機能を停止する(必要に応じて外付けする)
- proxyをapacheではなくtraefikに変更、httpd.confなどの不要な構成ファイルを削除
- docker-composeをcompose-spec対応形式に変更

https://github.com/Fintan-contents/collaborage/blob/master/src/common/docker/ci-on-gitlab/docker-compose.yml


```yaml
# compose-spec

x-logging: &default-logging
  logging:
    options:
      max-size: "10m"
      max-file: "10"

services:

  proxy:
    container_name: proxy
    image: traefik:2.7.0-rc2
    restart: always
    env_file: ./common.env
    environment:
      no_proxy: gitlab
    command:
      - --entrypoints.ssh.address=:22
      - --entrypoints.web.address=:80
      - --providers.docker
      - --providers.docker.exposedByDefault=false
      # [OPTION]prometheus監視エンドポイントを開く。不要であれば無効化してよい
      - --entryPoints.metrics.address=:8082
      - --metrics.prometheus=true
      - --metrics.prometheus.entryPoint=metrics
      # [OPTION]ダッシュボード。必ず本番環境では無効にすること
      - --api.insecure=true
    ports:
      - "22:22"
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    <<: *default-logging

  gitlab:
    container_name: gitlab
    image: gitlab/gitlab-ce:14.10.0-ce.0
    restart: always
    env_file: ./common.env
    volumes:
      - gitlab-config:/etc/gitlab
      - gitlab-log:/var/log/gitlab
      - gitlab-data:/var/opt/gitlab
    depends_on:
      - gitlab-db
    hostname: 'gitlab.examlples.com'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://gitlab.examlples.com/'
        gitlab_rails['time_zone'] = 'Tokyo'
        gitlab_rails['db_adapter'] = 'postgresql'
        gitlab_rails['db_host'] = 'gitlab-db'
        gitlab_rails['db_port'] = 5432
        gitlab_rails['db_database'] = 'gitlab'
        gitlab_rails['db_username'] = 'gitlab'
        gitlab_rails['db_password'] = 'gitlab'
        nginx['listen_port'] = 80
        nginx['listen_https'] = false
        nginx['proxy_set_headers'] = {
          "X-Forwarded-Proto" => "https",
          "X-Forwarded-Ssl" => "on"
        }
        
        #[OPTION]gitlab container registryを有効化することで、コンテナをローカルに保有できる
        registry_external_url 'http://registry.examlples.com'
        gitlab_rails['registry_enabled'] = true
        gitlab_rails['registry_api_url'] = "http://localhost:5000"
        registry['enable'] = true
        registry_nginx['enable'] = false
        registry['registry_http_addr'] = "0.0.0.0:5000"
        
        #[OPTION]pages機能を有効化することで、ブラウザでhtmlレポートが読めるようになる
        #pagesを使用する場合、生IPは使用不可。
        pages_external_url 'http://gitlab.examlples.com/'
        gitlab_pages['enable'] = true
        gitlab_pages['access_control'] = true
        gitlab_pages['redirect_http'] = false
        #proxy=>pagesのアクセス経路
        pages_nginx['enable'] = false
        gitlab_pages['external_http'] = ['0.0.0.0:8081']
        
        #[OPTION]リソース軽減するために不要な機能を停止
        gitlab_rails['gitlab_kas_enabled'] = false
        grafana['enable'] = false
        prometheus_monitoring['enable'] = false
        prometheus['enable'] = false
        alertmanager['enable'] = false
      no_proxy: proxy
    <<: *default-logging
    labels:
      - "traefik.enable=true"
      # ssh(git)
      - "traefik.tcp.services.gitlab-ssh.loadbalancer.server.port=22"
      - "traefik.tcp.routers.gitlab-ssh.service=gitlab-ssh"
      - "traefik.tcp.routers.gitlab-ssh.rule=HostSNI(`*`)"
      - "traefik.tcp.routers.gitlab-ssh.entrypoints=ssh"
      # http
      - "traefik.http.services.gitlab.loadbalancer.server.port=80"
      - "traefik.http.routers.gitlab.service=gitlab"
      - "traefik.http.routers.gitlab.rule=Host(`gitlab.examlples.com`)"
      - "traefik.http.routers.gitlab.entrypoints=web"
      # http-registry
      - "traefik.http.services.gitlab-registry.loadbalancer.server.port=5000"
      - "traefik.http.routers.gitlab-registry.service=gitlab-registry"
      - "traefik.http.routers.gitlab-registry.rule=Host(`registry.examlples.com`)"
      - "traefik.http.routers.gitlab-registry.entrypoints=web"
      # http-pages
      - "traefik.http.services.gitlab-pages.loadbalancer.server.port=8081"
      - "traefik.http.routers.gitlab-pages.service=gitlab-pages"
      - "traefik.http.routers.gitlab-pages.rule=Host(`foogroupname.gitlab.examlples.com`) || Host(`projects.gitlab.examlples.com`)"
      - "traefik.http.routers.gitlab-pages.entrypoints=web"

  gitlab-db:
    container_name: gitlab-db
    image: postgres:13.3-alpine3.14
    restart: always
    environment:
      POSTGRES_DB: gitlab
      POSTGRES_USER: gitlab
      POSTGRES_PASSWORD: gitlab
    volumes:
      - gitlab-db:/var/lib/postgresql/data
    <<: *default-logging

  gitlab-runner:
    container_name: gitlab-runner
    image: gitlab/gitlab-runner:ubuntu-v14.10.0
    restart: always
    volumes:
      - gitlab-runner-config:/etc/gitlab-runner
      - /var/run/docker.sock:/var/run/docker.sock
    <<: *default-logging
    extra_hosts:
      - "gitlab.examlples.com:192.168.***.****"
      - "registry.examlples.com:192.168.***.****"

volumes:
  gitlab-config:
  gitlab-log:
  gitlab-data:
  gitlab-db:
  gitlab-runner-config:

```

### クライアント側のhosts構成

hostsファイルに下記を追加する

```
192.168.***.**** gitlab.examlples.com registry.examlples.com foogroupname.gitlab.examlples.com projects.gitlab.examlples.com
```

### 実行

```
docker compose up -d
```

## 実行結果確認

### traefik dashboard

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/dcfe7736-097f-5489-f6e7-9bb2d69a6dfe.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/bcdcaf49-796a-f3cb-099b-ea123ea27a11.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/5696c7c4-36da-c83e-f861-48ec1fe0a524.png)


### Git

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/f27fb48f-d773-ca2e-d667-e939c22c0a4d.png)

### Gitlab-runner

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/0abaf32b-4c46-74be-05b8-45de7c2691f9.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/4c2c1416-a002-3080-0952-43f2e24ec30e.png)


### Container Registry

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/b24cf9cd-9662-fd87-d4b4-5574b12e304c.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/edd7bce6-857f-da86-0fab-4a382d65ed54.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/6579558b-23ba-1b5f-4d1e-9120428282f0.png)

### pages

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/161516/a3d2bab8-7420-406a-6f14-a1692bad4476.png)