DockerGitLinux

CentOS7にGitlab環境をコンテナで構築

Docker

CentOSの仮想マシン内にGitlabを構築してみます。

これまでであれば通常のパッケージをインストールしていましたが、最近はDockerコンテナを利用することで簡単に自宅内にGItlab環境を構築することができます。

今回はCentOS7にDockerCEおよびDocker-Composeをインストールして、GItlabのコンテナを構築まで実施したのでメモとして残しておきます。

環境

以下の環境にgitlabを構築したいと思います。メモリについては人数が多いと4GBは最低でも欲しいですが、今回はインストールだけなのでこれで進めます。

・構築場所:Windows10上のHyper-V上の仮想マシン
・OS:CentOS Linux release 7.8.2003
・CPU:2コア
・メモリ:2GB
・ホスト名:gitlab
・IPアドレス:192.168.10.31

環境準備

CentOS7については事前に用意されているものとします。私は以下の手順で構築済みです。

合わせて、FirewallやSELinuxを無効化しておきます。以下に手順を載せてきます。

Docker CEのインストール

まずDocker CEをインストールします。公式サイトの手順に従ってインストールしていきます。

Install Docker Engine on CentOS
Learn how to install Docker Engine on CentOS. These instructions cover the different installation methods, how to uninstall, and next steps.
[root@gitlab ~]# yum install -y yum-utils

[root@gitlab ~]# yum-config-manager \
>     --add-repo \
>     https://download.docker.com/linux/centos/docker-ce.repo

[root@gitlab ~]# yum install docker-ce docker-ce-cli containerd.io
(略)
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://download.docker.com/linux/centos/gpg
上記の処理を行います。よろしいでしょうか? [y/N]y
(略)

正常にインストールされたことを確認します。

[root@gitlab ~]# rpm -qa | grep docker
docker-ce-19.03.12-3.el7.x86_64
docker-ce-cli-19.03.12-3.el7.x86_64

dockerサービスを起動します。OS起動時も起動するようにします。

[root@gitlab gitlab]# systemctl start docker
[root@gitlab gitlab]# systemctl enable docker

Docker-Composeのインストール

次にDocker-Compseをインストールします。こちらも公式サイトに従います。

Overview of installing Docker Compose
Learn how to install Docker Compose. Compose is available natively on Docker Desktop, as a Docker Engine plugin, and as a standalone tool.

今回はGitlabのコンテナを単体で動かすだけなので、特になくてもコンテナ構築は簡単にできるのですが、私はdocker-composeのほうが管理しやすいので好んで使ってます。

[root@gitlab ~]# curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   638  100   638    0     0   2415      0 --:--:-- --:--:-- --:--:--  2416
100 11.6M  100 11.6M    0     0  1736k      0  0:00:06  0:00:06 --:--:-- 2472k
[root@gitlab ~]# chmod +x /usr/local/bin/docker-compose

正常にインストールされたことを確認します。パッケージでインストールしていないのでバージョン確認してみます。

[root@gitlab ~]# docker-compose --version
docker-compose version 1.26.2, build eefe0d31

gitlabコンテナの構築

ではDocker周りの準備ができたのでGitlabコンテナを構築していきます。

今回は/root/docker/gitlabというディレクトリ内にgitlabコンテナを構築するためのファイルを配置していきます。

構成ファイル(docker-compose.yml)の準備

[root@gitlab ~]# mkdir -p /root/docker/gitlab
[root@gitlab ~]# cd /root/docker/gitlab/
[root@gitlab gitlab]# vim docker-compose.yml
version: '3'
services:
  gitLab:
    image: gitlab/gitlab-ce:latest
    restart: always
    hostname: 'gitlab'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'https://gitlab.syachiku.local'
        gitlab_rails['gitlab_shell_ssh_port'] = 4022
    ports:
     - "80:80"
     - "443:443"
     - "4022:22"
    volumes:
     - './gitlab/config:/etc/gitlab'
     - './gitlab/logs:/var/log/gitlab'
     - './gitlab/data:/var/opt/gitlab'

コンテナの起動

docker-composeコマンドからコンテナを起動します。

[root@gitlab gitlab]# docker-compose up -d
Creating network "gitlab_default" with the default driver
Pulling gitLab (gitlab/gitlab-ce:latest)...
latest: Pulling from gitlab/gitlab-ce
6aa38bd67045: Pull complete
981ae4862c05: Pull complete
5bad8949dcb1: Pull complete
ca9461589e70: Pull complete
874f56cc5050: Pull complete
db4039798629: Pull complete
fc353e88adb4: Pull complete
413b7c7fd372: Pull complete
834c8b7e55e5: Pull complete
caccab5b0e36: Pull complete
Digest: sha256:820529ffe2ba47545882b541081b21c8340cd984ba2c434ebb40dc2a79666622
Status: Downloaded newer image for gitlab/gitlab-ce:latest
Creating gitlab_gitLab_1 ... done

正常に起動しているか確認してみます。

[root@gitlab gitlab]# docker-compose ps
     Name             Command               State                                       Ports
--------------------------------------------------------------------------------------------------------------------------
gitlab_gitLab_1   /assets/wrapper   Up (health: starting)   0.0.0.0:4022->22/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

もしコンテナ構築途中で以下のようなエラーが表示された場合には、再度docker-compose up -d コマンドを実行してみて下さい。

要はメッセージにある通りネットワークが遅いのでtimeoutしたということなので、2回目以降は成功するはずです。

ERROR:  for gitlab_gitlab_1   UnixHTTPConnectionPool(host='localhost' , port=None) : Read timed out. (read timeout=60)

ERROR:  for gitlab   UnixHTTPConnectionPool(host='localhost' , port=None) : Read timed out. (read timeout=60)
ERROR:  An HTTP request took too long to complete. Retry with --verbose to obtain debug information.
If you encounter this issue regularly because of slow network conditions, consider setteing COMPOSE_HTTP_TIMEOUT to a higher value (current value: 60)

Gitlabへログオン

構築が終わったらログオンしてみます。

コンテテが起動してから2,3分くらいgitlabが起動するまで待つ必要があります。

事前にgitlab.syachiku.localで名前解決ができるようDNSの設定もしくはHostsに記載にしておきます。

最初にrootのパスワードを設定してからログオンします。

ログオンできトップページが表示されました。

まとめ

CentOSにGitlabコンテナを構築する方法を紹介しました。

GitlabのインストーラーからCentOS上に直接構築するよりもはるかに早く環境構築ができました。やはりコンテナは便利ですね。

今回は以上となります。

コメント

タイトルとURLをコピーしました