1. 程式人生 > >在CentOS7下從0開始搭建docker併發布tomcat專案

在CentOS7下從0開始搭建docker併發布tomcat專案

一切從0開始,我也是個小白:

1.檢查你的系統是不是高於3.8的核心,如果沒有請升級CentOS7或者Ubuntu 14

#uname -a

2.CentOS7下安裝docker

#yum -y install docker

3.檢視docker版本

#docker version

4.啟動docker服務

#docker service start

5.檢視本地映象

#docker images

6.如果沒有本地映象,可以下載一個centos7的映象來作為基礎執行環境

#docker pull centos:7.2.1511

7.再次檢視本地映象

#docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    7.2.1511            feac5e0dfdb2        8 months ago        194.6 MB

8.在本地先備好jdk和tomcat,此處採用jdk1.7和tomcat7

#cd /root/software/

#wget http://zhibo100.oss-cn-hangzhou.aliyuncs.com/software/jdk-7u79-linux-x64.tar.gz

#wget http://zhibo100.oss-cn-hangzhou.aliyuncs.com/software/apache-tomcat-7.0.59.tar.gz

需要其他版本的jdk和tomcat操作方法類似。

9.掛載宿主主機到映象容器的/mnt/software/下:

#docker run -i -t -v /root/software/:/mnt/software/  --privileged=true  feac5e /bin/bash

10.進入到/mnt/software/下解壓jdk和tomcat並將其移動到/opt/目錄下

#cd /mnt/software/

#tar -zxvf jdk-7u79-linux-x64.tar.gz

#tar -zxvf apache-tomcat-7.0.59.tar.gz

#mv jdk1.7.0_79/ /opt/jdk

#mv apache-tomcat-7.0.59 /opt/tomcat

11.配置JAVA環境變數並檢測

#vi ~/.bashrc

在檔案末尾加上

export JAVA_HOME=/opt/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

儲存退出

#source ~/.bashrc

#java -version

java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

12.編寫系統啟動指令碼並賦予許可權

#vi /root/run.sh

加入以下程式碼

#!/bin/bash
source ~/.bashrc
sh /opt/tomcat/bin/catalina.sh run

#chmod u+x /root/run.sh

13.退出容器,並檢視確認沒有正在執行的容器

#exit

#docker ps

14.查詢所有容器

#docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
ee0ee8ac3a93        feac                "/bin/bash"         13 minutes ago      Exited (0) About a minute ago                       sad_noyce
15.根據當前的容器ID建立新的映象

#docker commit ee0ee sdzn.com/tomcat:0.1

16.檢視當前的映象

#docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sdzn.com/tomcat     0.1                 af0647ada992        56 seconds ago      514.6 MB
docker.io/centos    7.2.1511            feac5e0dfdb2        8 months ago        194.6 MB
17.以守護程序方式啟動新的映象並傳入啟動引數

#docker run -d -p 58080:8080 --name tomcat1 sdzn.com/tomcat:0.1 /root/run.sh

18.檢視正在執行的映象

#docker ps

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                     NAMES
10fe153d9d6d        sdzn.com/tomcat:0.1   "/root/run.sh"      14 seconds ago      Up 6 seconds        0.0.0.0:58080->8080/tcp   tomcat1

19.訪問映象:

http://127.0.0.1:58080



20.停止容器並移除

#docker ps -a

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                      PORTS                     NAMES
148df39831f0        af                    "/bin/bash"         56 seconds ago      Exited (2) 14 seconds ago                             admiring_kalam
10fe153d9d6d        sdzn.com/tomcat:0.1   "/root/run.sh"      6 minutes ago       Up 6 minutes                0.0.0.0:58080->8080/tcp   tomcat1
ee0ee8ac3a93        feac                  "/bin/bash"         25 minutes ago      Exited (0) 13 minutes ago                             sad_noyce
#docker stop 10f

#docker ps -a

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                            PORTS               NAMES
148df39831f0        af                    "/bin/bash"         2 minutes ago       Exited (2) 2 minutes ago                              admiring_kalam
10fe153d9d6d        sdzn.com/tomcat:0.1   "/root/run.sh"      8 minutes ago       Exited (137) About a minute ago                       tomcat1
ee0ee8ac3a93        feac                  "/bin/bash"         27 minutes ago      Exited (0) 15 minutes ago                             sad_noyce
#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sdzn.com/tomcat     0.1                 af0647ada992        12 minutes ago      514.6 MB
docker.io/centos    7.2.1511            feac5e0dfdb2        8 months ago        194.6 MB
#docker start 10f

#docker ps

#docker stop 10f

#docker ps -a

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                        PORTS               NAMES
148df39831f0        af                    "/bin/bash"         6 minutes ago       Exited (2) 6 minutes ago                          admiring_kalam
10fe153d9d6d        sdzn.com/tomcat:0.1   "/root/run.sh"      12 minutes ago      Exited (137) 13 seconds ago                       tomcat1
ee0ee8ac3a93        feac                  "/bin/bash"         31 minutes ago      Exited (0) 19 minutes ago                         sad_noyce

#docker ps -a

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                        PORTS               NAMES
10fe153d9d6d        sdzn.com/tomcat:0.1   "/root/run.sh"      12 minutes ago      Exited (137) 30 seconds ago                       tomcat1
ee0ee8ac3a93        feac                  "/bin/bash"         31 minutes ago      Exited (0) 19 minutes ago                         sad_noyce
#docker rm 10f

#docker rm ee0ee

21.移除已建立的映象

#docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sdzn.com/tomcat     0.1                 af0647ada992        17 minutes ago      514.6 MB
docker.io/centos    7.2.1511            feac5e0dfdb2        8 months ago        194.6 MB
#docker rmi af064

#docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    7.2.1511            feac5e0dfdb2        8 months ago        194.6 MB
至此結束,演示了從0開始搭環境,釋出啟動tomcat到清楚整個容器和映象的完整過程。

後續如何配置多個專案公用一個tomcat的文章待續。

資料參考:

1.https://my.oschina.net/sunchp/blog/616526

2.http://blog.csdn.net/u011627980/article/details/51898234