第16章 Docker环境下快速构建Tomcat Web服务器:学生实践指南

1,403 阅读3分钟

本文档详细介绍了如何在Docker环境中快速搭建一个安全且高效的Tomcat服务器。通过任务描述、任务实施和实验实训三个部分,引导学生从零开始学习Docker的基本操作、Tomcat镜像的拉取与配置、容器的启动与管理,以及如何验证Tomcat服务的正常运行。本指南不仅提供了详细的步骤说明,还涵盖了常见问题的解决方法,非常适合初学者学习和实践。

任务描述

本任务的目标是在Docker环境中搭建一个功能完备的Tomcat服务器。学生将学习如何使用Docker命令拉取Tomcat镜像、配置容器、设置端口映射和文件挂载,以及如何启动和管理容器。通过本任务,学生将掌握Docker的基本使用技巧和Tomcat的部署方法。

实验实训

  • 目标:通过本实验,学生应能独立完成Docker环境中Tomcat服务器的搭建,理解Docker容器的管理方法。
  • 要求: 完成所有任务步骤,确保Tomcat容器正常运行。 能够从外部浏览器成功访问Tomcat服务。
  • 编写实验报告,记录操作过程和遇到的问题及解决方案。

任务实施

1、安装镜像

可以先到hub.docker.com/  搜索镜像

You can get there first. hub.docker.com/  search mirror

或使用docker search 镜像名称

or use docker search  image name

docker pull tomcat

2、查看验证下载好的镜像

docker images

3、启动tomcat

  1. Start tomcat2. Check and verify the downloaded image.

-p : 8080 in front is the port of this machine, 8080 after the colon is the port of docker container, and tomcat is 8080 by default.

-d:后台运行

-i  启动这个容器

-t 容器创建成功后我们可以进入到容器中

-d: Run in background

--name : 是给容器起一个别名,方便使用,不然docker会默认给容器一个随机字符串的名称

--name : is to give an alias to the container for easy use, otherwise docker will give the container a random string name by default

-v 用来指定目录映射  --指定宿主机的某一个目录和容器中的某一个目录的对应关系

-p 用来指定端口映射  --指定宿主机的某一端口和容器中运行软件端口的对应关系:例如前边的8080是linux本机的端口,冒号后面的8080是docker容器的端口,tomcat默认是8080

-v: file mount path (not used in the above command)

docker run  -it -d --name tomcat 
-v /var/lib/docker/volumes/tomcat:/usr/local/tomcat/webapps 
-v /var/lib/docker/volumes/tomcat:/usr/local/tomcat/logs  
-v /usr/local/tomcat/conf:/usr/local/tomcat/conf   -p 8080:8080 tomcat

4、查看tomcat安装位置

View tomcat installation location

docker exec -it tomcat  /bin/bash

如果想得到容器更为详细信息,例如。挂载目录、tomcat版本等

(2) sudo docker inspect -f {{.State.Pid}} 44fc0f0582d9(2), and then get the full ID according to either of these two items.

docker inspect 容器ID

5、高版本的tomcat,默认把webapps下的测试应用移动到了webapps.dist

 (3) Copy the file from the local to the docker container

cp -rf  /usr/local/tomcat/webapps.dist/ROOT  /usr/local/tomcat/webapps/ROOT

6、访问测试

http://localhost:8080/