CData Software Blog

クラウド連携のCData Software の技術ブログです。

バインドマウントでDocker コンテナ(Tomcat)から APIServer を利用する方法

f:id:sennanvolar44:20210918002047p:plain こんにちは、テクニカルサポートの宮本です!

今回は Docker コンテナ(Tomcat) 上の APIServer を、Docker ホスト側のディレクトリで利用するバインドマウントの方法をご紹介します。

バインドマウントとは

ホストマシン上のファイルやディレクトリをコンテナで利用できるように、コンテナを起動する際にホスト側の対象となるディレクトリとコンテナ上のディレクトリの紐づけを行うことを指します。

ホスト側に APIServer をインストールすることで、コンテナの入れ替えが発生しても設定情報を永続的に利用することができます。
また、新たにコネクタを追加したいときには、対象のコネクタを所定のホスト側のディレクトリに配置するだけで追加することが可能となります。
f:id:sennanvolar44:20210921130819p:plain

手順

APIServer ダウンロード

まずは APIServer Java 版をダウンロードしてください。(30日間の無償トライアルが可能です)
www.cdata.com f:id:sennanvolar44:20210916175207p:plain

Docker ホスト側に APIServer を展開する

ダウンロードしたファイルを、Docker ホストとなるインスタンスに格納し、中にある APIServer のwarファイルを任意のディレクトリで展開します。
今回は「/opt/apiserver」配下に展開しました。

[root@apiserver]$ pwd
/opt/apiserver
[root@apiserver]$ ll
total 118364
-rw-r--r-- 1 root root 121202370 Aug 25 21:23 apiserver.war
[root@apiserver]$ jar xvf apiserver.war
~
[root@apiserver]$ ll
total 118408
drwxr-xr-x  2 root root       325 Aug 25 17:23 admin
-rw-r--r--  1 root root       290 Apr  5 13:36 admin.rsc
drwxr-xr-x  2 root root         6 Apr  5 13:36 api
-rw-r--r--  1 root root       288 Apr  5 13:36 api.rsc
-rw-r--r--  1 root root 121202370 Aug 25 21:23 apiserver.war
drwxr-xr-x  3 root root        41 Aug 25 17:23 app_data
-rw-r--r--  1 root root       320 Apr  5 13:36 error.jsp
-rw-r--r--  1 root root     17542 Apr  5 13:29 favicon.ico
drwxr-xr-x  3 root root        19 Aug 25 17:23 help
drwxr-xr-x  2 root root         6 Aug 25 17:19 includes
-rw-r--r--  1 root root        43 Apr  5 13:36 index.jsp
-rw-r--r--  1 root root       468 Apr  5 13:36 logout.jsp
drwxr-xr-x  2 root root        25 Aug 25 17:23 META-INF
drwxr-xr-x  2 root root         6 Aug 25 17:19 pub
-rw-r--r--  1 root root       554 Apr  5 13:36 query.rsc
drwxr-xr-x  2 root root         6 Aug 25 17:19 src
drwxr-xr-x 13 root root       193 Aug 25 17:23 ui
drwxr-xr-x  3 root root        32 Aug 25 17:23 WEB-INF
[root@apiserver]$

展開が完了しましたら、次はDockerfile 関連の準備になります。

Dockerfile の準備

Dockerfile の中身は、イメージの取得とtomcat-users.xml の配置を行うようにしています。ちなみに tomcat-users.xml では APIServer へのログインユーザーの情報を設定しています。

[root@ip-172-31-35-49 apiserver]# cat Dockerfile
FROM tomcat:9.0.0

ADD ./tomcat-users.xml /usr/local/tomcat/conf
[root@ip-172-31-35-49 apiserver]#

tomcat-users.xml の中身
userタグ(最後の部分)にroles="cdata_admin"のユーザを追加しています。

[root@apiserver]$ cat tomcat-users.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
<user name="admin" password="admin" roles="cdata_admin,rssbus_admin,admin-gui,manager-gui,
manager-status,manager-script,manager-jmx" />
</tomcat-users>

バインドマウントでコンテナ起動

では Dockerfile イメージを取得します。

