Docker ToolboxでTomcat8 × Java8の環境を構築する

試したいことがありTomcat8 × Java8の環境が必要だったので、Docker Toolboxを使ってDocker上に環境構築した。

# 環境
ホストOS：Windows10
ゲストOS：Debian GNU/Linux 8
Docker Toolbox：1.10.3


# Docker Toolboxのインストール
[Windows10マシンにDocker Toolbox を入れて個人用の開発環境を作る](http://qiita.com/osuo/items/99a2b7413ce75f8217be)を参考にインストール。

以降は全てDocker ToolboxのDocker Quickstart Terminalを利用して実行。

# Docker HubからTomcat8のイメージをダウンロード
以下のコマンドでDocker HubからTomcat8のイメージをダウンロード。

```
docker pull tomcat:8.0
```

docker imagesコマンドでイメージの確認。

```
N@N-PC MINGW64 ~
$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
tomcat                8.0                 1d71cd869100        2 days ago          357.3 MB
```

# コンテナの起動
以下のコマンドでコンテナ起動。

```
docker run -it --rm tomcat:8.0 /bin/bash
```

以下のようにプロンプトが変われば起動成功。

```
N@N-PC MINGW64 ~
$ docker run -it --rm tomcat:8.0 /bin/bash
root@b37f9964f98e:/usr/local/tomcat#
```

ちなみにディストリビューションはDebian系。

```
root@b37f9964f98e:/usr/local/tomcat#  cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
```

# vimのインストール

実はvimが入っていないのでインストールしておく。

```vimが入ってない.
root@b37f9964f98e:/usr/local/tomcat# whereis vim
vim:
```

apt-get update、apt-get install vimでvimをインストール。

```vimのインストール.
root@b37f9964f98e:/usr/local/tomcat# apt-get update
Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]
Get:2 http://security.debian.org jessie/updates/main amd64 Packages [291 kB]
Ign http://httpredir.debian.org jessie InRelease
Get:3 http://httpredir.debian.org jessie-updates InRelease [142 kB]
Get:4 http://httpredir.debian.org jessie Release.gpg [2373 B]
Get:5 http://httpredir.debian.org jessie Release [148 kB]
Get:6 http://httpredir.debian.org jessie-updates/main amd64 Packages [4982 B]
Get:7 http://httpredir.debian.org jessie/main amd64 Packages [9034 kB]
Fetched 9686 kB in 34s (280 kB/s)
Reading package lists... Done
root@b37f9964f98e:/usr/local/tomcat# apt-get install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libgpm2 vim-common vim-runtime
Suggested packages:
  gpm ctags vim-doc vim-scripts
The following NEW packages will be installed:
  libgpm2 vim vim-common vim-runtime
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 6218 kB of archives.
After this operation, 28.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://httpredir.debian.org/debian/ jessie/main libgpm2 amd64 1.20.4-6.1+b2 [34.0 kB]
Get:2 http://httpredir.debian.org/debian/ jessie/main vim-common amd64 2:7.4.488-7 [184 kB]
Get:3 http://httpredir.debian.org/debian/ jessie/main vim-runtime all 2:7.4.488-7 [5047 kB]
Get:4 http://httpredir.debian.org/debian/ jessie/main vim amd64 2:7.4.488-7 [953 kB]
Fetched 6218 kB in 11s (529 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libgpm2:amd64.
(Reading database ... 11828 files and directories currently installed.)
Preparing to unpack .../libgpm2_1.20.4-6.1+b2_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.4-6.1+b2) ...
Selecting previously unselected package vim-common.
Preparing to unpack .../vim-common_2%3a7.4.488-7_amd64.deb ...
Unpacking vim-common (2:7.4.488-7) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../vim-runtime_2%3a7.4.488-7_all.deb ...
Adding 'diversion of /usr/share/vim/vim74/doc/help.txt to /usr/share/vim/vim74/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim74/doc/tags to /usr/share/vim/vim74/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (2:7.4.488-7) ...
Selecting previously unselected package vim.
Preparing to unpack .../vim_2%3a7.4.488-7_amd64.deb ...
Unpacking vim (2:7.4.488-7) ...
Setting up libgpm2:amd64 (1.20.4-6.1+b2) ...
Setting up vim-common (2:7.4.488-7) ...
Setting up vim-runtime (2:7.4.488-7) ...
Processing /usr/share/vim/addons/doc
Setting up vim (2:7.4.488-7) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
Processing triggers for libc-bin (2.19-18+deb8u4) ...
root@b37f9964f98e:/usr/local/tomcat#
```

vimがインストールされたことを確認。

```vimがインストールされたことを確認.
root@b37f9964f98e:/usr/local/tomcat# whereis vim
vim: /usr/bin/vim.basic /usr/bin/vim /etc/vim /usr/share/vim /usr/share/man/man1/vim.1.gz
```

# Java8のインストール
Javaのバージョンが7なので8をインストール。

```javaのバージョンが7.
root@b37f9964f98e:/usr/local/tomcat# java -version
java version "1.7.0_95"
OpenJDK Runtime Environment (IcedTea 2.6.4) (7u95-2.6.4-1~deb8u1)
OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)
```

Java8のインストール手順は以下。

```javal.listファイルを作成.
vim /etc/apt/sources.list.d/java.list
```

```javal.listファイルにパッケージの取得元を記載.
deb http://ppa.launchpad.net/webupd8team/java/ubuntu precise main
deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu precise main
```

```認証キーを追加しリストを更新.
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886
apt-get update
```

```Java8のインストール.
apt-get install oracle-java8-installer
```

```Java8がインストールされたことを確認.
root@b37f9964f98e:/usr/local/tomcat/bin# java -version
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)
root@b37f9964f98e:/usr/local/tomcat/bin#
```

# Tomcatが使うJavaをJava8に変更
Java8をインストールしてもTomcatが使うJavaが7なので、インストールしたJava8に変更する。

```Tomcatが使うJavaが7.
root@b37f9964f98e:/usr/local/tomcat/bin# pwd
/usr/local/tomcat/bin
root@b37f9964f98e:/usr/local/tomcat/bin# startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/lib/jvm/java-7-openjdk-amd64/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
```

変更はJAVA_HOMEをJava8に変えるだけ。

```JAVA_HOMEをJava8に変える.
root@b37f9964f98e:/usr/local/tomcat/bin# JAVA_HOME=/usr/lib/jvm/java-8-oracle
root@b37f9964f98e:/usr/local/tomcat/bin# export JAVA_HOME
```

Tomcatを再起動させてJava8になっていることを確認する。

```
root@b37f9964f98e:/usr/local/tomcat/bin# startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/lib/jvm/java-8-oracle
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
```

これでTomcat8 × Java8の環境が出来上がり。

# tomcat-users.xmlでTomcatユーザを設定
Tomcat Webアプリケーションマネージャでアプリをデプロイするため、Tomcatユーザの設定を行う。
/usr/local/tomcat/conf/tomcat-users.xml を以下のように書き換える。

```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="manager-gui"/>
  <user username="tomcat" password="tomcat" roles="manager-gui"/>

</tomcat-users>
```

# イメージの作成
このままCtrl+dやexitしてコンテナを落とすと、上記の全ての設定がパァーになってしまうので、以下のようにコンテナからデタッチ(コンテナを起動したままコンテナを抜ける)して、現在の設定をイメージとして作成しておく。

```コンテナからでタッチ.
Ctrlを押しながら、p、qと順に押す。
※これでうまく抜けられなければもう1枚Docker Quickstart Terminalを
※立ち上げてそっちで作業する。
```

```イメージを作成するためコンテナIDを確認.
N@N-PC MINGW64 ~
$ docker ps
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                    NAMES
b37f9964f98e        tomcat:8.0            "/bin/bash"         19 hours ago        Up 19 hours         8080/tcp                 naughty_rosalind
```

```イメージを作成.
N@N-PC MINGW64 ~
$ docker commit -m "tomcat8 with java8"  b37f9964f98e tomcat8_with_java8
sha256:50857ea739fb471e1e33e7e8a4805519367e14425e877d3e2c1ed50cb5f54087
```

```イメージが作成されていることを確認.
N@N-PC MINGW64 ~
$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
tomcat8_with_java8    latest              50857ea739fb        16 seconds ago      988.7 MB
tomcat                8.0                 1d71cd869100        3 days ago          357.3 MB
```

# 参考
http://qiita.com/osuo/items/99a2b7413ce75f8217be
http://symfoware.blog68.fc2.com/blog-entry-1716.html