Get the App
SLTechnology News&Howtos  ›  Development  › 

How to configure Tomcat and run the web project in Docker

Shulou Source: shulou.com Published: 2022-06-01 17:00:00 09月22日 Update

This article mainly explains "how to configure Tomcat in Docker and run the web project". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to configure Tomcat in Docker and run the web project.

First, install Tomcat

1. Find the tomcat image on Docker Hub

Docker search tomcat

2. Pull the official image

Docker pull tomcat

It will take some time to wait for the download to finish.

3. View all the images of docker

Docker images

4. Start the tomcat image

Note: the former is the peripheral access port: the latter is the internal port of the container.

Docker run-d-p 8080 tomcat

Note: the former is the peripheral access port: the latter is the internal port of the container. The following command can start tomcat-d in the background: run the container in the background and return the container ID;-I: run the container in interactive mode, usually used at the same time with-t;-t: reassign a pseudo-input terminal to the container, usually used with-I

Docker run-d-p 8080 tomcat

The startup is successful as follows:

5. View the running container

Docker ps

Deploy your own web project to Tomcat

1. Upload war package

2. Create and edit "Dockerfile" as follows:

From tomcat # Image of your tomcat MAINTAINER * @ 163.com # author RUN rm-rf / usr/local/tomcat/webapps/*COPY button.war / usr/local/tomcat/webapps # is placed in tomcat's webapps directory ENV TZ=Asia/ShanghaiRUN ln-snf / usr/share/zoneinfo/$TZ / etc/localtime & & echo $TZ > / etc/timezone

3. Generate a new image

Docker build-t button:v1.

4. Start a new image

Docker run-d-p 8080 button:v1

5. View the started image

You can add the parameter-itd background to start tomcat to view the started images in the following ways

Docker ps

6. View the items in tomcat

You can enter the tomcat image using the following command:

Docker exec-it * / bin/bash # * is the container id (CONTAINER_ID)

When we enter the image and want to modify some parameters, we usually need to install a text editor. Take vim as an example: since the download address is overseas, the download speed is very slow and may be interrupted, so make the following configuration:

Mv / etc/apt/sources.list / etc/apt/sources.list.bakecho "deb http://mirrors.163.com/debian/ jessie main non-free contrib" > / etc/apt/sources.listecho "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" > > / etc/apt/sources.listecho "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib" > > / etc/apt/sources.listecho "deb-src http : / / mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib "> > / etc/apt/sources.list

Then update

Apt update

Now we can install the software we need, such as vim

Apt install vim

7. Enter ip and port to view

8. You can view the log of the specified container

Docker logs-f container id

9. After deploying the web project, there may be a difference of 8 hours between docker container and host, docker container and tomcat application printing log time and our current time. You can add the following configuration:

When ① starts the container, mounting the system time into the container can solve the problem of inconsistency between the docker container and the host time, as follows: add parameters

-v / etc/localtime:/etc/localtime:ro

The complete command is as follows:

Sudo docker run-d-- name button-api-p 8080V / etc/localtime:/etc/localtime:ro button-api:v1

The ② docker container is inconsistent with the printing log time of the tomcat application. After starting the tomcat container, go to the "bin" directory of tomcat, and modify the catalina.sh file to add the following in the first line at the end of the comment:

JAVA_OPTS= "$JAVA_OPTS-Dfile.encoding=UTF8-Duser.timezone=GMT+08"

As shown in the following figure:

Just restart the docker container after modifying and saving it. Restart method: "docker ps" query container id, and restart using the following command:

Docker restart Container id

The problem can be solved at this point.

Thank you for reading, the above is the content of "how to configure Tomcat in Docker and run web project". After the study of this article, I believe you have a deeper understanding of how to configure Tomcat and run web project in Docker, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Container image project run configuration time port content command parameter background problem learning consistency simultaneous address host log directory application Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB Xiaomi Shulou Technology Microsoft