在CentOS7下从0开始搭建docker并发布tomcat项目

时间:2024-12-10 08:04:19

一切从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

  1. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  2. 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

  1. java version "1.7.0_79"
  2. Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
  3. 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

  1. CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
  2. 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

  1. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  2. sdzn.com/tomcat     0.1                 af0647ada992        56 seconds ago      514.6 MB
  3. 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

  1. CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                     NAMES
  2. 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

在CentOS7下从0开始搭建docker并发布tomcat项目

20.停止容器并移除

#docker ps -a

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

#docker stop 10f

#docker ps -a

  1. CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                            PORTS               NAMES
  2. 148df39831f0        af                    "/bin/bash"         2 minutes ago       Exited (2) 2 minutes ago                              admiring_kalam
  3. 10fe153d9d6d        sdzn.com/tomcat:0.1   "/root/run.sh"      8 minutes ago       Exited (137) About a minute ago                       tomcat1
  4. ee0ee8ac3a93        feac                  "/bin/bash"         27 minutes ago      Exited (0) 15 minutes ago                             sad_noyce

#docker images

  1. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  2. sdzn.com/tomcat     0.1                 af0647ada992        12 minutes ago      514.6 MB
  3. docker.io/centos    7.2.1511            feac5e0dfdb2        8 months ago        194.6 MB

#docker start 10f

#docker ps

#docker stop 10f

#docker ps -a

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

#docker ps -a

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

#docker rm 10f

#docker rm ee0ee

21.移除已创建的镜像

#docker images

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

#docker rmi af064

#docker images

  1. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  2. 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.****.net/u011627980/article/details/51898234