Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Spring Boot open source using Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to use Docker to deploy Spring Boot open source, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The cloud collection project has been open source for more than 2 years. As a hands-on project that just started to learn Spring Boot, it uses a lot of new technologies at that time. Now it seems that many new technologies are not necessary, but as a learning case, it is indeed an excellent Spring Boot practice.

From open source to now, I have written some tutorials to show you how to deploy cloud collections and how to run cloud collections in IDE, but there are still many friends who don't know how to use them and how to deploy them. I have answered questions like "Please provide a cloud collection data structure" at least a hundred times, and have closed nearly ten similar questions on github.

It can also be seen from another aspect that the deployment of cloud collection projects is still complicated for some friends. Now with Docker, we can happily solve this problem. We only need three steps to deploy cloud collection projects and create a personal collection system.

Cloud collection

Some friends may not know about cloud collection yet. Let me give you a brief introduction:

Cloud Collection is an open source website built using Spring Boot, which allows users to collect online anytime, anywhere, and sort the collected sites or articles on the site, which can be used as a temporary storage for later reading. As an open source software, it allows users to import favorite contents from browsers to cloud favorites. It also supports exporting articles collected by cloud collections for backup at any time.

Product home page

Http://favorites.ren

Project home page

Https://github.com/cloudfavorites/favorites-web

Core function points:

Collect, classify and retrieve articles

Export, export (package live from browser)

You can like, share and discuss.

Registration, login, personal account

Collect temporarily and view other people's collections

Others.

The project uses technology:

Vue

Bootstrap

JQuery

Thymeleaf

Spring Data Jpa

Spring Boot Mail

WebJars

Mysql

Tomcat

Redis

The late removal of Redis is due to limited server resources and deployment hassle.

Project transformation

Dependent environment

To prepare a server whose system is Centos 7 or above, the system needs to install Docker and Docker Compos environment. For installation methods, please refer to the previous two articles:

Docker (1): an introduction to Docker

Docker (4): Docker the Docker Compose of the three Musketeers

Docker transformation

Project organization diagram after the transformation of the project

The new content is mainly described here.

Docker-compose.yaml file

Let's first take a look at the docker-compose.yaml file:

Version: '3'services: nginx: container_name: favorites-nginx image: nginx:1.13 restart: always ports:-80:80-443 version: / nginx/conf.d:/etc/nginx/conf.d-/ tmp/logs:/var/log/nginx mysql: build:. / mysql environment: MYSQL_DATABASE: favorites MYSQL_ROOT_PASSWORD: root MYSQL _ ROOT_HOST:'%'TZ: Asia/Shanghai ports:-"3306 volumes: -. / mysql_data:/var/lib/mysql restart: always app: restart: always build:. / app working_dir: / app volumes: -. / app:/app-~ / .m2:/root/.m2-/ tmp/logs:/usr/local/ Logs expose:-"8080" command: mvn clean spring-boot:run-Drun.profiles=docker depends_on:-nginx-mysql

Compared with the previous article, two new parts have been added to this docker-compose.yaml file:

1. Map the logs of Nginx and app to the host, so that we can view the logs.

2. Map the data storage of Mysql to the host, so that the data will not be lost after the cluster is shut down.

In the docker-compose.yaml file, log section:

Version: '3'services: nginx: volumes:-/ tmp/logs:/var/log/nginx app: volumes:-/ tmp/logs:/usr/local/logs

Map the Nginx and cloud collection project logs to the / tmp/logs of the host, respectively, so that we can view the project logs.

Customize mysql initialization information

In the docker-compose.yaml file, the Mysql changes:

Version: '3'services: mysql: build:. / mysql environment: TZ: Asia/Shanghai volumes: -. / mysql_data:/var/lib/mysql

I have extracted all the changes. Mysql added the TZ environment variable to point the time zone to Shanghai. In addition, we proposed the content of the Mysql image and built it separately under the mysql directory of the project. There are two files in the mysql directory, one is the Dockerfile definition Mysql image, the other is the my.cnf file definition Mysql encoding and other information.

My.cnf file content

# omit part of... character_set_server=utf8character_set_filesystem=utf8collation-server=utf8_general_ciinit-connect='SET NAMES utf8'init_connect='SET collation_connection = utf8_general_ci'skip-character-set-client-handshake

The main purpose of this file is to make Mysql support UTF-8.

Dockerfile file content

FROM mysql/mysql-server:5.7COPY my.cnf / etc/my.cnf

Use the Mysql5.7 version and copy the my.cnf in the same directory to the server / etc/my.cnf

In this way, the relevant information about Mysql is defined.

Other

Other contents have not changed much, the configuration file of Nginx is stored in the nginx directory, the application-docker.properties file is added to the project, and the database connection part can be modified.

After the transformation is complete, we just need to copy the project to the deployment server and execute: docker-compose up can be started.

Deployment

I have submitted the transformation content of the project to github, so that when you deploy, you only need three steps to successfully deploy the cloud collection project.

1. Download the source code and decompress

Download the latest release

Wget https://github.com/cloudfavorites/favorites-web/archive/favorites-1.1.1.zip

Decompression

Unzip favorites-1.1.1.zip

Enter the directory

Cd favorites-web-favorites-1.1.1/

2. Modify the configuration file

Modify the file application-docker.properties

Vi app/src/main/resources/application-docker.properties

The modifications are as follows

Favorites.base.path= http://xx.xxx.xx.xx/

Address is the address of the deployment server

3. Start the project

After the configuration is completed, the background starts

[root@~] # docker-compose up-dCreating network "favoriteswebfavorites111_default" with the default driverCreating favorites-nginx... DoneCreating favoriteswebfavorites111_mysql_1... DoneCreating favoriteswebfavorites111_app_1... Done

After launching, the browser visits the above configuration address: http://xx.xxx.xx.xx/, and you can see the home page of the cloud collection.

Auxiliary content

To check the operation of the service in a container after startup, you can use the following command to enter:

Use docker ps to view the Docker container running on the host

[root@VM_73_217_centos ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa466ce6e58a5 favoriteswebfavorites111_app "/ usr/local/bin/mv..." 16 hours ago Up 16 hours 8080/tcp Favoriteswebfavorites111_app_11b4f1b912de0 nginx:1.13 "nginx-g'daemon..." 16 hours ago Up 16 hours 0.0.0.0 daemon 80-> 80/tcp 0.0.0.0hours 443-> 443/tcp favorites-nginx65b481bb7741 favoriteswebfavorites111_mysql "/ entrypoint.sh my..." 16 hours ago Up 16 hours (healthy) 0.0.0.0 hours 3306-> 3306/tcp, 33060/tcp favoriteswebfavorites111_mysql_1

Based on the Docker container ID information queried above, execute the following command

Docker exec-ti CONTAINER_ID bash#, for example, enter the project container [root@VM_73_217_centos ~] # docker exec-ti a466ce6e58a5 bashroot@a466ce6e58a5:/app# ps-ef | grep java...

Exit the container and execute the following command:

Root@a466ce6e58a5:/app# exitext [root @ VM_73_217_centos ~] #

In this way, it will be very easy for us to deploy cloud collection projects in the future, and we only need three steps to build our own collection system happily.

On how to use Docker to deploy Spring Boot open source to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report