Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Tomcat web in docker

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Introduction to Tomcat

Tomcat is a core project of the Jakarta project of the Apache Software Foundation (Apache Software Foundation), which is jointly developed by Apache, Sun and other companies and individuals. With the participation and support of Sun, the latest Servlet and JSP specifications are always reflected in Tomcat, and Tomcat 5 supports the latest Servlet 2.4 and JSP 2.0 specifications. Because of its advanced technology, stable performance and free, Tomcat is deeply loved by Java enthusiasts and recognized by some software developers, and has become a popular Web application server.

Advantages of Tomcat

1. It has both expandability and security of du. If the number of access requests zhi increases sharply per unit time, you can use a relatively low fee dao to increase the front-end capacity.

2. It is easy to expand. Because it is a single servlet container running on this powerful server, there is no need to consider complex issues such as the maintenance of session state in a distributed environment.

3. The back-end server is not accessible to the outside world, and the hackers on the Internet can only access the front-end web server, but not the back-end application server directly, which improves the security. But this does not mean that hackers cannot carry out other forms of attack through web browsers.

I. preparatory work

Turn off the selinux service

Getenforce checks the selinux status and closes selinux, otherwise it will report an error when starting docker.

[root@localhost ~] # getenforce View selinux status

Disabled

Modify selinux configuration file

Docker startup error message:

Restart the server after modifying the selinux:

[root@localhost ~] # reboot

(note: installing software as docker may encounter this problem, while installing software as docker-ce may not.)

Download and install docker-ce

[root@localhost ~] # yum list | grep docker-ce checks the docker packet information of the yum source

[root@localhost~] # yum-config-manager-- add-repo https://download.docker.com/linux/centos/docker-ce.repo installs rpm update source

[root@localhost ~] # yum list docker-ce-- showduplicates | sort-r to view the docker version in the repository

[root@localhost ~] # yum install-y docker-ce install docker

[root@localhost ~] # systemctl strat docker.service starts the docker program

[root@localhost ~] # systemctl status docker.service to view the running status of docker

[root@localhost ~] # systemctl enable docker.service add boot boot

[root@localhost ~] # docker version View docker version

Client: Docker Engine-Community

Version: 19.03.5

API version: 1.40

Go version: go1.12.12

Git commit: 633a0ea

Built: Wed Nov 13 07:25:41 2019

OS/Arch: linux/amd64

Experimental: false

Deploy the jave environment

Wget https://download.oracle.com/otn/java/jdk/8u231-b11/5b13a193868b4bf28bcb45c792fce896/jdk-8u231-linux-x64.tar.gz?AuthParam=1575959715_a901df40b664e3cff9415b957ddf441e

[root@localhost ~] # docker cp jdk-8u231-linux-x64.tar.gz 4c069f9a9851:/root copies jdk to the container

Copy command: docker cp:

[root@localhost ~] # docker exec-it tomcat / bin/bash enters the Tomcat container

Root@4c069f9a9851:~# cd / root/

Root@4c069f9a9851:~# tar-zxvf jdk-8u231-linux-x64.tar.gz decompresses jdk

Modify the environment variable, add the jdk variable to .bashrc, and add the following code.

Export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_231

Export JRE_HOME=$ {JAVA_HOME} / jre

Export CLASSPATH=.:$ {JAVA_HOME} / lib:$ {JRE_HOME} / lib

Export PATH=$ {JAVA_HOME} / bin:$PATH

Restart the Tomcat container:

[root@localhost src] # docker ps-a

CONTAINERID IMAGE COMMAND CREATED STATUS PORTS NAMES

E14bcf5dd711 training/sinatra "/ bin/bash" 19 hours ago Exited (0) 19 hours ago kind_golick

4c069f9a9851 tomcat "catalina.shrun" 19 hours ago Up 17h ours 0.0.0.0 hours ago Up 8081-> 8080/tcp tomcat

[root@localhost src] # docker restart 4c069f9a9851

Or

[root@localhost src] # docker restart tomcat

Second, install web application

[root@localhost ~] # docker search tomcat looks up information about Tomcat images on the web server

We choose the one with the most "STARS" resources to pull.

[root@localhost ~] # docker pull docker.io/tomcat pull Tomcat image

[root@localhost ~] # docker run-d-- name tomcat-p 8081 name tomcat 8080 docker.io/tomcat background running Tomcat image

If the port is occupied, you can specify the mapping port of the container and the host, the former is the peripheral access port, and the latter is the internal port of the container. -d parameter: the container will run in the background and will not print the output to the host. A unique id is returned when started with the-d argument.

