4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MacでDockerコンテナ上にGitLabを構築する

Posted at

やろうとした理由

前回、Docker上にRedmineを入れたので、その流れでGitLabもやってみようと。

事前確認

1. Dockerイメージを確認する

Macのバージョンと、Dockerのバージョンは割愛。(前回やったから)

$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
mariadb                latest              8075b7694a2d        40 hours ago        407MB
redmine                latest              809721a71330        3 days ago          544MB
hybridcloudibm/wexdg   latest              b482197b4310        2 years ago         5.83GB

2. 起動中のDockerコンテナを確認する

Redmineとmariadbが動いている状態。

$ docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                       PORTS                                      NAMES
e9c787057ae4        redmine                   "/docker-entrypoint.…"   26 hours ago        Up 26 hours                  0.0.0.0:3000->3000/tcp                     redmine
8619aa4a4c36        mariadb                   "docker-entrypoint.s…"   26 hours ago        Up 26 hours                  3306/tcp                                   redmine-db
c522c16cdb88        hybridcloudibm/wexdg      "/opt/ibm/wex/sbin/e…"   22 months ago       Exited (255) 20 months ago   8000/tcp, 0.0.0.0:443->443/tcp, 9080/tcp   wex-d-g

3. docker-compose.ymlファイルを作成する

これがキモなので。

docker-compose.yml
version: '3.8'
services:
  gitLab:
    container_name: gitlab
    image: gitlab/gitlab-ce:latest
    ports:
     - "80:80"
    volumes:
     - ./GitLab/srv/gitlab/config:/etc/gitlab
     - ./GitLab/srv/gitlab/logs:/var/log/gitlab
     - ./GitLab/srv/gitlab/data:/var/opt/gitlab

DockerでGitLabを立ち上げる(docker-compose)

1. docker-composeコマンドを実行する

レッツゴー。

$ docker-compose up -d
Pulling gitLab (gitlab/gitlab-ce:latest)...
latest: Pulling from gitlab/gitlab-ce
6aa38bd67045: Pull complete
981ae4862c05: Pull complete
5bad8949dcb1: Pull complete
ca9461589e70: Pull complete
79b3488bd514: Pull complete
da7621effa84: Pull complete
c658ceb6fba5: Pull complete
1b64275b397c: Pull complete
204e4d0ddbcb: Pull complete
05f5900b030f: Pull complete
Digest: sha256:e7d76ace1548682d98be2a7f5863d5db147a6f5a3bf33339e8abd00a81f9dd08
Status: Downloaded newer image for gitlab/gitlab-ce:latest
Creating gitlab ... done

2. dockerイメージを確認する

$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
mariadb                latest              8075b7694a2d        40 hours ago        407MB
gitlab/gitlab-ce       latest              a7d268d6398b        2 days ago          1.91GB
redmine                latest              809721a71330        3 days ago          544MB
hybridcloudibm/wexdg   latest              b482197b4310        2 years ago         5.83GB

3. 起動中のDockerコンテナを確認する

$ docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                             PORTS                                      NAMES
87b7c0d54971        gitlab/gitlab-ce:latest   "/assets/wrapper"        20 seconds ago      Up 16 seconds (health: starting)   22/tcp, 443/tcp, 0.0.0.0:80->80/tcp        gitlab
e9c787057ae4        redmine                   "/docker-entrypoint.…"   26 hours ago        Up 26 hours                        0.0.0.0:3000->3000/tcp                     redmine
8619aa4a4c36        mariadb                   "docker-entrypoint.s…"   26 hours ago        Up 26 hours                        3306/tcp                                   redmine-db
c522c16cdb88        hybridcloudibm/wexdg      "/opt/ibm/wex/sbin/e…"   22 months ago       Exited (255) 20 months ago         8000/tcp, 0.0.0.0:443->443/tcp, 9080/tcp   wex-d-g

