はじめに
ローカルでGitLabのAPIを試してみたくなったので、GitLabをローカルで使えるように
仮想マシン上にDocker+GitLabをインストールしてみようと思います。
環境
以下の環境にDockerとGitLabをインストールします。
仮想マシン | VirtualBox 6.1 |
メモリ | 8 GB |
プロセッサー数 | 4 CPU |
ストレージ | 20 GB |
OS | Ubuntu 20.04.3 LTS |
Dockerインストール
以下のサイトを参考にして、Dockerをインストールします。
このようになっている部分は、上記ページの翻訳です。
旧バージョンのアンインストール(Uninstall old versions)
古いバージョンのDockerは、docker、docker.io、docker-engineと呼ばれていました。これらがインストールされている場合は、アンインストールしてください。
$ sudo apt-get remove docker docker-engine docker.io containerd runc
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
E: パッケージ docker-engine が見つかりません
apt-getがこれらのパッケージが一つもインストールされていないことを報告しても問題ありません。
リポジトリを使ってインストールする(Install using the repository)
新しいホストマシンに初めてDocker Engineをインストールする前に、Dockerリポジトリをセットアップする必要があります。その後、リポジトリからDockerをインストールおよびアップデートすることができます。
リポジトリのセットアップ(Set up the repository)
1 . apt パッケージインデックスをアップデートし、apt が HTTPS 上のリポジトリを使用できるようにパッケージをインストールします。
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
2 . Dockerの公式GPGキーを追加します。
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
3 . 安定版リポジトリの設定には、以下のコマンドを使用します。
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Docker Engineのインストール(Install Docker Engine)
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
hello-world イメージを実行して、Docker Engine が正しくインストールされていることを確認します。
sudo docker run hello-world
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:507ecde44b8eb741278274653120c2bf793b174c06ff4eaa672b713b3263477b
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
GitLabインストール
Dockerイメージ取得
まずは、Dockerイメージを取得します。
GitLabには、有料版と無料版があるらしく、無料版のgitlab-ce
を取得します。
sudo docker pull gitlab/gitlab-ce
$ sudo docker pull gitlab/gitlab-ce
Using default tag: latest
latest: Pulling from gitlab/gitlab-ce
08c01a0ec47e: Pull complete
b4b17aed4674: Pull complete
17b2f2a84d0d: Pull complete
e971383e4a60: Pull complete
d2bb4cf9ad02: Pull complete
9be5e8b7d281: Pull complete
be2f8a079d21: Pull complete
57c0a2b88a62: Pull complete
Digest: sha256:fddf07e3baede8f4935330440c2d5bc4e08c0afb11d91929276bb6581bbf2bab
Status: Downloaded newer image for gitlab/gitlab-ce:latest
docker.io/gitlab/gitlab-ce:latest
Dockerイメージ実行
以下のサイトを参考にして、Dockerイメージを実行します。
※:サイト内ではee(有料版)となっているので、ce(無料版)に置き換えて修正します。
ボリュームの場所を設定する(Set up the volumes location)
他のすべてを設定する前に、設定、ログ、およびデータファイルが存在するディレクトリを指す新しい環境変数 $GITLAB_HOME を設定します。そのディレクトリが存在し、適切なパーミッションが付与されていることを確認します。
以下は、初回だけで良いはずです。
sudo mkdir /srv/gitlab
Linuxユーザの場合、パスは/srv/gitlabに設定します。
export GITLAB_HOME=/srv/gitlab
Docker Engineを使用してGitLabをインストール(Install GitLab using Docker Engine)
ce用にカスタマイズしたもので実行してみます。
sudo docker run --detach \
--hostname localhost \
--publish 443:443 --publish 80:80 --publish 22:22 \
--name gitlab \
--restart always \
--volume $GITLAB_HOME/config:/etc/gitlab \
--volume $GITLAB_HOME/logs:/var/log/gitlab \
--volume $GITLAB_HOME/data:/var/opt/gitlab \
--shm-size 256m \
gitlab/gitlab-ce:latest
Dockerを実行しても、起動に数分かかる為、ばらく待つ必要があります。
以下のURLにアクセスします。
http://localhost
gitlabが起動完了したら、以下の画面となります。
ログインして設定します。
root
のパスワード
root
のパスワードは以下のファイルに記載されています。
/srv/gitlab/config/initial_root_password
$ sudo cat /srv/gitlab/config/initial_root_password
# WARNING: This value is valid only in the following conditions
# 1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
# 2. Password hasn't been changed manually, either via UI or via command line.
#
# If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.
Password: <rootのパスワード>
# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
注:このファイルは、24時間後の最初の再構成実行で自動的に削除されます。
どうやら自動的に削除されるみたいです。
Docker+GitLabの初期化
実際に、設定をミスっていたり、起動に待ちきれず何回か初期化を行いましたので、メモとして手順を記します。
CONTAINER ID
の取得
以下のコマンドからCONTAINER ID
を取得します。
sudo docker ps -a
xxxxxxxxxxx
やyyyyyyyyyyyy
は16進数の文字列となります。
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xxxxxxxxxxxx gitlab/gitlab-ce:latest "/assets/wrapper" 16 hours ago Up 16 hours (healthy) 0.0.0.0:22->22/tcp, :::22->22/tcp, 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp gitlab
yyyyyyyyyyyy hello-world "/hello" 17 hours ago Exited (0) 17 hours ago sleepy_tu
Dockerで動いているgitlabの停止
以下のコマンドからDockerで動いているgitlabを停止します。
sudo docker stop <CONTAINER ID>
※:<CONTAINER ID>
は先ほど一覧で出てきたCONTAINER ID
です。
$ sudo docker stop xxxxxxxxxxxx
xxxxxxxxxxxx
一応、sudo docker ps -a
で確認します。
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xxxxxxxxxxxx gitlab/gitlab-ce:latest "/assets/wrapper" 16 hours ago Exited (137) 48 seconds ago gitlab
yyyyyyyyyyyy hello-world "/hello" 17 hours ago Exited (0) 17 hours ago
STATUS
がExited (137)
になっているので停止しています。
Dockerからgitlabの削除
以下のコマンドからDocker上のgitlabを削除します。
sudo docker rm <CONTAINER ID>
※:<CONTAINER ID>
は先ほど一覧で出てきたCONTAINER ID
です。
$ sudo docker rm xxxxxxxxxxxx
xxxxxxxxxxxx
一応、sudo docker ps -a
で確認します。
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
yyyyyyyyyyyy hello-world "/hello" 17 hours ago Exited (0) 17 hours ago sleepy_tu
消えている事がわかります。
データの消去
ボリュームの場所(ここでは、/srv/gitlab
)にあるデータを削除します。
以下のコマンドで削除できます。
sudo rm -r $GITLAB_HOME/*
ポートフォワード
仮想マシンのホストからアクセスする為にポートフォワードの設定をします。
仮想マシンから「仮想マシン」→「設定...」を押下します。
左の一覧から、「ネットワーク」を選択します。
「高度」を押下します。
「ポートフォワーディング(P)」を押下します。
「名前」「ホストポート」「ゲストポート」を設定します。
※:ここでは、ホストから8080ポートを通して、ゲストの80ポートにアクセスします。
設定したら、「OK」ボタンを押下します。
ホストから以下のURLにアクセスすると、GitLabの画面が出てきます。
http://localhost:8080/
まとめ
何回か設定に失敗して迷走しましたが、なんとかDocker+GitLabが設定する事ができました。
次はGitLab APIを使ってみたいと思います。