Docker compose deploys SpringBoot project to connect to MySQL and the pitfalls encountered

Docker compose deploys SpringBoot project to connect to MySQL and the pitfalls encountered

Earlier, we used Docker to simply deploy the SpringBoot project and introduced what Dockerfile is.如果是鏡像比較多的情況下如何一鍵交付呢?Docker-compose! If you understand Docker-compose, it will be easier to understand K8s later.

1. Install docker-compose

Environment CentOS 7

//Download docker-compose
curl -L "https://get.daocloud.io/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

//Add executable permissions to the file, otherwise the root user does not have permission chmod +x /usr/local/bin/docker-compose

//Check whether the installation is successful [root@SJS local]# docker-compose --version
docker-compose version 1.27.3, build 4092ae5d

Common commands

#Build, create, and start related containers docker-compose up -d

#Stop all related containers docker-compose stop

#List all container information docker-compose ps

2. Deployment Steps

  1. 1. Use Dockerfile to define the application environment. This is usually only needed when you need to modify the initial image behavior.
  2. 2. Use docker-compose.yml to define the application service to be deployed so that the script can be deployed once
  3. 3. Use the docker-compose up command to deploy all application services at once

3. Deploy SpringBoot project

Overall project

insert image description here

1. Write Dockerfile

FROM java:8
# Create a container volume in the docker container for data storage and persistence VOLUME /tmp
#Used to copy files and decompress (COPY cannot decompress)
#Copy the current redpacket-backend-1.0.0-SNAPSHOT.jar to the root directory of the docker container ADD redpacket-backend-1.0.0-SNAPSHOT.jar app.jar
#Create an app.jar file during the run process RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
#Execute the Linux command with the passed parameters to start the jar package #The difference between CMD is that only the last one of multiple CMD commands will take effect, and CMD will be replaced by the parameters after docker run #The command executed by ENTRYPOINT will be appended and will not overwrite ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

2. Write the docker-compose.yml file

This document is divided into three main parts

Project: The entire yml file is a project service: The services node is the service container: The service is the container

version: '3.8'
services:
  #mysql service name mysql:
    #Image used: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    ports:
      -3306:3306
    environment:
      MYSQL_ROOT_HOST: '%'
      MYSQL_ROOT_PASSWORD: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
      MYSQL_DATABASE: 'redpacket'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: '123456'
    #File volumes that need to be mounted:
      - /mydata/mysql/data:/var/lib/mysql
      - /mydata/mysql/log:/var/log/mysql
      - /mydata/mysql/conf:/etc/mysql
    restart: always
    networks:
      -test_network
  #SpringBoot project service redpacket:
    container_name: redpacket
    #Build the Dockerfile in the specified directory
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      -mysql
    ports:
      - "8090:8090"
    restart: always
    networks:
      -test_network
networks:
  test_network:

3. Pay attention to modifying the yml file link name

insert image description here

4. Start the project with one click

Put the project's dockerfile, docker-compose.yml, and jar package in the same directory and start it using docker-compose

insert image description here

It succeeded

insert image description here

4. Pitfalls

If you modify the Dockerfile during use,一定要把之前的鏡像刪掉! Otherwise, even if the docker-compose up command is successful, it will not rebuild the previous image but just create a new container.

Reference articles:

https://mp.weixin.qq.com/s/iMl9bJ4SxUsNHBbiS5VUcw

https://docs.docker.com/compose/env-file/

https://blog.csdn.net/u011943534/article/details/81392993

This is the end of this article about Docker compose deployment of SpringBoot project to connect to MySQL. For more relevant Docker compose deployment of SpringBoot project content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of docker-compose deployment project based on MySQL8
  • How to run MySQL using docker-compose
  • Use dockercompose to build springboot-mysql-nginx application
  • A brief analysis of the problem of mysql being inaccessible when deployed with docker-compose
  • How to build an elk system using docker compose
  • Detailed process of building mongodb and mysql with docker-compose

<<:  Use CSS's clip-path property to display irregular graphics

>>:  Front-end advanced teaching you to use javascript storage function

Recommend

Detailed explanation of vuex persistence in practical application of vue

Table of contents vuex persistence Summarize vuex...

Detailed explanation of Nginx forwarding socket port configuration

Common scenarios for Nginx forwarding socket port...

MySQL 5.7.23 version installation tutorial and configuration method

It took me three hours to install MySQL myself. E...

Use of TypeScript Generics

Table of contents 1. Easy to use 2. Using generic...

Detailed explanation of keepAlive usage in Vue front-end development

Table of contents Preface keep-avlive hook functi...

Express implements login verification

This article example shares the specific code for...

JS operation object array to achieve add, delete, modify and query example code

1. Introduction Recently, I helped a friend to ma...

Detailed description of the function of meta name="" content="

1. Grammar: <meta name="name" content...

Play mp3 or flash player code on the web page

Copy code The code is as follows: <object id=&...

Four ways to compare JavaScript objects

Table of contents Preface Reference Comparison Ma...

React implements multi-component value transfer function through conetxt

The effect of this function is similar to vue的pro...

How to insert weather forecast into your website

We hope to insert the weather forecast into the w...

How to install Jenkins using Docker

Table of contents 1. Pull the image 2. Create a l...