4. ブラウザで起動したGitLabを確認する

http://localhost:80で開きます。

20200726_IMG_0000.png

docker越しに、GitLabの構成情報バックアップコマンドを実行

GitLabで本当にやりたかったのはこっち。
CREATIONLINEさんの記事にあったコマンドを自分で打って動きを確認しておきたかった。

1. /etc直下を確認

$ docker exec -it 87b7c0d54971 ls -l /etc
total 444
drwxr-xr-x  3 root root    4096 Apr 12  2016 X11
-rw-r--r--  1 root root    3028 Jun 19 18:25 adduser.conf
drwxr-xr-x  1 root root    4096 Jul 23 21:12 alternatives
drwxr-xr-x  1 root root    4096 Jun 19 18:26 apt
-rw-r--r--  1 root root    2188 Aug 31  2015 bash.bashrc
drwxr-xr-x  2 root root    4096 Jun 19 18:25 bash_completion.d
-rw-r--r--  1 root root     367 Jan 27  2016 bindresvport.blacklist
drwxr-xr-x  2 root root    4096 Apr 12  2016 binfmt.d
drwxr-xr-x  3 root root    4096 Jul 23 21:12 ca-certificates
-rw-r--r--  1 root root    5678 Jul 23 21:12 ca-certificates.conf
drwxr-xr-x  2 root root    4096 Jun 19 18:25 cron.daily
drwxr-xr-x  2 root root    4096 Jun 19 18:25 cron.weekly
drwxr-xr-x  3 root root    4096 Apr 12  2016 dbus-1
-rw-r--r--  1 root root    2969 Nov 10  2015 debconf.conf
-rw-r--r--  1 root root      12 Apr 30  2015 debian_version
drwxr-xr-x  1 root root    4096 Jul 23 21:12 default
-rw-r--r--  1 root root     604 Jul  2  2015 deluser.conf
drwxr-xr-x  3 root root    4096 Jun 19 18:25 dhcp
drwxr-xr-x  1 root root    4096 Jun 19 18:25 dpkg
-rw-r--r--  1 root root      96 Jun 19 18:25 environment
-rw-r--r--  1 root root      37 Jun 19 18:25 fstab
-rw-r--r--  1 root root    2584 Feb 18  2016 gai.conf
drwxrwxr-x 11 root root     352 Jul 26 09:16 gitlab

〜省略〜

drwxr-xr-x  2 root root    4096 Apr 12  2016 udev
drwxr-xr-x  3 root root    4096 Jul 23 21:12 ufw
drwxr-xr-x  2 root root    4096 Jul 23 21:12 vim
-rw-r--r--  1 root root    4942 Apr  8  2019 wgetrc
drwxr-xr-x  3 root root    4096 Apr 12  2016 xdg

2. /etc/gitlab直下を確認

$ docker exec -it 87b7c0d54971 ls -l /etc/gitlab
total 152
-rw------- 1 root root  18892 Jul 26 09:25 gitlab-secrets.json
-rw------- 1 root root 110359 Jul 26 09:16 gitlab.rb
-rw------- 1 root root    227 Jul 26 09:16 ssh_host_ecdsa_key
-rw-r--r-- 1 root root    179 Jul 26 09:16 ssh_host_ecdsa_key.pub
-rw------- 1 root root    411 Jul 26 09:16 ssh_host_ed25519_key
-rw-r--r-- 1 root root     99 Jul 26 09:16 ssh_host_ed25519_key.pub
-rw------- 1 root root   1675 Jul 26 09:16 ssh_host_rsa_key
-rw-r--r-- 1 root root    399 Jul 26 09:16 ssh_host_rsa_key.pub
drwxr-xr-x 2 root root     64 Jul 26 09:16 trusted-certs

3. GitLab構成情報のバックアップ取得

何か出たけど、ちゃんとバックアップは取れていそう。

