Db2 が Dockerでpullできるようになってる

OSSとかはたくさんあるけど、Db2も出たみたいです。
Macでも動くみたいだけどLinuxでとりあえずやってみました。ログを全部コピペで書いたので記事としてはめっちゃ長くなってますが、**所要時間としては10分くらいでdb2構築できちゃいます。打つコマンドも5個くらいです。**

https://store.docker.com/images/db2-developer-c-edition/plans/124ad32d-aca4-4731-b282-87b94e876e42?tab=instructions

##Dockerログイン

```console
# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xxxx
Password:
Login Succeeded

```

##コンテナをダウンロード

WEBページの右上のコマンドコピペのところに書いてあるpullコマンドを使うと、うまくいきます。

```console

#合ってるコマンド
# docker pull store/ibmcorp/db2_developer_c:11.1.2.2
11.1.2.2: Pulling from store/ibmcorp/db2_developer_c
18b8eb7e7f01: Pull complete
bd79b882979f: Pull complete
7062daba2413: Pull complete
5309e003a4bb: Pull complete
a4a9ce1100fe: Pull complete
48b47247495c: Pull complete
Digest: sha256:f9a4ccfc37ecd845443750ef54583f2968a3aed8de8780db04882b0ffc522d99
Status: Downloaded newer image for store/ibmcorp/db2_developer_c:11.1.2.2
```

## 設定ファイルの作成
とりあえずカレントディレクトリに設定ファイルを作成。viでつくりました。あとノリでOracle compatibilityとかをtrueにしてみました

```console
sh-4.2# cat db2.env
# set License to accept to use the Db2 software contained in this image
 LICENSE=accept
 # specify the Db2 Instance name
 DB2INSTANCE=db2inst1
 # specify the Db2 Instance password
 DB2INST1_PASSWORD=password
 # create an initial database with the name provided or leave empty
 DBNAME=qiitadb
 # set to true to enable BLU on instance
 BLU=false
 # set to true to enable Oracle Compatibility on instance
 ENABLE_ORACLE_COMPATIBILITY=true
 # set to yes if we have an existing instance and running a new container with an higher Db2 level. Will be deprecated on next release
 UPDATEAVAIL=NO
 # set to true to create sample database
 TO_CREATE_SAMPLEDB=false
 # set to true to create DSM repository database
 REPODB=true
```

## では、run!

ガイドにあるようなコマンドで実行

```console:webページに出てるコマンド
 #docker run -h db2server_<your_container_name> \
            --name db2server --restart=always \  
            --detach \  
            --privileged=true \
            -p 50000 \
            --env-file .env_list \ 
            -v <db storage dir>:/database \
            <image name>
```

ちなみに実際に実行したのはこちら

```console:実際に打ったコマンド
#docker run -h db2server_qiitaTry --name db2server --restart=always --detach --privileged=true -p 50000 --env-file ./db2.env -v /data:/database 83e8b59d8007
```

## docker logs -f で途中経過を観察する

**めちゃめちゃ早い**...たぶん5分もかからずに全部終わりました。

```console
# docker logs -f db2server
(*) Previous setup has not been detected. Creating the users...
(*) Creating users ...
(*) Previous setup has not been detected. Creating the instance...
(*) Creating instance ...
DB2 installation is being initialized.

 Total number of tasks to be performed: 4
Total estimated time for all tasks to be performed: 309 second(s)

Task #1 start
Description: Setting default global profile registry variables
Estimated time 1 second(s)
Task #1 end

Task #2 start
Description: Initializing instance list
Estimated time 5 second(s)
Task #2 end

Task #3 start
Description: Configuring DB2 instances
Estimated time 300 second(s)
Task #3 end

Task #4 start
Description: Updating global profile registry
Estimated time 3 second(s)
Task #4 end

The execution completed successfully.

For more information see the DB2 installation log at "/tmp/db2icrt.log.55".
DBI1446I  The db2icrt command is running.


DBI1070I  Program db2icrt completed successfully.


(*) Applying Db2 license ...

LIC1402I  License added successfully.


LIC1426I  This product is now licensed for use as outlined in your License Agreement.  USE OF THE PRODUCT CONSTITUTES ACCEPTANCE OF THE TERMS OF THE IBM LICENSE AGREEMENT, LOCATED IN THE FOLLOWING DIRECTORY: "/opt/ibm/db2/V11.1/license/en_US.iso88591"
(*) Saving the checksum of the current nodelock file ...
(*) Fixing db2nodes file configuration ...
(*) Updating DBM CFG parameters ...
DB20000I  The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
DB20000I  The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
DB20000I  The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
(*) Enabling Oracle compatibility
(*) Enabling Oracle Compatibility on instance ...

DB2 State : Operable
DB2 has not been started
Starting DB2...

10/20/2017 14:29:49     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
(*) User chose to create a SAMPLE database

  Creating database "SAMPLE"...
  Connecting to database "SAMPLE"...
  Creating tables and data in schema "DB2INST1"...
  Creating tables with XML columns and XML data in schema "DB2INST1"...

  'db2sampl' processing complete.

10/20/2017 14:30:58     0   0   SQL1026N  The database manager is already active.
SQL1026N  The database manager is already active.
### Enabling LOGARCHMETH1

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.2.2
 SQL authorization ID   = DB2INST1
 Local database alias   = SAMPLE

DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification
were not changed dynamically. For these configuration parameters, the database
must be shutdown and reactivated before the configuration parameter changes
become effective.
### Restarting DB2
10/20/2017 14:31:01     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
10/20/2017 14:31:03     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
### Making backup directory and performing backup

Backup successful. The timestamp for this backup image is : 20171020143106

(*) Applying autoconfiguration for instance ...

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.2.2
 SQL authorization ID   = DB2INST1
 Local database alias   = SAMPLE

DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification
were not changed dynamically. For these configuration parameters, the database
must be shutdown and reactivated before the configuration parameter changes
become effective.
SQL5146W  "MAXLOCKS" must be set to "AUTOMATIC" when "LOCKLIST" is
"AUTOMATIC".  "MAXLOCKS" has been set to "AUTOMATIC".
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification
were not changed dynamically. For these configuration parameters, the database
must be shutdown and reactivated before the configuration parameter changes
become effective.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification
were not changed dynamically. For these configuration parameters, the database
must be shutdown and reactivated before the configuration parameter changes
become effective.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification
were not changed dynamically. For these configuration parameters, the database
must be shutdown and reactivated before the configuration parameter changes
become effective.
DB20000I  The SQL command completed successfully.
10/20/2017 14:31:17     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
10/20/2017 14:31:19     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
(*) Enabling text search for sample

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.2.2
 SQL authorization ID   = DB2INST1
 Local database alias   = SAMPLE

DB20000I  The SQL command completed successfully.
DB20000I  The SQL command completed successfully.
CIE00001 Operation completed successfully.
CIE00001 Operation completed successfully.
(*) Creating repository database for DSM ...
DB20000I  The CREATE DATABASE command completed successfully.
(*) User chose to create qiitadb database
(*) Creating database qiitadb ...
DB20000I  The CREATE DATABASE command completed successfully.
DB20000I  The ACTIVATE DATABASE command completed successfully.
10/20/2017 14:33:54     0   0   SQL1026N  The database manager is already active.
SQL1026N  The database manager is already active.
### Enabling LOGARCHMETH1

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.2.2
 SQL authorization ID   = DB2INST1
 Local database alias   = QIITADB

DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification
were not changed dynamically. For these configuration parameters, the database
must be shutdown and reactivated before the configuration parameter changes
become effective.
### Restarting DB2
10/20/2017 14:34:00     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
10/20/2017 14:34:02     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
### Making backup directory and performing backup

Backup successful. The timestamp for this backup image is : 20171020143405

(*) Applying autoconfiguration for instance ...

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.2.2
 SQL authorization ID   = DB2INST1
 Local database alias   = QIITADB

DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification
were not changed dynamically. For these configuration parameters, the database
must be shutdown and reactivated before the configuration parameter changes
become effective.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification
were not changed dynamically. For these configuration parameters, the database
must be shutdown and reactivated before the configuration parameter changes
become effective.
DB20000I  The SQL command completed successfully.
10/20/2017 14:34:17     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
10/20/2017 14:34:18     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
(*) Enabling text search for qiitadb

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.2.2
 SQL authorization ID   = DB2INST1
 Local database alias   = QIITADB

DB20000I  The SQL command completed successfully.
DB20000I  The SQL command completed successfully.
SQL20427N  An error occurred during a text search administration procedure or command. The error message is "CIE00204 Instance services already started. ". SQLSTATE=38H14
CIE00001 Operation completed successfully.
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA ED25519
(*) All databases are now active.
(*) Setup has completed.
          from "/database/data/db2inst1/NODE0000/SQL00003/LOGSTREAM0000/".

2017-10-20-14.34.44.970350+000 E428668E532           LEVEL: Event
PID     : 16803                TID : 139945645696768 PROC : db2sysc 0
INSTANCE: db2inst1             NODE : 000            DB   : QIITADB
APPHDL  : 0-21                 APPID: *LOCAL.db2inst1.171020143437
AUTHID  : DB2INST1             HOSTNAME: db2server_qiitaTry
EDUID   : 22                   EDUNAME: db2agent (idle) 0
FUNCTION: DB2 UDB, base sys utilities, sqeLocalDatabase::FreeResourcesOnDBShutdown, probe:16244
STOP    : DATABASE: QIITADB  : DEACTIVATED: NO
```

