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

Multiple ways to calculate age by birthday in MySQL

I didn't use MySQL very often before, and I w...

Tutorial diagram of installing TomCat in Windows 10

Install TomCat on Windows This article will intro...

JavaScript navigator.userAgent obtains browser information case explanation

The browser is probably the most familiar tool fo...

Detailed explanation of the usage of setUp and reactive functions in vue3

1. When to execute setUp We all know that vue3 ca...

How to solve the problem of MySQL query character set mismatch

Find the problem I recently encountered a problem...

Data Structure - Tree (III): Multi-way Search Tree B-tree, B+ tree

Multi-way search tree Height of a complete binary...

HTML Tutorial: Unordered List

<br />Original text: http://andymao.com/andy...

Detailed explanation of galera-cluster deployment in cluster mode of MySQL

Table of contents 1: Introduction to galera-clust...

How to install a virtual machine with Windows services on Mac

1. Download the virtual machine Official download...

Detailed explanation of MySQL replication principles and practical applications

This article uses examples to illustrate the prin...

MySQL time types and modes details

Table of contents 1. MySQL time type 2. Check the...

How to Install Oracle Java 14 on Ubuntu Linux

Recently, Oracle announced the public availabilit...