MySql Docker 容器的数据在 docker-compose up 后不会保留

Data of MySql Docker Container doesn't persist after docker-compose up

我是运行两个docker容器:

而且由于数据量大,我确保数据的持久性。

那是我的 docker-compose.yml

version: '3'
services:

  docker-mysql-activity:
    image: mysql:latest
    container_name: docker-mysql-activity
    volumes:
    - mysql_activity:/var/lib/mysql
    ports:
      - 33061:3306
    environment:
        - MYSQL_ROOT_PASSWORD=admin123
        - MYSQL_DATABASE=SCactivity
        - MYSQL_USER=testuser
        - MYSQL_PASSWORD=testuser@123

  activity-service:
    image: activity-service:latest
    container_name: activity-service
    environment:
      - SPRING_APPLICATION_NAME=activity-service
    hostname: activity-service
    build:
      context: ./activity-service
      dockerfile: Dockerfile
    ports:
      - 9001:9001
    depends_on:
      - service-registry
      - docker-mysql-activity

volumes:
  mysql_activity:

当我执行 docke-compose up 一切顺利并创建卷时,我已验证查看 Docker 的卷文件夹并且它正确保存数据(所有已通过邮递员验证 - GET 和 POST 数据正常工作)。

当我尝试 docker-compose downdocker-compose stop 然后docker-重新组合,数据不再持久化。

我确定问题与 docker-compose down 无关,因为卷仍然存在,而是当我重置时卷内的数据执行命令docker-compose up.

编辑

activity-service 是一个 Spring 引导微服务。

它有这个 application.yml:

server:
  port: 9001

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://docker-mysql-activity:3306/SCactivity?createDatabaseIfNotExist=true
    username: testuser
    password: testuser@123
    hikari:
      initialization-fail-timeout: 0
  jpa:
    database-platform: org.hibernate.dialect.MySQL5Dialect
    generate-ddl: true
    show-sql: true
    hibernate:
      ddl-auto: create

table Class 是 Activity.java:

@Entity
@Table(name = "Activity")
@Getter
@Setter
public class Activity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ACT_ID")
    private long id;
    @Column(name = "NAME")
    private final String name;
    @Column(name = "DESCR")
    private final String descr;
    @Column(name = "fitness")
    private Boolean fitness;
    @Column(name = "nuoto")
    private Boolean nuoto;


    public Activity() {
        this.name = "";
        this.descr = "";
    }
}

已解决 问题出在这里:

server:
  port: 9001

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://docker-mysql-activity:3306/SCactivity?createDatabaseIfNotExist=true
    username: testuser
    password: testuser@123
    hikari:
      initialization-fail-timeout: 0
  jpa:
    database-platform: org.hibernate.dialect.MySQL5Dialect
    generate-ddl: true
    show-sql: true
    hibernate:
      ddl-auto: create

替换

ddl-auto: create 

ddl-auto:update