## では、ログインして使ってみよう

まずはコンテナ内に入ってみる

```console
# docker exec -ti db2server bash -c "su - db2inst1"

Last login: Fri Oct 20 14:34:37 UTC 2017
[db2inst1@db2server_qiitaTry ~]$
```

DBに接続してテーブル一覧とか出してみる

```console
[db2inst1@db2server_qiitaTry ~]$ db2 connect to sample

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.2.2
 SQL authorization ID   = DB2INST1
 Local database alias   = SAMPLE

[db2inst1@db2server_qiitaTry ~]$ db2 list tables

Table/View                      Schema          Type  Creation time
------------------------------- --------------- ----- --------------------------
ACT                             DB2INST1        T     2017-10-20-14.30.49.511231
ADEFUSR                         DB2INST1        S     2017-10-20-14.30.52.539125
CATALOG                         DB2INST1        T     2017-10-20-14.30.56.467247
CL_SCHED                        DB2INST1        T     2017-10-20-14.30.48.031812
CUSTOMER                        DB2INST1        T     2017-10-20-14.30.56.042770
DEPARTMENT                      DB2INST1        T     2017-10-20-14.30.48.365904
DEPT                            DB2INST1        A     2017-10-20-14.30.48.625406
EMP                             DB2INST1        A     2017-10-20-14.30.48.794913
EMPACT                          DB2INST1        A     2017-10-20-14.30.49.509690
EMPLOYEE                        DB2INST1        T     2017-10-20-14.30.48.626851
EMPMDC                          DB2INST1        T     2017-10-20-14.30.53.225084
EMPPROJACT                      DB2INST1        T     2017-10-20-14.30.49.454499
EMP_ACT                         DB2INST1        A     2017-10-20-14.30.49.510532
EMP_PHOTO                       DB2INST1        T     2017-10-20-14.30.48.795679
EMP_RESUME                      DB2INST1        T     2017-10-20-14.30.49.015854
INVENTORY                       DB2INST1        T     2017-10-20-14.30.55.765216
IN_TRAY                         DB2INST1        T     2017-10-20-14.30.49.677550
ORG                             DB2INST1        T     2017-10-20-14.30.49.714063
PRODUCT                         DB2INST1        T     2017-10-20-14.30.55.257809
PRODUCTSUPPLIER                 DB2INST1        T     2017-10-20-14.30.56.809284
PROJ                            DB2INST1        A     2017-10-20-14.30.49.330554
PROJACT                         DB2INST1        T     2017-10-20-14.30.49.331267
PROJECT                         DB2INST1        T     2017-10-20-14.30.49.179440
（以下略）
```

んー。。簡単だ。。。