Docker-compose steps to configure the spring environment

Docker-compose steps to configure the spring environment

Recently, I need to package the project for members to test, but the packaged operation will affect the development. So I plan to use docker to simulate the production environment to run the packaged project, so that I can develop and test it for members at the same time.

Since the original project was too large, I planned to try it out with a large software engineering experiment. The large software engineering experiment used spring-boot,redis,mysql,angular,nginx environments. I first tried docker build of spring-boot,redis,mysql in the background.

docker-compose

The compose project is an official open source project of Docker, responsible for realizing the rapid orchestration of Docker container clusters. compose is an application that defines and runs multiple Docker containers.

We all know that an application container can be created using a Dockerfile template file, but usually in a project, multiple application containers are required, just like my backend, which requires applications such as jdk8 , redis , and mysql at the same time. docker-compose just meets this need. It allows users to define a group of related application containers as a project through a single docker-compose.yml template file.

Two important concepts in compose :

Service: A container for an application, which can actually include several container instances running the same image.
Project: A complete business unit consisting of a set of related application containers, defined in the docker-compose.yml file.

The service is docker container we need, and the project is composed of many services. After understanding docker-compose , you can build the environment.

Build spring-boot

To compile spring-boot , follow the following process:

  • Based on jdk8 image
  • Copy the mvnw and pom.xml under spring-boot to the container and import the Maven dependency
  • Copy the source code in the src directory to the container and use Maven to package the spring-boot project
  • Run the project using jre8 environment

Build the Dockerfile :

#### Build spring-boot project FROM openjdk:8-jdk-alpine as build

# Set the project working directory WORKDIR /app in the docker container

# Copy the Maven executable program into the container COPY mvnw .
COPY .mvn .mvn

# Copy pom.xml file COPY pom.xml .

# Import all Maven dependencies RUN ./mvnw dependency:go-offline -B

# Copy the project source code COPY src src

# Package the application RUN ./mvnw package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

#### Set the minimum docker container that can run the application FROM openjdk:8-jre-alpine

ARG DEPENDENCY=/app/target/dependency

# Copy project dependencies from the build stage
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app

ENTRYPOINT ["java","-cp","app:app/lib/*","com.xiang.airTicket.AirTicketApplication"]

Docker-compose integrated project services

After completing the spring-boot container construction, you can use docker-compose to integrate spring-boot、redis、mysql containers. Specific ideas:

  • Declaration Item
  • Declare the services that the project needs to integrate

docker-compose.yml file:

version: '3.7'

# Define services:
 # spring-boot service app-server:
  build:
   context: . #Configure the path to build the Dockerfile relative to docker-compose.yml
   dockerfile: Dockerfile
  ports:
   - "8080:8080" # Map local port 8080 to container port 8080 restart: always
  depends_on: 
   -db # The dependent services need to be built first - redis
  environment: #Set environment variables SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/airTicket?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
   SPRING_DATASOURCE_USERNAME: root
   SPRING_DATASOURCE_PASSWORD: 123456
   SPRING_REDIS.HOST: redis
  networks: # Network connection mysql and redis
   - backend
 db:
  image:mysql:5.6
  ports:
   - "3306:3306"
  restart: always
  environment:
   MYSQL_DATABASE: airTicket
   MYSQL_USER: htx
   MYSQL_PASSWORD: 123456
   MYSQL_ROOT_PASSWORD: 123456
  volumes:
   -db-data:/var/lib/mysql
  networks:
   - backend
 redis:
  image: redis
  command: [ "redis-server", "--protected-mode", "no" ]
  hostname:
   redis
  ports:
   - "6379:6379"
  networks:
   - backend   
volumes:
 db-data:
networks:
 backend:

Use docker-compose build to build the project container:


Start the container using docker-compose up :


When you see the log of successful spring-boot startup, it is successfully configured.

Follow-up

This time, I only built the backend. I hope to build the frontend angular、nginx as well, and try to start the application directly with a docker command.

Reference link: Spring Boot, Mysql, React docker compose example

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker compose configuration file writing and command usage examples
  • Using Docker Compose to build and deploy ElasticSearch configuration process
  • Docker-compose installation yml file configuration method
  • Detailed explanation of the process of deploying the distributed configuration center Apollo with one click using docker compose
  • Detailed tutorial on docker-compose deployment and configuration of Jenkins
  • How to install and configure the Docker Compose orchestration tool in Docker.v19
  • Detailed explanation of software configuration using docker-compose in linux
  • Detailed explanation of Docker Compose configuration file parameters

<<:  Vue uses three methods to refresh the page

>>:  Causes and solutions for MySQL master-slave synchronization delay

Recommend

Vue implements the magnifying glass function of the product details page

This article shares the specific code of Vue to i...

WeChat applet implements waterfall flow paging scrolling loading

This article shares the specific code for WeChat ...

What to do if the online MySQL auto-increment ID is exhausted

Table of contents Table definition auto-increment...

HTML table tag tutorial (46): table footer tag

The <tfoot> tag is used to define the style...

How to improve Idea startup speed and solve Tomcat log garbled characters

Table of contents Preface Idea startup speed Tomc...

Springboot+Vue-Cropper realizes the effect of avatar cutting and uploading

Use the Vue-Cropper component to upload avatars. ...

Datagrip2020 fails to download MySQL driver

If you cannot download it by clicking downloadlao...

Detailed explanation of JavaScript stack and copy

Table of contents 1. Definition of stack 2. JS st...

XHTML Basic 1.1, a mobile web markup language recommended by W3C

W3C recently released two standards, namely "...

5 common scenarios and examples of JavaScript destructuring assignment

Table of contents Preface 1. Extract data 2. Alia...

Detailed explanation of the use of the <meta> tag in HTML

In the web pages we make, if we want more people ...

Detailed explanation of JavaScript closure issues

Closures are one of the traditional features of p...