[root@apiserver]$ ll
total 245824
drwxr-xr-x 2 root root       133 Sep 16 14:14 BWCG-V
-rw-r--r-- 1 root root 251699605 Aug 26 06:33 BWCG-V.zip
-rw-r--r-- 1 root root        65 Sep 16 14:52 Dockerfile
-rw-r--r-- 1 root root      8749 Sep 16 14:48 server.xml
-rw-r--r-- 1 root root      2301 Aug 19 14:01 tomcat-users.xml
[root@apiserver]$
[root@apiserver]$
[root@apiserver]$ docker build --no-cache -t tomcat:apiserver .
Sending build context to Docker daemon  634.3MB
~
Status: Downloaded newer image for tomcat:9.0.0
 ---> 273c6a7e33d5
Step 2/2 : ADD ./tomcat-users.xml /usr/local/tomcat/conf
 ---> b59790b9b686
Successfully built b59790b9b686
Successfully tagged tomcat:apiserver
[root@apiserver]$

次に、ユーザーディレクトリの配下に~/cdata/apiserver というディレクトリを作成しておきます。このディレクトリ配下には APIServer 上の設定情報が保持されるようになります。
今回は root ユーザーで実行しているので、/root/cdata/apiserverディレクトリを作成しました。

それでは Docker コンテナを起動していきます。
先ほど APIServer を展開したパスと上記設定情報保持用のパスをコンテナでマウントするよう -v で指定します。

[root@apiserver]$ docker run --rm -it --name apiserver -v /opt/apiserver/:/usr/local/tomcat/webapps/apiserver/ -v /root/cdata/apiserver/:/root/cdata/apiserver/ -p 8080:8080 tomcat:apiserver
~
16-Sep-2021 15:09:58.208 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
16-Sep-2021 15:09:58.219 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
16-Sep-2021 15:09:58.222 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 15615 ms

これでエラーが出力されてなければ起動完了です。
以下のようなURLで接続を試みてください。
http://XXXXXXXXX:8080/apiserver

うまくいけばログイン画面が表示され、tomcat-users.xml で指定したユーザー/パスワードでログインすれば、APIServer のトップ画面が表示されます。
f:id:sennanvolar44:20210917234654p:plain

コンテナを入れ替え後の確認

まずは現 APIServer でいくつか設定をしていきましょう。
ABOUT タブにいってトライアルのアクティベートを行ってください。その後、SETTINGS → Connections で接続設定を行います。今回はMySQL に接続してみています。
f:id:sennanvolar44:20210917235400p:plain

あとは適当にResources で利用するリソースなどを作成しておきます。
f:id:sennanvolar44:20210917235625p:plain

この状態でコンテナを入れ替えてみます。APIServer のアプリケーション本体と設定内容は Docker ホスト側に格納されているので、コンテナの入れ替えが発生しても影響を受けないはずです。

では起動中の Docker を停止させます。なお、Docker 起動時に --rm と指定していたので、停止したタイミングでコンテナが削除され、コンテナリストを表示させても何も残らないようにしています。

[root@ip-172-31-35-49 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@ip-172-31-35-49 ~]#

それでは Docker イメージの生成から行っていきます。

[root@ip-172-31-35-49 apiserver]# docker build --no-cache -t tomcat:apiserver .
Sending build context to Docker daemon  755.5MB
Step 1/2 : FROM tomcat:9.0.0
 ---> 273c6a7e33d5
Step 2/2 : ADD ./tomcat-users.xml /usr/local/tomcat/conf
 ---> 68e095a8d671
Successfully built 68e095a8d671
Successfully tagged tomcat:apiserver
[root@ip-172-31-35-49 apiserver]#

生成したイメージをもとに再度コンテナを起動してみます。

[root@ip-172-31-35-49 apiserver]# docker run --rm -it --name apiserver -v /opt/apiserver/:/usr/local/tomcat/webapps/apiserver/ -v /root/cdata/apiserver/:/root/cdata/apiserver/ -p 8080:8080 tomcat:apiserver

f:id:sennanvolar44:20210918000440p:plain

先ほど設定したリソースが表示されているのが確認できました!

おわりに

いかがでしたでしょうか。Docker 上で APIServer をご利用される際は、Docker ホスト側に設定情報を保持する方がデータが永続的に保持されますのでおすすめです。 もし設定情報が外部のDBで保持したいというようであれば、こちらの記事をご参照ください。