Note: encountered here that the Tomcat image cannot be started without running this command. The error is as follows:

[root@localhost ~] # docker start docker.io/tomcat

Error response from daemon: No such container: docker.io/tomcat

Error: failed to start containers: docker.io/tomcat

Second, when viewing the mirror status, no information is displayed:

[root@localhost] # docker ps-a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

[root@localhost] # docker run-d-- name tomcat-p 8081 name tomcat 8080 docker.io/tomcat

[root@localhost ~] # systemctl status firewalld.service to view the running status of firewall

[root@localhost ~] # docker start tomcat

Note:

Premise: it cannot be started when the Tomcat image is started because the firewall was turned off earlier.

Reason: due to the need for NAT service in Tomcat image, the NAT service cannot be used when the firewall is disabled.

Problem information:

[root@localhost ~] # docker start tomcat

Error response from daemon: driver failed programming external connectivity on endpoint tomcat (826ed3a9043296bc37b913c8f0cd900fe7424ffdcb93ff22d5f95fc39070a21f): (iptables failed: iptables-- wait-t nat-A DOCKER-p tcp-d 0 DNAT 0-- dport 8081-j DNAT-- to-destination 172.17.0.2 to-destination 8080!-I docker0: iptables: No chain/target/match by that name.

[root@localhost ~] # docker ps-a View Image Information

Note whether the PORTS information of the Tomcat image is 0.0.0.0IP:8081 8081-> 8080/tcp content. If so, access the IP:8081 directly. If not, you may need to do NAT conversion and customize the port access.

Third, effect picture

Fourth, deploy web page testing

Copy web data into a Tomcat container

[root@localhost ~] # docker cp XXX.war 4c069f9a9851:/usr/local/tomcat/webapps

[root@localhost ~] # docker exec-it 4c069f9a9851 / bin/bash enters the Tomcat container

Root@4c069f9a9851:/usr/local/tomcat# cd webapps/

Root@4c069f9a9851:/usr/local/tomcat/webapps# ls

ROOT docs examples host-manager index.html manager

Exit the docker container, Ctrl+p+q

Restart the container, docker restart tomcat

At this point, the web deployment is complete.

Please refer to: https://blog.csdn.net/liqz666/article/details/82180581

5. Common commands

Ip addr/ip link/ ifconfig: check the ip address in the host (the host indicates a virtual machine)

Systemctl start docker/service start docker: start the docker service

Systemctl stop docker: turn off the docker service

Docker run: start container

Docker seach xxxx (Mirror name): retrieving an image

Docker pull xxx (Mirror name): pull image

Docker images: list mirrors

Docker-- version: view the version information of docker

Docker exec Container Id: enter the container

Docker exec-it NAMES (custom name) / bin/bash: enter-- name named container

Docker exec NAMES (custom name of the container)-it bash: enter the container to perform operations

Docker attach Container Id: enter the terminal of the container

Docker inspect IMAGE ID: displays container or mirrored metadata

[root@ (container id)]: for example, root@24527d42d3eb indicates that it has entered the container of a CONTAINER ID in docker.

Docker ps: check the container running in docker (status is Up.... Indicates that it is running)

Docker ps-a: view containers in docker that are stopped or running (status is Exit)

Docker start CONTAINER ID: if the status of the container is Exit, use it to start the container

Docker stop CONTAINER ID: stop a running container. Deleting a container must be stopped first.

Docker restart: when you deploy the war file of the web project to the webapps in tomcat, restart the docker service and automatically extract the war file.

Docker rm CONTAINER ID: deleting containers

Docker rm $(docker ps-a-Q): delete all stopped containers

Docekr rmi IMAGE ID: deleting mirrors

Rm-rf xxxx: forcibly deletes a war. File or directory, file

Docker cp / xxx.war CONTAINER ID: / usr/local/tomcat/webapps: copy a packaged Java web war file from the / root directory to the webapps working directory in the tomcat container

Docker exec CONTAINER ID ls / usr/local/tomcat: default webapps directory where tomcat image is installed

Docker run-name xxx-p (custom port number used for access): (container port number: for example, tomcat 8080 docker MySQL 3306)-d docker.io/tomcat (image name): indicates that a tomcat container is running in docker. If there is no image, automatically pull a tomcat image.

Docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

-- name gives the container a name

The port number to be mapped by the-p container

-d: running in the background

-t: enter the terminal

-I: get an interactive connection by getting the input of container

/ bin/bash: start a bash shell in container

For example: docker run-- name tomcat-p 8081 tomcat 8080-d

Check the docker log

[root@localhost ~] # journalctl-u docker.service View docker Log

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report