$ docker exec -it 87b7c0d54971 tar cvzf /etc/20200726backup.tar.gz /etc/gitlab
tar: Removing leading `/' from member names
/etc/gitlab/
/etc/gitlab/trusted-certs/
/etc/gitlab/ssh_host_rsa_key
/etc/gitlab/ssh_host_ed25519_key
/etc/gitlab/gitlab-secrets.json
/etc/gitlab/ssh_host_ecdsa_key.pub
/etc/gitlab/ssh_host_rsa_key.pub
/etc/gitlab/ssh_host_ed25519_key.pub
/etc/gitlab/gitlab.rb
/etc/gitlab/ssh_host_ecdsa_key

4. 作成したtarファイルをローカルにコピー

$ docker cp 87b7c0d54971:/etc/20200726backup.tar.gz ./20200726backup2.tar.gz
$ 
$ ls -l
total 88
-rw-r--r--  1 hogehoge  staff  43914  7 26 21:28 20200726backup2.tar.gz
drwxr-xr-x  3 hogehoge  staff     96  7 26 18:16 srv

5. アプリケーションデータのバックアップ取得

データが入っていないのに、結構時間がかかった。
Warningは、以前のバックアップが存在しないから?
時間はUTCなので、9時間ずれています。

$ docker exec -it 87b7c0d54971 gitlab-rake gitlab:backup:create
2020-07-26 12:50:56 +0000 -- Dumping database ... 
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2020-07-26 12:52:37 +0000 -- done
2020-07-26 12:52:37 +0000 -- Dumping repositories ...
2020-07-26 12:52:39 +0000 -- done
2020-07-26 12:52:39 +0000 -- Dumping uploads ... 
2020-07-26 12:52:39 +0000 -- done
2020-07-26 12:52:39 +0000 -- Dumping builds ... 
2020-07-26 12:52:39 +0000 -- done
2020-07-26 12:52:39 +0000 -- Dumping artifacts ... 
2020-07-26 12:52:39 +0000 -- done
2020-07-26 12:52:39 +0000 -- Dumping pages ... 
2020-07-26 12:52:39 +0000 -- done
2020-07-26 12:52:39 +0000 -- Dumping lfs objects ... 
2020-07-26 12:52:39 +0000 -- done
2020-07-26 12:52:39 +0000 -- Dumping container registry images ... 
2020-07-26 12:52:39 +0000 -- [DISABLED]
Creating backup archive: 1595767959_2020_07_26_13.2.1_gitlab_backup.tar ... done
Uploading backup archive to remote storage  ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... skipping
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data 
and are not included in this backup. You will need these files to restore a backup.
Please back them up manually.
Backup task is done.
$ 
$ docker exec -it 87b7c0d54971 ls -l /var/opt/gitlab/backups
total 200
-rw------- 1 root root 204800 Jul 26 12:52 1595767959_2020_07_26_13.2.1_gitlab_backup.tar
$ 
$ docker exec -it 87b7c0d54971 date
Sun Jul 26 12:54:05 UTC 2020

6. バックアップファイルの保存先変更

大体、470行目くらいに出てきます。

$ docker exec -it 87b7c0d54971 vi /etc/gitlab/gitlab.rb
gitlab.rb(BEFORE)
### Backup Settings
###! Docs: https://docs.gitlab.com/omnibus/settings/backups.html

# gitlab_rails['manage_backup_path'] = true
# gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
gitlab.rb(AFTER)
### Backup Settings
###! Docs: https://docs.gitlab.com/omnibus/settings/backups.html

gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups2020"

7. 設定を反映

これをやっておくことが重要なようです。

$ docker exec -it 87b7c0d54971 gitlab-ctl reconfigure
Starting Chef Infra Client, version 15.9.17
resolving cookbooks for run list: ["gitlab"]
Synchronizing Cookbooks:
  - gitlab (0.0.1)
  - redis (0.1.0)
  - package (0.1.0)
  - registry (0.1.0)
  - postgresql (0.1.0)
  - consul (0.1.0)
  - gitaly (0.1.0)
  - letsencrypt (0.1.0)
  - runit (5.1.3)
  - monitoring (0.1.0)
  - mattermost (0.1.0)
  - crond (0.1.0)
  - nginx (0.1.0)
  - praefect (0.1.0)
  - acme (4.1.1)
Installing Cookbook Gems:

〜省略〜

Recipe: gitlab::puma
  * runit_service[puma] action restart (up to date)
  * runit_service[puma] action restart (up to date)
Recipe: gitlab::sidekiq-cluster
  * sidekiq_service[sidekiq] action restart
  Recipe: <Dynamically Defined Resource>
    * service[sidekiq] action nothing (skipped due to action :nothing)
    * runit_service[sidekiq] action restart (up to date)
     (up to date)
Recipe: gitlab::gitlab-rails
  * execute[clear the gitlab-rails cache] action run
    - execute /opt/gitlab/bin/gitlab-rake cache:clear

Running handlers:
Running handlers complete
Chef Infra Client finished, 9/728 resources updated in 01 minutes 45 seconds
gitlab Reconfigured!

8. アプリケーションデータのバックアップ再取得

もう一度バックアップを取得してみます。

$ docker exec -it 87b7c0d54971 gitlab-rake gitlab:backup:create
2020-07-26 13:16:06 +0000 -- Dumping database ... 
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2020-07-26 13:16:50 +0000 -- done
2020-07-26 13:16:50 +0000 -- Dumping repositories ...
2020-07-26 13:16:51 +0000 -- done
2020-07-26 13:16:51 +0000 -- Dumping uploads ... 
2020-07-26 13:16:51 +0000 -- done
2020-07-26 13:16:51 +0000 -- Dumping builds ... 
2020-07-26 13:16:51 +0000 -- done
2020-07-26 13:16:51 +0000 -- Dumping artifacts ... 
2020-07-26 13:16:51 +0000 -- done
2020-07-26 13:16:51 +0000 -- Dumping pages ... 
2020-07-26 13:16:51 +0000 -- done
2020-07-26 13:16:51 +0000 -- Dumping lfs objects ... 
2020-07-26 13:16:51 +0000 -- done
2020-07-26 13:16:51 +0000 -- Dumping container registry images ... 
2020-07-26 13:16:51 +0000 -- [DISABLED]
Creating backup archive: 1595769411_2020_07_26_13.2.1_gitlab_backup.tar ... done
Uploading backup archive to remote storage  ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... skipping
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data 
and are not included in this backup. You will need these files to restore a backup.
Please back them up manually.
Backup task is done.

9. バックアップの保存先が変わったことの確認

/var/opt/gitlab/backupsを確認して、先ほど作成したバックアップしか無いこと、新しいバックアップが存在しないことを確認します。

$ docker exec -it 87b7c0d54971 ls -l /var/opt/gitlab/backups
total 200
-rw------- 1 root root 204800 Jul 26 12:52 1595767959_2020_07_26_13.2.1_gitlab_backup.tar

新しく指定したフォルダに格納されていることを確認します。

$ docker exec -it 87b7c0d54971 ls -l /var/opt/gitlab/backups2020
total 200
-rw------- 1 root root 204800 Jul 26 13:16 1595769411_2020_07_26_13.2.1_gitlab_backup.tar

感想

dockerでGitLabを構築するのは、そんなに苦労せずにできました。
やはり慣れると便利・・・。
オーケストレーションを理解していないので、次はKubernatesですかね。

GitLabのバックアップコマンドは、一度動きを見ておきたかったので、docker越しだけど実行できてよかった。
あとは、データ量があるものをバックアップしようとしたときに、どのくらいの時間がかかるのか・・・ですね。
それは会社の仮想サーバ上で試すことにします。

参考にさせていただいた記事など

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?