Docker tomcat Setting Memory Size Configuration Mode

  • 2021-10-25 08:21:20
  • OfStack

When installing tomcat in docker, memory overflow of tomcat will occur when downloading large files or in some cases, so it is necessary to configure the memory size of tomcat. There are two ways to configure the memory size of tomcat in docker:

1. Mount configuration files in docker

You can mount the catalina. sh configuration file for tomcat in docker to the host machine, and then configure the jvm memory size in catalina. sh.

1. One tomcat container needs to be rerun (note: port and container names cannot be duplicated)


 docker run -d \
 -v /server/webapps:/usr/local/tomcat/webapps/ \
 -v /server/catalina.sh:/usr/local/tomcat/bin/catalina.sh \
 -v /server/logs/demo:/server/logs/demo \
 -e TZ="Asia/Shanghai" \
 --privileged=true \
 --name demo \
 -p 8080:8080 \
 tomcat8

Description:

*-v/server/webapps:/usr/local/tomcat/webapps/Mount the running directory of the current project to webapps under tomcat

*-v/server/catalina. sh:/usr/local/tomcat/bin/catalina. sh mounts catalina. sh of the host to catalina. sh under tomcat in docker

*-v/server/logs/demo:/server/logs/demo mounts logs to the host

*-e TZ= "Asia/Shanghai" Set the time zone

* --privileged=true setting has true root permissions in the container

*-p 8080: 8080 mapping port

* The name of the tomcat8 image

2. Configuration Memory in catalina. sh

cygwin=false Add:


JAVA_OPTS="-server -Xms1024m -Xmx2048m -XX:MaxNewSize=256m -XX:PermSize=128m -XX:MaxPermSize=256m"

-xms initializes heap memory

-xmx Maximum Heap Memory

2. Modify the catalina. sh configuration of the tomcat container directly

You can directly enter the container to modify the configuration file of catalina. sh of tomcat, but this method is not recommended, because it is troublesome to show again later, and it is inconvenient to configure. If you need to rerun the container, you don't know the configuration of the memory size in the container

1. Enter the container


docker exec -it <container_name> /bin/bash

2. Find the catalina. sh configuration file and add the configuration (just add the configuration statement in Step 1)


//  If not vi  Command, you need to install the 
vi /usr/local/tomcat/bin/catalina.sh
//  Exit the container and restart 
exit
docker restart <containner_name>

Note:

If the Catalina. sh is prompted that the file is a read-only file or that there is no permission to modify it, then you need to enter the container with root permission. The specific method is as follows

Supplement: root Permission Acquisition in Docker Container

There are 1 operations that often need to enter the docker container, such as the sudden need to modify a configuration file, etc. However, modifying the file requires root permission, you can refer to the following solution

Usually you will be prompted when modifying a file: read-only file system or Permission denied

1. Mount the configuration file

When running the docker container, you can copy the configuration file to the host machine, and then load the-v parameter when run, and mount the configuration file of the host machine into docker.

2. Enter the docker container with root permission

Command:


docker exec -it -u root <container_id> /bin/bash

Note:

The premise of executing the above command is that when run container, this parameter is added: --privileged=true Otherwise, you will enter the container or prompt that you have no permission to modify it


Related articles: