Get the App
SLTechnology News&Howtos  ›  Development  › 

Docker compose deployment SpringBoot project connects to MySQL and what are the pits encountered

Shulou Source: shulou.com Published: 2022-06-03 09:35:07 09月25日 Update

In this issue, Xiaobian will bring you about Docker compose deployment SpringBoot project connection MySQL and what pits are encountered. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

Install Docker-Compose

Environment Centos7

//Download docker-compositeurl-L "https://get.daocloud.io/docker/composite/releases/download/1.27.3/docker-composite-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose//Add executable permissions to the file, otherwise root user does not have permissions chmod +x /usr/local/bin/docker-compose//Check whether the installation is successful [root@SJS local]# docker-compose --versionondocker-compose version 1.27.3, build 4092ae5d

frequently used commands

#Build, create, start related containers docker-compose up -d#Stop all related containers docker-compose stop#List all container information docker-compose ps II. Deployment steps

1. Dockerfile is used to define the application environment, which is generally required when the initial mirroring behavior needs to be modified

2. Use docker-compose.yml to define the application service to deploy so that scripts can be deployed once

3. Deploy all application services at once using the docker-compose up command

III. Deploying SpringBoot Project

project as a whole

1. Writing Dockerfile

FROM java: 8 #Create your own container volume in docker container, For data preservation and persistence VOLUME /tmp#for copying files and decompressing (COPY cannot be decompressed)#Copy the current redpacket-backend-1.0.0-SNAPSHOT.jar to ADD redpacket-backend-1.0.0-SNAPSHOT.jar in the root directory of docker container APSHOT.jar app.jar #Create an app.jar file during run RUN sh -c 'touch/app.jar'ENV JAVA_OPTS=""#Execute linux command with passed parameters, start jar package #Different CMD is: Multiple CMD commands can only be the last one to take effect,CMD will be replaced by docker run parameters #ENTRYPOINT executed commands will not overwrite ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./ urandom -jar /app.jar" ]

2. Write docker-compose.yml file

The document is divided into three main components

Project: The whole yml file is a project

Services: Services are under the services node

Container: container under service

version: '3.8'services: #mysql service name mysql: #Mirrors used image: 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 to mount volumes: - /mydata/mysql/data:/var/lib/mysql - /mydata/mysql/log:/var/log/mysql - /mydata/mysql/conf:/etc/mysql restart: always networks: - test_network #SpringBoot project services redpacket: container_name: redpacket #Build Dockerfile in specified directory build: context: . dockerfile: Dockerfile depends_on: - mysql ports: - "8090:8090" restart: always networks: - test_networknetworks: test_network:

3. Note to modify yml file link name

4. One-click start project

Put the dockerfile, docker-compose.yml, jar package of the project into the same directory, and use docker-compose to start it.

did the trick

IV. The pit stepped on

If you modify Dockerfile in the process of using it, be sure to delete the previous image!, Otherwise, even if the docker-compose up command succeeds, it will not rebuild the previous image but simply recreate a container.

The above is the Docker compose deployment SpringBoot project shared by everyone. What are the pits encountered and MySQL? If there are similar doubts, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Tags: Container file project service command image success application one-time content parameter name that is project application permission environment directory program or Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info MySQL MariaDB Xiaomi