The local docker develops the springboot project and the local update code is updated in real time in the docker container

This is a simple tutorial to help you use Docker locally for Spring Boot project development. The actual development process may vary depending on the complexity and needs of the project

The local docker develops the springboot project and updates the code locally in the docker container in real time

To implement real-time updates in the Docker container after the local code is updated, you can mount the local code directory to the Docker container and use Spring Boot's hot deployment function. This way, when you modify the code locally, the application inside the Docker container will automatically reload the latest code and reflect the changes in real time. The following are the specific steps:

  1. Writing a Dockerfile: Create a Dockerfile for building an image containing a Spring Boot application. Make sure to include dependencies needed for debugging and hot deployment, such as JVM arguments and DevTools plugins, in your Dockerfile. Here is an example Dockerfile:

Dockerfile

# 基于Java 11的官方Spring Boot镜像
FROM adoptopenjdk:11-jre-hotspot

# 设置工作目录
WORKDIR /app

# 复制应用程序的JAR文件到镜像中
COPY target/your-application.jar app.jar

# 设置环境变量,启用Spring Boot热部署
ENV SPRING_PROFILES_ACTIVE=dev
ENV SPRING_DEVTOOLS_REMOTE_SECRET=mysecret

# 设置启动命令
CMD ["java", "-jar", "-Dspring.devtools.remote.secret=${SPRING_DEVTOOLS_REMOTE_SECRET}", "app.jar"]

Note that in this Dockerfile we set up SPRING_PROFILES_ACTIVE=devto enable the development environment and use SPRING_DEVTOOLS_REMOTE_SECRETa remote key with the DevTools plugin specified.

  1. Create docker-compose.yml: Create a docker-compose.yml file to define the service and container configuration. Here, we mount the code directory into a Docker container for live updates. Here is an example docker-compose.yml:

yaml

version: '3'
services:
  spring-app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    volumes:
      - ./src:/app/src
      - ./target:/app/target

In this docker-compose.yml, we ./srcmount the directory to /app/srcthe directory of the container so that the code can be synchronized in real time. Likewise, we also ./targetmount the directory to the container's /app/targetdirectory to keep the app's build results in sync.

  1. Build and start the container: Build and start the Docker container with the following commands:

bash

docker-compose up --build

Your Spring Boot application should now be running in a Docker container, with code changes reflected in the container in real time. You can edit the code locally, and after saving, the application inside the Docker container will automatically reload the latest code.

Note that there may be some performance overhead when using the hot deployment feature, so in a production environment, hot deployment should be disabled and the application deployed using a precompiled JAR package. But in the development phase, hot deployment is very useful, which can speed up the development iteration and debugging process.

Guess you like

Origin blog.csdn.net/qq_40963664/article/details/131893467