In this paper, the target

Docker-compose, spring Boot application for a cluster (2 dockers, multiple similar, as long as the docker-compose. Yml plus boot application service can be published process)Copy the code

architecture

Spring Boot uses Nginx as the reverse proxy, Redis as the cache, mysql as the database, all docker-basedCopy the code

Main configuration file description

application-compose.yml


docker-compose.yml


Start the SpringBoot project to see what the interface looks like



Generate jar package


Copy the jar package


Generate a Docker image


or


1 discount ️ Enter the project directory

2 ⃣ ️ CD/Users/mengfanxiao/Documents/project/person/springboot/docker - compose/compose - demo

3 docker build. -t comement-Demo :1.0

Copy the code


Start the docker compose

docker-compose up -d

D indicates that log output at runtime is not displayed

Use docker-compose logs to view

Copy the code


As you can see, redis, mysql, Spring Boot, nginx containers are all startedCopy the code

Initialize the mysql database

  • Check the mysql docker ID
docker ps
Copy the code

  • Copy the compose. SQL file to the docker /opt directory
docker cp compose.sql 01bd894f9ebd:/opt        
Copy the code
  • Enter the mysql docker container
docker exec -it 01bd894f9ebd /bin/bash
Copy the code
  • Docker /opt/compose. SQL to initialize the database

    1 ️ mysql -u root -P

    Enter the password of user root as set in docker-comemage. ymlCopy the code

    2 ⃣ ️ source/opt/compose. The SQL


Exception handling

  • Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication required.
Visit curl 127.0.0.1/redis for the exception reported

Solution The redis access password is not configured

Copy the code


  • Failed to obtain JDBC Connection
Visit http://127.0.0.1/commands on the surface of the paper

Solution mysql Docker port mapping

Copy the code


The process to comb

1. Open the JAR package

2. Generate dokcer image from jar package

Docker compose: mysql, Redis, 2 Springboot, nginx, etc

4, Access nginx interface route forward to Springboot and then access Redis and mysql

Copy the code

The interface test



Analysis of test results

  • Since the nginx port is 80, no port number is used to access it
  • The IP address returned by /redis is 172.18.0.4 and 172.18.0.5, respectively.

extension

  • Restart docker-compose Down

  • Where does db_data map to in docker-comemage. yml

docker volume ls
Copy the code


1 One ️ The red box in the figure is the location of actual DB_data.

2 discount ️ Here actually stores mysql data, even if the mysql container (docker-compose Down, or Docker RM XXX) is deleted,

3 discount ️ When the next container is established, the data is still in and the root password will not change.

4 discount if the password in Docker-compose changes, the volume db_data should be deleted, and docker-compose should rebuild the volume and re-initialize the database.

Docker volume rm comement-demo_db_data docker volume rm comement-demo_db_dataCopy the code

Copy the code

Code resources

https://gitee.com/pingfanrenbiji/compose-demo.git
Copy the code

reference

https://www.cnblogs.com/bear129/p/12523573.html
Copy the code

This article is formatted using MDNICE