Two ways to deploy war package in tomcat container under docker

Two ways to deploy war package in tomcat container under docker

  • [Deploy java web project] in tomcat image[]

  • Package the javaweb project

    Like the usual javaweb project deployment, you first need to package the javaweb project into a war package, which will not be described in detail.

  • View the container started by the tomcat image

    containerID is the container ID or container name of the tomcat image you started .</p> <p>This is to put test .war under webapps of tomcat

    Record the ID or container name of the container started by the tomcat image.

  • method one
    ==============

  • Put the war package into [tomcat] under docker
    ————————————————————————————————————–

    First of all, you need to put the war package into the centos system. In this article, a test.war project is put into the main directory of the centos system. This can be put into the centos through the ssh tool. Everyone should be familiar with it. Move test.war from centos to the container started by the tomcat image under docker.

    docker cp test.war containerID:/usr/local/tomcat/webapps/

    illustrate:

    docker exec -it ea422adc42c5 bash #Enter
    the webapps folder to check the existing war package
    cd webapps

    • Docker common commands

      For the convenience of understanding, the tomcat image is taken as an example. One line of syntax description, one line of example.

  • Generate a container from an image

    docker run -p port1:port2 containerName:tag    

    Note: port1 is the system port, port2 is the container port

    docker run -p 8080:8080 tomcat:latest

  • start an existing container

    docker start containerID/containerName

    Where admiring_turing is a container name created by the tomcat image, or its container ID can be used

  • stop a container

    docker stop containerID/containerName

  • restart a container

    docker restart containerID/containerName

  • Interact with an already started container

    docker exec -i -t container ID / image name /bin/bash

  • Copy files to container

    docker cp /test.txt Image name: /usr/local/

    test.txt is a test file created by myself and copied to the /usr/local directory of the container.

  • Run a container in the background

    docker run -d -p tomcat:latest

  • Interactively enter the container that was just run

docker ps

docker exec -it mytomcat7 /bin/bash

  • Copy the war package to the tomcat webapps directory in the container

docker cp dockertest.war mytomcat7:/usr/local/tomcat/webapps

  • quit

exit

Method Two:

Enter the file directory of the Docker container and configure the tomcat administrator through tomcat-users.xml

  • First check the CONTAINER ID of the tomcat container :

root@ea422adc42c5:/usr/local/tomcat/webapps# cd ..
root@ea422adc42c5:/usr/local/tomcat# cd conf/
root@ea422adc42c5:/usr/local/tomcat/conf# ls
Catalina     catalina.properties  jaspic-providers.xml  logging.properties  tomcat-users.xml  web.xml
catalina.policy  context.xml          jaspic-providers.xsd  server.xml      tomcat-users.xsd
root@ea422adc42c5:/usr/local/tomcat/conf# 
root@ea422adc42c5:/usr/local/tomcat/conf# vi tomcat-users.xml 
bash: vi: command not found
root@ea422adc42c5:/usr/local/tomcat/conf#

  • Enter the container’s internal file system 

Access inside the container There are some settings files inside the container that can only be accessed in this way 

Interactively enter the tomcat container. The “docker exec” command starts a new process in a running container.

Access inside the container There are some settings files inside the container that can only be accessed in this way

2. exec enters the container to modify docker exec -it containerID bash You can install vi for modification

3. cp can copy the configuration file of the host to the container, docker cp host/path:/container/path

  • Go to the conf folder of tomcat:

#cp the xml file into the container 
docker cp tomcat-users.xml ea422adc42c5:/usr/ local /tomcat/conf/

#Enter the container to view 
docker exec -it ea422adc42c5 bash

Then we found that the xml file cannot be edited directly with the vi command.

Here are three methods:

<?xml version='1.0' encoding='utf-8'?>

<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
     <role role/>    
    <role role/>    
    <role role/>    
    <role role/>    
    <role role/>    
    <role role/>    
    <role role/>    
    <user user/> 

</tomcat-users>

It is recommended to use the third

Find a configured tomcat-users.xml file outside the tomcat container.

How to configure the tomcat-users.xml file can refer to ( /qq_39530754/article/details/82387497 )

as follows:

We copy this file to the Tomcat container and overwrite the tomcat-users.xml in the tomcat container.

#cp the xml file into the container 
docker cp tomcat-users.xml ea422adc42c5:/usr/ local /tomcat/conf/

#Enter the container to view 
docker exec -it ea422adc42c5 bash

Here is my tomcat-users.xml file:

<?xml version='1.0' encoding='utf-8'?>

<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
     <role role/>    
    <role role/>    
    <role role/>    
    <role role/>    
    <role role/>    
    <role role/>    
    <role role/>    
    <user user/> 

</tomcat-users>

The yellow proof copy came in.

Let’s restart the tomcat container

docker restart ea422adc42c5

Wait for a while to see if the configuration is successful. If the configuration is not successful, you need to configure two other xml files.

You can refer to ( /qq_39530754/article/details/82387497 )

Copy these configuration files to the corresponding location of the tomcat container,

After restarting, it’s ok~~

Then we can directly deploy our war package here.

Leave a Comment

Your email address will not be published. Required fields are marked *