阿里云服务器使用docker部署Apache Tomcat

背景

最近学习了docker的基础知识,需要实战演练一下。今天就来介绍下,在阿里云服务器中通过docker部署Apache Tomcat并配置公网可以访问。

docker学习教程

部署tomcat

阿里云服务器上docker环境配置

  • 详见本文中的第3和第4点
  • 补充一下,安装完Docker后,按照如下配置可以设置非root用户直接执行docker命令,不会报没有权限错误(无需每次执行docker命令前加上sudo)。
    	#1 添加docker用户组(一般安装docker时会自动添加)
    	sudo groupadd docker 
    	#2 将指定用户添加到docker用户组中 注:将USER替换为自己的用户名
    	sudo gpasswd -a USER docker
    	#3 重启docker服务
    	sudo systemctl restart docker
    	#4 退出SSH连接重新登录或重启系统。
    

阿里云控制台配置安全组规则

因为tomcat默认端口是8080,所以安全组规则就新增一条规则配置端口为8080

在阿里云服务器终端中从Docker hub 官网搜索tomcat镜像并pull下来

[root@aliyun docker_practice]# docker search tomcat
NAME                          DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
tomcat                        Apache Tomcat is an open source implementati…   2773                [OK]                
tomee                         Apache TomEE is an all-Apache Java EE certif…   79                  [OK]                
dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base…   54                                      [OK]
bitnami/tomcat                Bitnami Tomcat Docker Image                     35                                      [OK]
kubeguide/tomcat-app          Tomcat image for Chapter 1                      29                                      
consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              17                                      [OK]
cloudesire/tomcat             Tomcat server, 6/7/8                            15                                      [OK]
aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              13                                      [OK]
arm32v7/tomcat                Apache Tomcat is an open source implementati…   10                                      
rightctrl/tomcat              CentOS , Oracle Java, tomcat application ssl…   6                                       [OK]
maluuba/tomcat7-java8         Tomcat7 with java8.                             5                                       
unidata/tomcat-docker         Security-hardened Tomcat Docker container.      4                                       [OK]
jelastic/tomcat               An image of the Tomcat Java application serv…   2                                       
amd64/tomcat                  Apache Tomcat is an open source implementati…   2                                       
arm64v8/tomcat                Apache Tomcat is an open source implementati…   2                                       
oobsri/tomcat8                Testing CI Jobs with different names.           1                                       
camptocamp/tomcat-logback     Docker image for tomcat with logback integra…   1                                       [OK]
cfje/tomcat-resource          Tomcat Concourse Resource                       1                                       
99taxis/tomcat7               Tomcat7                                         1                                       [OK]
ppc64le/tomcat                Apache Tomcat is an open source implementati…   1                                       
i386/tomcat                   Apache Tomcat is an open source implementati…   1                                       
secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                                       [OK]
s390x/tomcat                  Apache Tomcat is an open source implementati…   0                                       
appsvc/tomcat                                                                 0                                       
picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAGER…   0                                       [OK]
[root@aliyun docker_practice]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
e9afc4f90ab0: Pull complete 
989e6b19a265: Pull complete 
af14b6c2f878: Pull complete 
5573c4b30949: Pull complete 
fb1a405f128d: Pull complete 
612a9f566fdc: Pull complete 
cf63ebed1142: Pull complete 
fbb20561cd50: Pull complete 
76c915a2cfb7: Pull complete 
a2c2864c3363: Pull complete 
Digest: sha256:11f247df062558074169fb92a54033ab2eb6563bda9765b3a9e53106db3c2f4a
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@aliyun docker_practice]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              latest              6055d4d564e1        2 days ago          647MB
centos              latest              831691599b88        3 weeks ago         215MB
nginx               latest              2622e6cca7eb        4 weeks ago         132MB

使用pull下来的tomcat镜像启动一个守护式容器

[root@aliyun docker_practice]# docker run -d -p 8080:8080 --name my_tomcat tomcat
2bc7a26e8569869a199446055c712218a3986b1ccf798fadf80a9473318aec09
[root@aliyun docker_practice]# 
[root@aliyun docker_practice]# 
[root@aliyun docker_practice]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                    NAMES
2bc7a26e8569        tomcat              "catalina.sh run"        36 seconds ago      Up 35 seconds               0.0.0.0:8080->8080/tcp   my_tomcat
d1d4657e2c4f        centos              "/bin/sh -c 'while t…"   23 hours ago        Exited (137) 19 hours ago                            centos02
a8ec86acc075        centos              "/bin/bash"              23 hours ago        Exited (0) 19 hours ago                              my_centos
7185357908cf        bf756fb1ae65        "/hello"                 24 hours ago        Exited (0) 24 hours ago                              dazzling_yalow

此时从浏览器去访问http://wenbin.org.cn:8080/访问首页出现404错误,原因是tomcat目录下的webapps文件夹是空的。原因详见这篇文章

接下来可以通过docker commit命令将修改后的容器生成新的镜像的方式来解决这个问题。

将webapps.dist中的内容复制到webapps中

