SoFunction
Updated on 2025-03-08

Detailed explanation of Docker automatic deployment of tomcat

Docker automatically deploys tomcat

1. Download the mirror

# Docker pull centos:latest  #Get the latest centos image

2. Start the container

#docker run -i -t -v /home/user/software/:/home/software/ imageId /bin/bash

In fact, it contains the following three parts:

docker run <relevant parameters> <mirror ID> <initial command>

Among them, the relevant parameters include:

-i: means running the container in "interactive mode"

-t: It means that the container will enter its command line after it is started

-v: indicates which local directory needs to be mounted into the container, format: -v <host directory>: <container directory>

Suppose all our installers are placed in the host's /home/user/software/ directory, and now they need to be mounted to the container's /home/software/ directory. The initial command indicates that the command that needs to be run once the container is started. At this time, use "/bin/bash", which means that nothing is done, just go to the command line.

3. Install JDK

In order to build a Java Web running environment, we need to install JDK and Tomcat, and the following processes are carried out inside the container.

#chmod +x

#./

# java -version

4. Install Tomcat

① Upload the unzipped tomcat to the root directory of /opt/ and name it tomcat7.0. Then configure the environment variables of jdk and tomcat. Modify the file /etc/profile to add the following environment variables at the end of the profile file:

export JAVA_HOME=/usr/java/jdk1.6.0_38
export JRE_HOME=/usr/java/jdk1.6.0_38/jre
export CLASSPATH=.:$JAVA_HOME/lib/:$JAVA_HOME/lib/ 
export TOMCAT_HOME=/opt/tomcat7.0
export CATALINA_HOME=/opt/tomcat7.0 
export PATH=$PATH:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:$JAVA_HOME/bin 

② Setting the startup container is a startup script, add:

# cd /root/

#!/bin/bash

source /etc/profile

sh /opt/tomcat7.0/bin/ run

③ Modify the tomcat user login account and password

# vi /opt/tomcat7.0/conf/

<tomcat-users>

 <role rolename="root"/>

 <user username="root" password="root" roles="manager-gui"/>

</tomcat-users>

5. Exit the container

After all the above steps are completed, you can use the exit command to exit the container. Then, you can use the following command to view the running container:

#docker ps

At this time, you should not see any running programs, because the container that has just been exited with the exit command, and the container is in a stopped state. You can use the following command to view all containers:

#docker ps -a

Remember the above Container ID (Container ID), and then we will create an image that runs the Java Web through the container.

6. Create Java Web Image

Use the following command to create a new "mirror" based on a "Container ID":

#docker commit 57c312bbaad1 javaweb:0.1

The container's ID is "57c312bbaad1", and the created image name is "javaweb:0.1", and the Java Web container can be started using the image.

7. Start Java Web Container

It is necessary to first use the docker images command to view all current images.

It can be seen that the newly created image "javaweb:0.1" has been seen at this time, and its image ID is "d70ea3e3000c". As described above, we can start the container by "mirror name" or "mirror ID". Unlike the last time we started the container, we no longer go to the container's command line, but instead start the Tomcat service inside the container directly. At this time, the following command is required:

#docker run -d -v /home/user/software/:/home/software/ -p 58080:8080 --name javaweb javaweb:0.1 /root/

If creation fails, try restarting docker.

-d: means that the /root/ script is executed in "Daughter mode". At this time, the Tomcat console will not appear on the output terminal.

-p: Indicates the port mapping between the host and the container. At this time, the 8080 port inside the container is mapped to the 58080 port of the host, which exposes port 58080 to the outside world. You can access the 8080 port inside the container through the Docker bridge.

--name: represents the container name, just name it with a meaningful name.

Check that tomcat has started through the log.

10. Test

Enter the URL address locally using the browser: http://10.5.83.210:58080/manager/

success!

Thank you for reading, I hope it can help you. Thank you for your support for this site!