docker imageを作成する手順

### UbuntuにCentos7をpull
$ sudo docker pull centos:7
$ sudo mkdir -p /root/tomcat-container/logs
// 「-p」オプションでポートフォワード設定、「-v」オプションでhostとコンテナのディレクトリを共有
$ sudo docker run -it -d -p 18080:8080 -v /root/tomcat-container/logs:/share/logs –name tomcat centos:7
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
beba506d8248 centos:7 “/bin/bash” 18 minutes ago Up 18 minutes 0.0.0.0:18080->8080/tcp tomcat

### tomcat download
// Tomcat 9.0.31 download https://tomcat.apache.org/download-90.cgi#9.0.31
$ wget http://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.31/bin/apache-tomcat-9.0.31.tar.gz
// docker cp :
$ sudo docker cp apache-tomcat-9.0.31.tar.gz tomcat:/opt/

### コンテナ側での作業
// container login
$ sudo docker exec -it tomcat bash
// java install
$ yum install -y java
$ cd /opt/
$ tar zxf apache-tomcat-9.0.31.tar.gz
$ ls
$ cd apache-tomcat-9.0.31
$ ls
$ ./bin/startup.sh

### ログの出力先変更
$ sed -i -e “s/\${catalina.base}\/logs/\/share\/logs/g” ./conf/logging.properties
$ ./bin/shutdown.sh
$ ./bin/startup.sh
$ ls -la /share/logs/
// ubuntu側にログが共有されている
$ sudo ls -la /root/tomcat-container/logs/
total 20
drwxr-xr-x 2 root root 4096 Mar 16 13:29 .
drwxr-xr-x 3 root root 4096 Mar 16 11:55 ..
-rw-r—– 1 root root 5890 Mar 16 13:29 catalina.2020-03-16.log
-rw-r—– 1 root root 0 Mar 16 13:29 host-manager.2020-03-16.log
-rw-r—– 1 root root 408 Mar 16 13:29 localhost.2020-03-16.log
-rw-r—– 1 root root 0 Mar 16 13:29 manager.2020-03-16.log
$ sudo docker stop tomcat
$ sudo docker ps -a
$ sudo ls -la /root/tomcat-container/logs/

### Docker image作成
// docker commit
$ sudo docker commit tomcat tomcat-image
$ sudo docker images

### 作成したDocker imageからコンテナ起動(上記の複製)
$ sudo mkdir -p /root/tomcat-container/logs2
$ sudo docker run -it -d -p 18082:8080 -v /root/tomcat-container/logs2:/share/logs –name tomcat2 tomcat-image
$ sudo docker ps -a
$ sudo docker exec -it tomcat2 bash
$ ls /opt
$ cd /opt/apache-tomcat-9.0.31
$ ./bin/startup.sh
http://192.168.33.10:18082/
$ exit
$ sudo docker stop tomcat2

繰り返しやることで、少しずつ慣れてきます。