GitlabとRedmineを1つのポートでたてる
前の記事でGitlabのサーバーを立てました。
次はRedmineを立てたいです。
ローカルで運用するくらいなら別のポートで立てれば良いですが、HTTPSで外部に立てることを考えると同一ドメインで立てる方が好都合なので、せっかくならサブディレクトリで立てたいです。
こんな感じ↓
-
http://localhost/gitlab
→ Gitlab -
http://localhost/redmine
→ Redmine
とりあえず別ポートでたてる
いったんサブディレクトリは考えず別のポートで立ててみます。
複数のコンテナを立てるので、docker-compose.yml
に書いていきます。
version: '3.8'
services:
gitlab:
image: gitlab/gitlab-ee:latest
container_name: gitlab
restart: always
ports:
- "8080:80"
volumes:
- ./gitlab/config:/etc/gitlab
- ./gitlab/logs:/var/log/gitlab
- ./gitlab/data:/var/opt/gitlab
networks:
- app-network
redmine:
image: redmine:latest
container_name: redmine
restart: always
ports:
- "3000:3000"
volumes:
- ./redmine/files:/usr/src/redmine/files
networks:
- app-network
networks:
app-network:
driver: bridge
起動コマンドはこちら。(-d
はバックグラウンドで実行させるオプションです)
docker-compose up -d
Gitlabは起動に少し時間がかかりますが、無事アクセスできました。
プロキシサーバ(Nginx)を導入する
GitlabもRedmineも80番ポートでアクセスできるように、Nginxに振り分けさせます。
イメージ図↓
そこで、docker-compose.yml
にnginxを追加します。
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- gitlab
- redmine
networks:
- app-network
...
そして、nginx.conf
に振り分け設定を書きます。
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /gitlab/ {
proxy_pass http://gitlab;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /redmine/ {
proxy_pass http://redmine:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
この状態でRedmineにアクセスしてみると、、
スタイル崩れが起きています。
エラーを見てみると、JavaScriptやCSSが見つかっていないというエラーです。
おそらく、Redmineは自分がルート(/
)にデプロイされていると思っているので、ルートを基準としてパスを組み立てます。
例) http://localhost/assets/hoge.js
しかし、URL上では実際に(http://localhost/redmine/assets/hoge.js
)に資材があるため、資材のミスマッチが起きてしまいます。
Gitlabも同様です。
強引にロケーションを書き換えてあげるだけではだめで、Gitlab・Redmine側で「ルートパスが変わったよ」ということが伝わるように設定を書き換えてあげる必要がありそうです。
Gitlabの設定変更
docker exec -it gitlab /bin/bash
でコンテナの中に入り、/etc/gitlab/gitlab.rb
のexternal_url
を以下のように書き換えます。
external_url 'http://localhost/gitlab'
その後、gitlab-ctl reconfigure
で設定を反映させます。
すると、http://localhost/gitlab
でアクセスできるようになりました。
Remineの設定変更
docker-compose.yml
に次の設定を加えます
volumes:
- ./redmine/config.ru:/usr/src/redmine/config.ru
environment:
- RAILS_RELATIVE_URL_ROOT=/redmine
...
config.pu
の中身は以下。
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
map ENV['RAILS_RELATIVE_URL_ROOT'] do
run Rails.application
end
さらに、nginx.conf
のproxy_pass
を以下のようにします。
location /redmine/ {
proxy_pass http://redmine:3000/redmine/;
すると、http://localhost/redmine
でアクセスできるようになりました。
補足
実は、Gitlabはexternal_url
の設定だけではうまくいかず、relative_url_root
の設定もしました。
gitlab_rails['relative_url_root'] = '/gitlab'
しかし、その後relative_url_root
の設定を外した状態でもうまくいってしまい、これが成功要因なのか断定できなかったため本文から外しています。
もしexternal_url
だけでうまくいかなかったらこちらも試してみると良いかもしれません。
最後に
- 私はこの通りにやってもうまくいかず、若干手順異なりますが、参考にさせていただいた記事を貼っておきます。
https://qiita.com/myoshimi/items/c106c31f83df62329019 - 簡単にできるのかとたかをくくっていたのですが、製品の設定を変えないといけないので想定以上に大変でした。。
Discussion