1.安装docker

[root@iZ2ze82p1dogve7neb5tuoZ ~]# yum install docker

2.查看 docker 是否成功

[root@iZ2ze82p1dogve7neb5tuoZ ~]# docker

会显示相关的docker命令

3.宿主机 /data下载好tomcat和jdk

[root@iZ2ze82p1dogve7neb5tuoZ data]# ll
total 194628
-rw-r--r-- 1 root root   9532698 Mar  5 21:50 apache-tomcat-8.5.29.tar.gz
-rw-r--r-- 1 root root 189756259 Dec 21 03:10 jdk-8u161-linux-x64.tar.gz?AuthParam=1522988861_96ffdef6b5315866dc4e0e7fcc908849
[root@iZ2ze82p1dogve7neb5tuoZ data]# 

3.2 下载docker image,这里下载的是centos

  docker pull  centos

4.启动docker容器,并挂载 目录到docker容器内

启动容器:

[root@iZ2ze82p1dogve7neb5tuoZ data]# docker run  -i -t -v  /data:/mnt/software  2d194b392dd1 /bin/bash

进入 容器:

[root@cece81dcd1b2 /]# ll
total 56
-rw-r--r--  1 root root 11976 Mar  2 01:07 anaconda-post.log
lrwxrwxrwx  1 root root     7 Mar  2 01:06 bin -> usr/bin
drwxr-xr-x  5 root root   360 Apr  6 04:50 dev
drwxr-xr-x  1 root root  4096 Apr  6 04:50 etc
drwxr-xr-x  2 root root  4096 Nov  5  2016 home
lrwxrwxrwx  1 root root     7 Mar  2 01:06 lib -> usr/lib
lrwxrwxrwx  1 root root     9 Mar  2 01:06 lib64 -> usr/lib64
drwxr-xr-x  2 root root  4096 Nov  5  2016 media
drwxr-xr-x  1 root root  4096 Apr  6 04:50 mnt
drwxr-xr-x  2 root root  4096 Nov  5  2016 opt
dr-xr-xr-x 84 root root     0 Apr  6 04:50 proc
dr-xr-x---  2 root root  4096 Mar  2 01:07 root
drwxr-xr-x  1 root root  4096 Apr  6 04:50 run
lrwxrwxrwx  1 root root     8 Mar  2 01:06 sbin -> usr/sbin
drwxr-xr-x  2 root root  4096 Nov  5  2016 srv
dr-xr-xr-x 13 root root     0 Apr  6 04:50 sys
drwxrwxrwt  7 root root  4096 Mar  2 01:07 tmp
drwxr-xr-x 13 root root  4096 Mar  2 01:06 usr

drwxr-xr-x 18 root root  4096 Mar  2 01:07 var

查看挂载点:

[root@cece81dcd1b2 /]#  ll  /mnt/software   
total 194628
-rw-r--r-- 1 root root   9532698 Mar  5 13:50 apache-tomcat-8.5.29.tar.gz
-rw-r--r-- 1 root root 189756259 Dec 20 19:10 jdk-8u161-linux-x64.tar.gz?AuthParam=1522988861_96ffdef6b5315866dc4e0e7fcc908849

[root@cece81dcd1b2 /]# 

宿主机的/data 中的数据都可使用

5. 解压 jdk和tomcat 分别到/usr/local/jdk和/usr/local/tomcat

在usr/local中分别创建jdk和tomcat

[root@cece81dcd1b2 local]# mkdir  jdk tomcat
[root@cece81dcd1b2 local]# ll
total 48
drwxr-xr-x 2 root root 4096 Nov  5  2016 bin
drwxr-xr-x 2 root root 4096 Nov  5  2016 etc
drwxr-xr-x 2 root root 4096 Nov  5  2016 games
drwxr-xr-x 2 root root 4096 Nov  5  2016 include
drwxr-xr-x 2 root root 4096 Apr  6 04:57 jdk
drwxr-xr-x 2 root root 4096 Nov  5  2016 lib
drwxr-xr-x 2 root root 4096 Nov  5  2016 lib64
drwxr-xr-x 2 root root 4096 Nov  5  2016 libexec
drwxr-xr-x 2 root root 4096 Nov  5  2016 sbin
drwxr-xr-x 5 root root 4096 Mar  2 01:06 share
drwxr-xr-x 2 root root 4096 Nov  5  2016 src
drwxr-xr-x 2 root root 4096 Apr  6 04:57 tomcat
[root@cece81dcd1b2 local]# 