[root@aliyun docker_practice]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                    NAMES
2bc7a26e8569        tomcat              "catalina.sh run"        36 seconds ago      Up 35 seconds               0.0.0.0:8080->8080/tcp   my_tomcat
d1d4657e2c4f        centos              "/bin/sh -c 'while t…"   23 hours ago        Exited (137) 19 hours ago                            centos02
a8ec86acc075        centos              "/bin/bash"              23 hours ago        Exited (0) 19 hours ago                              my_centos
7185357908cf        bf756fb1ae65        "/hello"                 24 hours ago        Exited (0) 24 hours ago                              dazzling_yalow
[root@aliyun docker_practice]# docker exec -it 2bc7a26e8569 /bin/bash
root@2bc7a26e8569:/usr/local/tomcat# 
root@2bc7a26e8569:/usr/local/tomcat# pwd
/usr/local/tomcat
root@2bc7a26e8569:/usr/local/tomcat# ls -la
total 160
drwxr-xr-x 1 root root  4096 Jul  6 21:28 .
drwxr-xr-x 1 root root  4096 Jul  9 06:18 ..
-rw-r--r-- 1 root root 18982 Jun 30 20:14 BUILDING.txt
-rw-r--r-- 1 root root  5409 Jun 30 20:14 CONTRIBUTING.md
-rw-r--r-- 1 root root 57092 Jun 30 20:14 LICENSE
-rw-r--r-- 1 root root  2333 Jun 30 20:14 NOTICE
-rw-r--r-- 1 root root  3255 Jun 30 20:14 README.md
-rw-r--r-- 1 root root  6898 Jun 30 20:14 RELEASE-NOTES
-rw-r--r-- 1 root root 16262 Jun 30 20:14 RUNNING.txt
drwxr-xr-x 2 root root  4096 Jul  6 21:28 bin
drwxr-xr-x 1 root root  4096 Jul  9 06:17 conf
drwxr-xr-x 2 root root  4096 Jul  6 21:27 lib
drwxrwxrwx 1 root root  4096 Jul  9 06:17 logs
drwxr-xr-x 2 root root  4096 Jul  6 21:28 native-jni-lib
drwxrwxrwx 2 root root  4096 Jul  6 21:27 temp
drwxr-xr-x 2 root root  4096 Jul  6 21:27 webapps
drwxr-xr-x 7 root root  4096 Jun 30 20:12 webapps.dist
drwxrwxrwx 2 root root  4096 Jun 30 20:09 work
root@2bc7a26e8569:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@2bc7a26e8569:/usr/local/tomcat# ls webapps/
ROOT  docs  examples  host-manager  manager

ctrl+p+q不退出容器返回到宿主机目录下,使用docker commit命令将修改后的容器生成新的镜像

root@2bc7a26e8569:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@2bc7a26e8569:/usr/local/tomcat# ls webapps/
ROOT  docs  examples  host-manager  manager
root@2bc7a26e8569:/usr/local/tomcat# read escape sequence
[root@aliyun docker_practice]# docker commit -a='passerby223' -m='modify the container to generate a new image' 2bc7a26e8569 new_tomcat_image
sha256:7cfe9fbc638f7f2a6b8eed2eb522d3ce4b52c0db320a44be25d8f4b46cbc4162
[root@aliyun docker_practice]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
new_tomcat_image    latest              7cfe9fbc638f        5 seconds ago       652MB
tomcat              latest              6055d4d564e1        2 days ago          647MB
centos              latest              831691599b88        3 weeks ago         215MB
nginx               latest              2622e6cca7eb        4 weeks ago         132MB

停止运行并删除之前旧版本的tomcat镜像生成的容器

[root@aliyun docker_practice]# docker stop 2bc7a26e8569
2bc7a26e8569
[root@aliyun docker_practice]# docker rm 2bc7a26e8569
2bc7a26e8569
[root@aliyun docker_practice]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
d1d4657e2c4f        centos              "/bin/sh -c 'while t…"   23 hours ago        Exited (137) 18 hours ago                       centos02
a8ec86acc075        centos              "/bin/bash"              23 hours ago        Exited (0) 19 hours ago                         my_centos
7185357908cf        bf756fb1ae65        "/hello"                 24 hours ago        Exited (0) 24 hours ago                         dazzling_yalow

运行新的镜像

[root@aliyun docker_practice]# docker run -d -p 8080:8080 --name mytomcat new_tomcat_image
0088731d7f4998ee7da5e8f3704b0ff921c4f469f3844eb571adc7bab9f407d6
[root@aliyun docker_practice]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                    NAMES
0088731d7f49        new_tomcat_image    "catalina.sh run"        15 seconds ago      Up 14 seconds               0.0.0.0:8080->8080/tcp   mytomcat
d1d4657e2c4f        centos              "/bin/sh -c 'while t…"   23 hours ago        Exited (137) 19 hours ago                            centos02
a8ec86acc075        centos              "/bin/bash"              23 hours ago        Exited (0) 19 hours ago                              my_centos
7185357908cf        bf756fb1ae65        "/hello"                 24 hours ago        Exited (0) 24 hours ago                              dazzling_yalow

此时再次访问http://wenbin.org.cn:8080/就可以看到tomcat的首页内容了。
image.png