Home Java javaTutorial Use Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications

Use Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications

Oct 20, 2023 pm 03:16 PM
docker spring boot Rapid deployment

使用Docker和Spring Boot实现应用的快速部署和水平扩展

Use Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications

With the development of cloud computing and container technology, more and more enterprises are beginning to adopt Docker To implement application deployment and management. Docker is characterized by being lightweight, highly portable, and capable of rapid deployment and expansion, while Spring Boot is a framework for building Java applications and provides a way to simplify development. This article will introduce how to combine Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications, and provide specific code examples.

1. The concept and use of Docker

Docker is a container technology that can package applications and their dependent environments into a container to achieve rapid deployment and transplantation of applications in different environments. and share.

  1. Install Docker

First of all, we need to install Docker. You can go to the Docker official website to download the corresponding installation package and install it according to the official documentation.

  1. Create a Docker image

Docker image is the basis of the Docker container and the packaging form of the application. We can use Dockerfile to define the image building process. The following is a simple Dockerfile example:

FROM openjdk:11
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Copy after login
Copy after login

This example uses the officially provided OpenJDK 11 as the base image, copies the application's jar package into the container, and specifies the startup command through the ENTRYPOINT instruction.

  1. Build the Docker image

In the directory where the Dockerfile is located, execute the following command to build the Docker image:

docker build -t my-app .
Copy after login
Copy after login

Among them, my- app is the name of the image and can be modified according to the actual situation.

  1. Run the Docker container

After building the image, we can use the following command to run the Docker container:

docker run -d -p 8080:8080 my-app
Copy after login
Copy after login

Among them, -d# The ## parameter indicates running the container in background mode, the -p parameter indicates mapping the host's 8080 port to the container's 8080 port, and my-app is the name of the image.

2. Rapid deployment of Spring Boot applications

Spring Boot is a rapid development framework that can be used to quickly build independent, Spring-based applications.

    Create a Spring Boot application
First, we need to create a Spring Boot application. You can use Spring Initializr (https://start.spring.io/) to generate a basic Spring Boot project.

    Writing application logic
In the generated project, we can write our own business logic. Here is a simple RESTful interface as an example. The code is as follows:

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, Docker!";
    }

}
Copy after login

    Package into an executable jar package
In the root directory of the project, execute the following command to Packaged into an executable jar package:

./mvnw clean package
Copy after login

Among them,

./mvnw is the packaging script used to execute Maven commands, clean package is the Maven command, use For cleaning, compiling and packaging projects.

    Build the Docker image
In the root directory of the project, create a file named

Dockerfile and copy the following content into it:

FROM openjdk:11
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Copy after login
Copy after login

    Build Docker image
In the root directory of the project, execute the following command to build the Docker image:

docker build -t my-app .
Copy after login
Copy after login

Among them,

my- app is the name of the image and can be modified according to the actual situation.

    Run the Docker container
After building the image, we can use the following command to run the Docker container:

docker run -d -p 8080:8080 my-app
Copy after login
Copy after login

Among them,

-d# The ## parameter indicates running the container in background mode, the -p parameter indicates mapping the host's 8080 port to the container's 8080 port, and my-app is the name of the image. By accessing

http://localhost:8080/hello

, we can see that the returned content is Hello, Docker!, indicating that the Spring Boot application has been successfully deployed . 3. Horizontal expansion of applications

Another advantage of Docker is that it can easily expand horizontally applications to meet high concurrency requirements.

Using Docker Compose
  1. Docker Compose is a tool for defining and running multiple Docker containers. We can use Docker Compose to manage multiple instances of an application.

First, we need to create a file named

docker-compose.yml

and copy the following content into it: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>version: '3' services: app: image: my-app ports: - &quot;8080:8080&quot; environment: - SPRING_PROFILES_ACTIVE=dev1 app2: image: my-app ports: - &quot;8081:8080&quot; environment: - SPRING_PROFILES_ACTIVE=dev2</pre><div class="contentsignin">Copy after login</div></div>The above configuration file defines two services ,

app

and app2 respectively correspond to two application instances. We can specify the application's configuration environment by setting the SPRING_PROFILES_ACTIVE environment variable.

Start the application instance
  1. In the directory where the configuration file is located, execute the following command to start the application instance:
docker-compose up -d
Copy after login

Among them,

- The d

parameter indicates running the container in background mode. By accessing

http://localhost:8080/hello

and http://localhost:8081/hello, we can see that the returned content is still Hello, Docker!, indicating that the two application instances have been started successfully. <p>By using Docker and Spring Boot, we can achieve rapid deployment and horizontal expansion of applications. Through Docker's containerization technology, we can package the application and its dependent environment into a container to achieve cross-platform deployment and transplantation. Using Spring Boot, we can quickly build the skeleton of the application and simplify development. I hope this article will help you understand and use Docker and Spring Boot. </p>

The above is the detailed content of Use Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1654
14
PHP Tutorial
1252
29
C# Tutorial
1225
24
How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop &lt;container_name&gt; Command Use docker kill &lt;container_name&gt; command in the host terminal (force exit)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] &lt;Container Path&gt; &lt;Host Path&gt;. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop &lt;container_id&gt;); start the container (docker start &lt;container_id&gt;); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

See all articles