Detailed tutorial on running multiple Springboot with Docker

Detailed tutorial on running multiple Springboot with Docker

Docker runs multiple Springboot

First: Port mapping Second: Specify memory size Third: Read and write physical files Fourth: Log files Fifth: Multiple container internal network access Sixth: Problems encountered

First: Port Mapping

Nginx uses forwarding, so this is the simplest.

[hn@iZbp1gp1t778obaz5m8vk8Z guides]$ docker run -d -p 8093:8093 guides-collect
 
Just map the physical port to a virtual port. This is very simple.

Second: Specify the memory size

-m, --memory Memory limit, the format is a number plus a unit, the unit can be b, k, m, g. Minimum 4M
--memory-swap Total limit of memory + swap partition size. Same format as above. Must be larger than the soft limit set by -m --memory-reservation Memory limit. Same format as above --oom-kill-disable Whether to prevent OOM killer from killing containers, not set by default --oom-score-adj The priority of the container being killed by OOM killer, the range is [-1000, 1000], the default is 0
--memory-swappiness is used to set the virtual memory control behavior of the container. The value is an integer between 0 and 100. --kernel-memory Kernel memory limit. Same format as above, minimum size is 4M
Example:
 
[hn@iZbp1gp1t778obaz5m8vk8Z guides]$ docker run -d -m 1G --memory-swap 4G -p 8093:8093 guides-collect

The third one: read physical files and write physical files

The above two situations are loaded at startup, but what should I do if I need to read local files continuously during runtime?

[hn-docker@iZbp1gp1t778obaz5m8vk8Z ~]$ docker run -d -m 1G --memory-swap 4G -p 8092:8092 \
-v /home/hn-docker/docker/wechat:/springboot/wechat \
guides-message
 
The startup of the message container is equivalent to mapping the file address in the image to the address on the physical machine; for example, the image address is: /springboot/wechat
Physical address: /home/hn-docker/docker/wechat

Bind mounts

The Bind mounts mode is very similar to Volumes. The difference is that the Bind mounts mode mounts any file or folder on the host to the container, while Volumes essentially mounts an area managed by the Docker service (the default is a folder under /var/lib/docker/volumes) to the container.

The use of bind mounts is similar to that of volumes, and the host files are mounted in the container through -v or --mount parameter. Here is an example:

When using the --mount parameter, you need to specify type=bind :

$ docker run -d \
 --name=nginxtest \
 --mount type=bind,source=/usr/local/web,destination=/usr/share/nginx/html \
 nginx:latest

The above example mounts the /usr/local/web folder on the host to the /usr/share/nginx/html folder in the container.

Or use the -v parameter:

$ docker run -d \
 --name=nginxtest \
 -v /usr/local/web:/usr/share/nginx/html \
 nginx:latest

For details, please refer to: Docker Data Storage Bind Mounts Detailed Explanation

Fourth: Log File

Step 1: Get the ID of the running container: da3199e9d032    
 
[hn@iZbp1gp1t778obaz5m8vk8Z guides]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da3199e9d032 guides-collect "java -Djava.secur..." 2 hours ago Up 2 hours 0.0.0.0:8093->8093/tcp sharp_mirzakhani
 
Step 2: Query the running log - Done [hn@iZbp1gp1t778obaz5m8vk8Z guides]$ docker logs -f 51c384b8afee

Reference Information:

Command format:

$ docker logs [OPTIONS] CONTAINER
 Options:
    --details Display more information -f, --follow Track real-time logs --since string Display logs after a certain timestamp, or relative time, such as 42m (i.e. 42 minutes)
    --tail string How many lines of log are displayed from the end of the log, the default is all
  -t, --timestamps Display timestamps --until string Display logs before a certain timestamp, or relative time, such as 42m (i.e. 42 minutes)

example:

View the logs after the specified time and only display the last 100 lines:

$ docker logs -f -t --since="2018-02-08" --tail=100 CONTAINER_ID

View the logs for the last 30 minutes:

$ docker logs --since 30m CONTAINER_ID

View the logs after a certain time:

$ docker logs -t --since="2018-02-08T13:23:37" CONTAINER_ID

View logs for a certain period of time:

$ docker logs -t --since="2018-02-08T13:23:37" --until "2018-02-09T12:23:37" CONTAINER_ID

Fifth: Host and container network interaction

Problem description: When the springboot running in your docker wants to access the host's Redis and get the MySQL of other containers, are you dumbfounded?

Solution: You need to query the soft connection IP of the host container and use the intranet IP to access it. docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' xxxid (container id)
 
For example:
[yn@iZbp1gp1t778obaz5m8vk8Z logs]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abe77b60254a assistant "java -Djava.secur..." 6 minutes ago Up 6 minutes 0.0.0.0:9304->8091/tcp nostalgic_albattani
37c6dd777636 collect "java -Djava.secur..." 30 hours ago Up 30 hours 0.0.0.0:8093->8093/tcp silly_poincare
4dcb9fcae281 message "java -Djava.secur..." 3 days ago Up 3 days 0.0.0.0:8092->8092/tcp reverent_galileo
 
For example, to check: abe77b60254a The network address of this container [yn@iZbp1gp1t778obaz5m8vk8Z logs]$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' abe77b60254a
172.17.0.4
 
At this time:
The IP address of the abe77b60254a container is: 172.17.0.4
If you want to access it, just use the intranet address 172.17.0.4. At the same time, you can also know that the host machine's IP should be 172.17.0.1

Sixth: The pitfalls you will encounter

1: Mirror startup fails, then how to view the log

After the startup fails, you need to know which image ID it is, and then use the log to check: docker logs -f image id

For example:
[yn@iZbp1gp1t778obaz5m8vk8Z logs]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abe77b60254a assistant "java -Djava.secur..." 6 minutes ago Up 6 minutes 0.0.0.0:9304->8091/tcp nostalgic_albattani
37c6dd777636 collect "java -Djava.secur..." 30 hours ago Up 30 hours 0.0.0.0:8093->8093/tcp silly_poincare
4dcb9fcae281 message "java -Djava.secur..." 3 days ago Up 3 days 0.0.0.0:8092->8092/tcp reverent_galileo
 
Check the latest 10 lines of this container log [yn@iZbp1gp1t778obaz5m8vk8Z ~]$ docker logs -f -t --tail 10 abe77b60254a

2. The log time or data stored in the database is 8 hours less than the actual time

RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone 

This is the end of this article about the detailed tutorial on running multiple Springboots with Docker. For more information about running multiple Springboots with Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the infinite restart problem when running the SpringBoot project docker environment
  • How to run the springboot project in docker
  • Implementation of running springboot project with Docker

<<:  Solve the problem of no my.cnf file in /etc when installing mysql on Linux

>>:  Steps to split and compress CSS with webpack and import it with link

Recommend

Detailed steps for implementing timeout status monitoring in Apache FlinkCEP

CEP - Complex Event Processing. The payment has n...

How to use Portainer to build a visual interface for Docker

Portainer Introduction Portainer is a graphical m...

MySQL table type storage engine selection

Table of contents 1. View the storage engine of t...

How to monitor and delete timed out sessions in Tomcat

Preface I accidentally discovered that the half-h...

Detailed explanation of Linux zabbix agent deployment and configuration methods

1. Install zabbix-agent on web01 Deploy zabbix wa...

Vue implements simple image switching effect

This article example shares the specific code of ...

Detailed process of building mongodb and mysql with docker-compose

Let's take a look at the detailed method of b...

Use of Linux stat command

1. Command Introduction The stat command is used ...

How to deploy Rancher with Docker (no pitfalls)

Must read before operation: Note: If you want to ...

How to manually deploy war packages through tomcat9 on windows and linux

The results are different in Windows and Linux en...

A brief discussion on Nginx10m+ high concurrency kernel optimization

What is high concurrency? The default Linux kerne...

jQuery implements percentage scoring progress bar

This article shares the specific code of jquery t...

Detailed explanation of the WeChat applet request pre-processing method

question Because some of our pages request data i...

Solution to the problem that Centos8 cannot install docker

Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...