Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その20 )( Docker で Redis の環境を構築する3(Redis を 5.0.1 → 5.0.2 にバージョンアップする+.env の環境変数を使用するよう変更する))
概要
記事一覧はこちらです。
Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その19 )( Docker で Redis の環境を構築する2(Redis Cluster 構成)) の続きです。
- 今回の手順で確認できるのは以下の内容です。
- https://hub.docker.com/_/redis/ を見たら 5.0.2 の Docker Image が出ていました。Docker Compose で Redis 環境を構築する時の Docker Image を redis:5.0.1 → redis:5.0.2 へ変更します。
- redis のバージョン番号をいろいろなファイルに直接書いていますが、.env ファイルに環境変数を定義して、それを使用する方法に変更します。
参照したサイト・書籍
.env files support
https://plugins.jetbrains.com/plugin/9525--env-files-support https://github.com/adelf/idea-php-dotenv-plugin/EnvFile
https://plugins.jetbrains.com/plugin/7861-envfile https://github.com/Ashald/EnvFileDockerfile reference - Understand how ARG and FROM interact
https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interactdocker-compose.ymlでcommandの引数が長いのでエスケープして改行したい
https://qiita.com/minamijoyo/items/fbd4ec127f29283fb458
目次
- .env 用の IntelliJ IDEA Plugin である .env files support をインストールする
- .env に環境変数を追加する
- docker/redis/Dockerfile を環境変数を使用するよう変更する
- docker-compose.yml を環境変数を使用するよう変更する
- 動作確認
手順
.env 用の IntelliJ IDEA Plugin である .env files support をインストールする
.env ファイルを開くと Plugins supporting *.env files found.
というメッセージが表示されました。「Install plugins」リンクをクリックします。
「Choose Plugins to Install or Enable」ダイアログが表示されて Plugin が2つ表示されました。JetBrains Plugins Repository で見るとどちらも★5つですが、.env files support のダウンロード数が多かったので .env files support をインストールしてみます。.env files support だけチェックした状態にして「OK」ボタンをクリックします。
Plugin をインストールした後、IntelliJ IDEA を再起動します。
.env ファイルを見ると今度は色が付くようになりました。一旦このまま使用してみます。
.env に環境変数を追加する
.env に環境変数を追加します。
HOST_IP_ADDRESS=172.23.136.33 REDIS_VERSION=5.0.2 REDIS_CLUSTER_1_PORT=6379 REDIS_CLUSTER_2_PORT=6380 REDIS_CLUSTER_3_PORT=6381 REDIS_CLUSTER_4_PORT=6382 REDIS_CLUSTER_5_PORT=6383 REDIS_CLUSTER_6_PORT=6384
- 以下の記述を追加します。redis のバージョンとホスト上のポート番号をここで指定します(Cluster Bus ポート番号は +10000 したものを使用します)。
REDIS_VERSION=5.0.2
REDIS_CLUSTER_1_PORT=6379
REDIS_CLUSTER_2_PORT=6380
REDIS_CLUSTER_3_PORT=6381
REDIS_CLUSTER_4_PORT=6382
REDIS_CLUSTER_5_PORT=6383
REDIS_CLUSTER_6_PORT=6384
docker/redis/Dockerfile を環境変数を使用するよう変更する
docker/redis/Dockerfile の以下の点を変更します。
ARG REDIS_VERSION FROM redis:${REDIS_VERSION} RUN apt-get update -qq && apt-get install -y expect ADD redis.conf /etc/redis/
ARG REDIS_VERSION
を追加します。FROM redis:5.0.1
→FROM redis:${REDIS_VERSION}
に変更します。
このファイルでの ${REDIS_VERSION}
は .env ファイルに定義されている環境変数ではなく、docker-compose.yml ファイルから build 時に引数で渡された値になります。docker-compose.yml の方で build時に渡す引数に .env ファイルに定義した環境変数を渡すよう記述します。
docker-compose.yml を環境変数を使用するよう変更する
docker-compose.yml の以下の点を変更します。
# 起動したコンテナに /bin/sh でアクセスする場合には以下のコマンドを実行する # docker exec -it redis /bin/sh # # 起動したコンテナの redis に redis-cli でアクセスするには以下のコマンドを実行する # docker exec -it redis redis-cli # ############################################################################# # 単体 Redis サーバ # redis: # image: redis:${REDIS_VERSION} # container_name: redis # ports: # - "6379:6379" # ############################################################################# # Redis Cluster redis-cluster-1: build: context: ./docker/redis args: - REDIS_VERSION=${REDIS_VERSION} image: redis:${REDIS_VERSION}-custom container_name: redis-cluster-${REDIS_CLUSTER_1_PORT} ports: - "${REDIS_CLUSTER_1_PORT}:6379" - "1${REDIS_CLUSTER_1_PORT}:16379" command: - /bin/sh - -c - | redis-server /etc/redis/redis.conf \ --cluster-announce-ip ${HOST_IP_ADDRESS} \ --cluster-announce-port ${REDIS_CLUSTER_1_PORT} \ --cluster-announce-bus-port 1${REDIS_CLUSTER_1_PORT} redis-cluster-2: image: redis:${REDIS_VERSION}-custom container_name: redis-cluster-${REDIS_CLUSTER_2_PORT} ports: - "${REDIS_CLUSTER_2_PORT}:6379" - "1${REDIS_CLUSTER_2_PORT}:16379" command: - /bin/sh - -c - | redis-server /etc/redis/redis.conf \ --cluster-announce-ip ${HOST_IP_ADDRESS} \ --cluster-announce-port ${REDIS_CLUSTER_2_PORT} \ --cluster-announce-bus-port 1${REDIS_CLUSTER_2_PORT} depends_on: - redis-cluster-1 redis-cluster-3: image: redis:${REDIS_VERSION}-custom container_name: redis-cluster-${REDIS_CLUSTER_3_PORT} ports: - "${REDIS_CLUSTER_3_PORT}:6379" - "1${REDIS_CLUSTER_3_PORT}:16379" command: - /bin/sh - -c - | redis-server /etc/redis/redis.conf \ --cluster-announce-ip ${HOST_IP_ADDRESS} \ --cluster-announce-port ${REDIS_CLUSTER_3_PORT} \ --cluster-announce-bus-port 1${REDIS_CLUSTER_3_PORT} depends_on: - redis-cluster-1 redis-cluster-4: image: redis:${REDIS_VERSION}-custom container_name: redis-cluster-${REDIS_CLUSTER_4_PORT} ports: - "${REDIS_CLUSTER_4_PORT}:6379" - "1${REDIS_CLUSTER_4_PORT}:16379" command: - /bin/sh - -c - | redis-server /etc/redis/redis.conf \ --cluster-announce-ip ${HOST_IP_ADDRESS} \ --cluster-announce-port ${REDIS_CLUSTER_4_PORT} \ --cluster-announce-bus-port 1${REDIS_CLUSTER_4_PORT} depends_on: - redis-cluster-1 redis-cluster-5: image: redis:${REDIS_VERSION}-custom container_name: redis-cluster-${REDIS_CLUSTER_5_PORT} ports: - "${REDIS_CLUSTER_5_PORT}:6379" - "1${REDIS_CLUSTER_5_PORT}:16379" command: - /bin/sh - -c - | redis-server /etc/redis/redis.conf \ --cluster-announce-ip ${HOST_IP_ADDRESS} \ --cluster-announce-port ${REDIS_CLUSTER_5_PORT} \ --cluster-announce-bus-port 1${REDIS_CLUSTER_5_PORT} depends_on: - redis-cluster-1 redis-cluster-6: image: redis:${REDIS_VERSION}-custom container_name: redis-cluster-${REDIS_CLUSTER_6_PORT} ports: - "${REDIS_CLUSTER_6_PORT}:6379" - "1${REDIS_CLUSTER_6_PORT}:16379" command: - /bin/sh - -c - | redis-server /etc/redis/redis.conf \ --cluster-announce-ip ${HOST_IP_ADDRESS} \ --cluster-announce-port ${REDIS_CLUSTER_6_PORT} \ --cluster-announce-bus-port 1${REDIS_CLUSTER_6_PORT} depends_on: - redis-cluster-1 redis-cluster-make: image: redis:${REDIS_VERSION}-custom container_name: redis-cluster-make command: - /bin/sh - -c - | expect -c " spawn redis-cli --cluster create \ ${HOST_IP_ADDRESS}:${REDIS_CLUSTER_1_PORT} \ ${HOST_IP_ADDRESS}:${REDIS_CLUSTER_2_PORT} \ ${HOST_IP_ADDRESS}:${REDIS_CLUSTER_3_PORT} \ ${HOST_IP_ADDRESS}:${REDIS_CLUSTER_4_PORT} \ ${HOST_IP_ADDRESS}:${REDIS_CLUSTER_5_PORT} \ ${HOST_IP_ADDRESS}:${REDIS_CLUSTER_6_PORT} \ --cluster-replicas 1 expect \"Can I set the above configuration? (type 'yes' to accept): \" send \"yes\n\" expect eof " depends_on: - redis-cluster-1 - redis-cluster-2 - redis-cluster-3 - redis-cluster-4 - redis-cluster-5 - redis-cluster-6 redis_exporter: image: oliver006/redis_exporter:latest container_name: redis_exporter ports: - "9121:9121" environment: - REDIS_ADDR=redis://${HOST_IP_ADDRESS}:${REDIS_CLUSTER_1_PORT},redis://${HOST_IP_ADDRESS}:${REDIS_CLUSTER_2_PORT},redis://${HOST_IP_ADDRESS}:${REDIS_CLUSTER_3_PORT},redis://${HOST_IP_ADDRESS}:${REDIS_CLUSTER_4_PORT},redis://${HOST_IP_ADDRESS}:${REDIS_CLUSTER_5_PORT},redis://${HOST_IP_ADDRESS}:${REDIS_CLUSTER_6_PORT}
- サービス名を redis-cluster-6379 ~ redis-cluster-6384 → redis-cluster-1 ~ redis-cluster-6 に変更します。サービス名に環境変数は使用できないので、1~6 の連番にしました。
- redis-cluster-1 の build の記述を
build: ./docker/redis
から build の下に context, args を記述する方式に変更します。args にREDIS_VERSION=${REDIS_VERSION}
を記述することで build 時に Dockerfile に .env ファイルに記述した REDIS_VERSION の値が渡されます。 - image の記述を
redis:5.0.1-custom
→redis:${REDIS_VERSION}-custom
に変更します。 - ポート番号が記述してある箇所を
${REDIS_CLUSTER_1_PORT}
~${REDIS_CLUSTER_6_PORT}
に変更します。Cluster Bus ポート番号の記述は16379
→1${REDIS_CLUSTER_1_PORT}
のように変更します。
動作確認
docker-compose up -d
コマンドで起動します。5.0.2: Pulling from library/redis
と出力されており 5.0.2 の Docker Image がダウンロードされています。
(.....途中は長いので省略.....)
redis-cluster-make コンテナのログを見ると、Redis Cluster が正常に生成されているようです。
redis-cluster-6379 コンテナで redis-cli を起動し cluster nodes
コマンドを実行すると、master, slave の情報が表示されます。
Tomcat を起動して http://localhost:8080/ にアクセスしてログイン画面を表示後、ログインすることもできました。
履歴
2018/12/01
初版発行。