Deploy Springboot+Mysql project with Docker Compose

Deploy Springboot+Mysql project with Docker Compose

In the last article Spring Boot(Maven)+Docker packaging In, we implemented one click packaging and deployment of the Springboot project source code as a Docker service.

In the above, if our Springboot project wants to connect to the database, it must be connected to the public IP. This paper will deploy Springboot+Mysql with one click and use it out of the box.

The application scenario of this article is: stand-alone deployment of web services.

premise

The Spring Boot project can be packaged into a jar file using Maven, and this file can be started through java -jar example-0.0.1-SNAPSHOT.jar.

Docker Compose must be installed.

operation

Step 1: create a Dockerfile in the root directory of the project. The contents are as follows. The project in this paper uses jdk11 and opens port 8086. Note: you need to change the following two examples-0.0.1-snapshot to the name of your project.

FROM maven:3.8.3-openjdk-11 AS MAVEN_BUILD
COPY settings.xml /usr/share/maven/conf/settings.xml
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn package

FROM maslick/minimalka:jdk11
COPY --from=MAVEN_BUILD /build/target/example-0.0.1-SNAPSHOT.jar /app/
RUN mkdir -p "/app/static/images/upload"
EXPOSE 8086
ENTRYPOINT ["java","-jar","/app/example-0.0.1-SNAPSHOT.jar"]

You will notice that Dockerfile is only different from the previous article, that is, there is an additional line of RUN mkdir -p "/app/static/images/upload". This is because my back-end project receives and stores some files uploaded by users, and I use Volume to persist the file content.

Step 2: create the settings.xml file, replace the Maven official source, and use the domestic source. The content of the file is the same as that in the previous article.

Step 3: create the file docker-compose.yml in the project root directory, as follows:

version: '3'
services:
  web:
    build: .
    container_name: web-server
    restart: always
    volumes:
      - imagedata:/app/static/images/upload
    ports:
      - "8086:8086"
    depends_on:
      - mysql
  mysql:
    image: mysql:8.0.26
    container_name: mysql
    restart: always
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      MYSQL_DATABASE: webserver #Create an initial empty database called webserver
      MYSQL_ROOT_USER: root #root user
      MYSQL_ROOT_PASSWORD: QWEasd123 #root password
      MYSQL_ROOT_HOST: '%' 
      TZ: Asia/Shanghai #time zone
volumes:
  dbdata:
  imagedata:

The explanation of docker-compose.yml is as follows:

  • web and mysql services are created and their container names are declared respectively.
  • dbdata and imagedata volumes are created to store database data and files uploaded by users.
  • Map the imagedata volume to the / app/static/images/upload directory of the web service; The dbdata volume is mapped to the / var/lib/mysql directory of mysql service, which is the default data directory of mysql.
  • The 8086 port of the web service is mapped to the 8086 port of the host.

For services declared in the same docker-compose.yml, they can communicate with each other through the domain name without creating a new network, so we have not created a new network.

Step 4: modify the data source in the Springboot configuration file as follows:

spring.datasource.url=jdbc:mysql://mysql:3306/webserver?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=QWEasd123

Step 5: deploy and run.

Under the project root directory, execute the following command:,

docker-compose up

If you want to execute the service in the background, you can add the - d parameter:

docker-compose up -d

Wait for the project to build and run!

last

After the project is built and run, through docker images, we will find that in addition to the project image we just built, there are two additional images maven:3.8.3-openjdk-11 and minimalka:jdk11, which are the basic images that must be used when building our own image. If we need to build the project again in the future, we can keep these two images, Otherwise, you can delete it.

Keywords: MySQL Docker Maven

Added by derekm on Sat, 16 Oct 2021 12:11:03 +0300