解压jdk:

[root@cece81dcd1b2 jdk]# tar -zxvf /mnt/software/jdk-8u161-linux-x64.tar.gz\?AuthParam\=1522988861_96ffdef6b5315866dc4e0e7fcc908849 

解压tomcat:

[root@cece81dcd1b2 tomcat]# tar -zxvf /mnt/software/apache-tomcat-8.5.29.tar.gz 

6. 配置jdk环境变量

 

[root@cece81dcd1b2 etc]# vi /etc/profile

把下面的路径添加到最后:

#config jdk environment  path

export JAVA_HOME=/usr/local/jdk/jdk1.8.0_161

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export PATH=$JAVA_HOME/bin:$PATH

查看 jdk 安装

[root@cece81dcd1b2 /]# java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
[root@cece81dcd1b2 /]# 

 

7.退出容器

exit

8.查看刚才进入的容器

[root@iZ2ze82p1dogve7neb5tuoZ /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
cece81dcd1b2        2d194b392dd1        "/bin/bash"         43 minutes ago      Exited (1) About a minute ago                       reverent_lovelace

9621dbb1999e        516082cd94fa        "catalina.sh run"   39 hours ago        Exited (143) 5 seconds ago                          competent_shockley

9.根据某个容器id,创建新的镜像

[root@iZ2ze82p1dogve7neb5tuoZ /]# docker  commit cece81dcd1b2  javaweb:1.0
sha256:473aa6b18d586d5c8258171d7bb3c84b1980c7f4b6dc64afd1aed5e985cb2b23

[root@iZ2ze82p1dogve7neb5tuoZ /]# 

cece81dcd1b2  :容器id

 javaweb:1.0:新的镜像名

 

查看镜像:

[root@iZ2ze82p1dogve7neb5tuoZ /]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE

 

javaweb             1.0                 473aa6b18d58        About a minute ago   613 MB
docker.io/tomcat    latest              516082cd94fa        2 weeks ago          547 MB
docker.io/centos    latest              2d194b392dd1        4 weeks ago          195 MB

 

[root@iZ2ze82p1dogve7neb5tuoZ /]# 

10.启动创建新的容器

[root@iZ2ze82p1dogve7neb5tuoZ /]# docker run -i -t -p 8080:8080 --name javaweb  473aa6b18d58  /bin/bash

-i:交互模式

-t:进入命令模式

-p:端口映射   宿主机端口:docker端口

--name:给启动容器起名字

473aa6b18d58  :镜像id 或者 镜像名字也行

/bin/bash : linux启动加载的

[root@a7bbefc5c0c8 /]# 

11.在a7bbefc5c0c8  启动 tomcat 

 

先加载jdk环境变量配置:

[root@a7bbefc5c0c8 /]# source /etc/profile

[root@a7bbefc5c0c8 /]# java -version

 

java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

 

[root@a7bbefc5c0c8 /]# 

启动 tomcat:

[root@a7bbefc5c0c8 /]# ./usr/local/tomcat/apache-tomcat-8.5.29/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat/apache-tomcat-8.5.29
Using CATALINA_HOME:   /usr/local/tomcat/apache-tomcat-8.5.29
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-8.5.29/temp
Using JRE_HOME:        /usr/local/jdk/jdk1.8.0_161
Using CLASSPATH:       /usr/local/tomcat/apache-tomcat-8.5.29/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-8.5.29/bin/tomcat-juli.jar
Tomcat started.

[root@a7bbefc5c0c8 /]# 

 

查看tomcat是否启动:

[root@a7bbefc5c0c8 /]# ps -ef 

 

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 05:44 ?        00:00:00 /bin/bash
root        60     1  2 05:54 ?        00:00:03 /usr/local/jdk/jdk1.8.0_161/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-8.5.29/conf/logging.properties -Djava.util.logging.manager=o
root       104     1  0 05:56 ?        00:00:00 ps -ef
[root@a7bbefc5c0c8 /]# 

 

12. 后台启动dcoker

ctrl+p+q:后台运行docker

查看:

[root@iZ2ze82p1dogve7neb5tuoZ /]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
a7bbefc5c0c8        473aa6b18d58        "/bin/bash"         19 minutes ago      Up 19 minutes       0.0.0.0:8080->8080/tcp   javaweb
[root@iZ2ze82p1dogve7neb5tuoZ /]# 

13.现在在浏览器 输入 ip:8080 ,就可以看到tomcat主页了

 

success!

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