docker-compose configuration spring environment

Recently, packaged projects need to be tested by members, but once packaged, development is affected, so you plan to use docker to simulate a production environment to run packaged projects so that you can develop and test members as well.
Because the original project is too large, we intend to take the large experiment of software engineering to try the water first. The big experiment of software engineering uses the environment of spring-boot,redis,mysql,angular,nginx. First, we try to build docker of spring-boot,redis,mysql in the background.

docker-compose

The compose project is the official open source project for Docker, responsible for the rapid organization of Docker container clusters.Compose positioning is the application of defining and running multiple docker containers.

We all know that you can create an application container with a single Dockerfile template file, but often in a project, you need multiple application containers, like my backstage, along with jdk8, redis, mysql applications.The docker-compose just meets this need.It allows users to define a set of associated application containers as a project through a single docker-compose.yml template file.

Two important concepts in compose are:

  • Service: An application container that can actually include several container instances running the same mirror.
  • Project: A complete business unit consisting of a set of associated application containers, defined in the docker-compose.yml file.

Services are the docker containers we need, and projects are made up of many services. Once you know docker-compose, you can set up your environment.

Build spring-boot

To compile spring-boot, based on the following process:

  • Based on jdk8 mirror
  • Copy mvnw, pom.xml under spring-boot into container to import maven dependencies
  • Copy the source code from the src directory into the container and package the spring-boot project with maven
  • Run the project using the jre8 environment

Build Dockerfile file:

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

# Set project working directory in docker container
WORKDIR /app

# Copy maven executable to container
COPY mvnw .
COPY .mvn .mvn

# Copy pom.xml file
COPY pom.xml .

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

# Copy Project Source Code
COPY src src

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

#### Set the minimum docker container to 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 service

Once you have built the container for spring-boot, you can use docker-compose to integrate the spring-boot, redis, and mysql containers.Specific ideas:

  • Declare items
  • Declare that the project requires integrated services

Doker-compose.yml file:

version: '3.7'

#  Define Services
services:
  # spring-boot service
  app-server:
    build:
      context: . # Configuration requires the path of the Dockerfile to be built relative to docker-compose.yml
      dockerfile: Dockerfile
    ports:
      - "8080:8080" # Map native 8080 port to container 8080 port
    restart: always
    depends_on: 
      - db # Build first with dependent services
      - redis
    environment: # Setting 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:  

Build a project container using docker-compose build:

Start the container with docker-compose up:

When you see the log of successful spring-boot startup, you are configuring it successfully.

Follow-up

This time, only the background is built. I hope that the angular and nginx of the foreground are also built, and strive for a docker command to start the application directly.

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

Tags: Linux Docker Spring Redis MySQL

Posted on Fri, 08 Nov 2019 22:13:45 -0500 by SetToLoki