Implementation of docker compose Based on MySQL8 Deployment Project

  • 2021-10-13 09:10:45
  • OfStack

1. First, create the corresponding folder according to the following path


/usr/local/docker/mysql

2. Then create the docker-compose. yml file in this directory and add the following configuration to the file


version: '3.1'
services:
 db:
  image: mysql
  restart: always
  environment:
   MYSQL_ROOT_PASSWORD: 123456
  command:
   --default-authentication-plugin=mysql_native_password
   --character-set-server=utf8mb4
   --collation-server=utf8mb4_general_ci
   --explicit_defaults_for_timestamp=true
   --lower_case_table_names=1
   --max_allowed_packet=128M;
  ports:
   - 3306:3306
  volumes:
   - ./data:/var/lib/mysql

 adminer:
  image: adminer
  restart: always
  ports:
   - 8080:8080

3. Create the corresponding folder according to the following path


/usr/local/docker/tomcat

4. Create an docker-compose. yml under the directory of this folder and fill in the relevant configuration information (because the 8080 port of the upper host here is occupied, it can only be changed to another port here)


version: '3.1'
services:
 tomcat:
  restart: always
  image: tomcat
  container_name: tomcat
  ports:
   - 8082:8080
  volumes:
   - /usr/local/docker/tomcat:/usr/local/tomcat/webapps/ROOT
  environment:
   TZ: Asia/Shanghai

Note: If the directories created are different, the/usr/local/docker/tomcat directories above cannot be the same

5. If you can't start it, you can try it directly with the start command


docker run -p 8082:8080  Mirror image id Or mirror name 

6. Upload the project to the sibling directory of tomcat, unzip it and run it to achieve deployment

Description:

One container can deploy one project, so is it strange, if I deploy three applications, one front-end UI1, one back-end Admin and one database MySQL on the same server, then the back-end should manage the front-end data, and their configuration files docker-compose are as follows

admain Path:/usr/local/docker/tomcat


version: '3.1'
services:
 tomcat:
  restart: always
  image: tomcat
  container_name: tomcat
  ports:
   - 8082:8080
  volumes:
   - /usr/local/docker/tomcat:/usr/local/tomcat/webapps/ROOT
  environment:
   TZ: Asia/Shanghai

UI: /usr/local/docker/tomcat_ui


version: '3.1'
services:
 tomcat:
  restart: always
  image: tomcat
  container_name: tomcatui
  ports:
   - 8083:8080
  volumes:
   - /usr/local/docker/tomcat_ui:/usr/local/tomcat/webapps/ROOT
  environment:
   TZ: Asia/Shanghai~

mysql Path:/usr/local/docker/mysql

Configuration of docekr-compose


version: '3.1'
services:
 db:
  image: mysql
  restart: always
  environment:
   MYSQL_ROOT_PASSWORD: 123456
  command:
   --default-authentication-plugin=mysql_native_password
   --character-set-server=utf8mb4
   --collation-server=utf8mb4_general_ci
   --explicit_defaults_for_timestamp=true
   --lower_case_table_names=1
  ports:
   - 3306:3306
  volumes:
   - ./data:/var/lib/mysql

 adminer:
  image: adminer
  restart: always
  ports:
   - 8080:8080

How does the back-end manage the front-end data? In fact, the person will contact the project you deployed. There is a data connection configuration in the project as follows


# JDBC
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://192.168.206.128:3306/twg?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123456
# JDBC Pool
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20
# JDBC Test
jdbc.testSql=SELECT 'x' FROM DUAL

So jdbc.connectionURL=jdbc: mysql://192.168. 206.128: 3306/twg configured here? useUnicode = true & characterEncoding=utf-8 & useSSL=false is the key, in fact, it is through this ip that data management is carried out. This ip is the server ip deployed by mysql, so the deployed project connection configuration points to this ip, so that the background can obtain the data of this database and directly manage the data of the foreground. Moreover, database visualization interfaces such as Navicat and SQLyog can easily manage the data of server database with IP deployed by database, such as IP above.

If you need to stop a service, you can directly stop a service by using docker-compose down under the folder corresponding to that service and the directory at the same level of docker-compose


Related articles: