如何在docker中运行mysql来运行maventesting

我有一个Maven Java项目。 我需要在Maventesting阶段运行一个mysql docker镜像来运行testing,当它完成后,我可以删除mysql docker镜像。

一个例子是使用Docker Maven插件( https://dmp.fabric8.io/ )。 这里是一个示例pom,它将启动一个MySQL容器,使用Maven Failsafe插件进行集成testing,然后停止MySQL容器。 它还会将属性mysql.jdbc.url传递给testing,以便它们具有正确的JDBC URL到运行在特定Docker主机上的MySQL容器(根据运行Docker的不同,这可能会有所不同)。

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.20.1</version> <extensions>true</extensions> <configuration> <images> <image> <alias>database</alias> <name>mysql:5.7</name> <run> <wait> <log>mysqld: ready for connections</log> <time>20000</time> </wait> <env> <MYSQL_ROOT_PASSWORD>abc123</MYSQL_ROOT_PASSWORD> <MYSQL_DATABASE>testdb</MYSQL_DATABASE> <MYSQL_USER>mysql</MYSQL_USER> <MYSQL_PASSWORD>mysql</MYSQL_PASSWORD> </env> <ports> <port>3306:3306</port> </ports> </run> </image> <image> <name>mvndemo</name> <build> <from>java:8-jre</from> <assembly> <descriptorRef>artifact</descriptorRef> </assembly> </build> </image> </images> </configuration> <executions> <execution> <id>docker:start</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> </execution> <execution> <id>docker:stop</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>2.17</version> <executions> <execution> <id>integration-test</id> <goals> <goal>integration-test</goal> </goals> </execution> <execution> <id>verify</id> <goals> <goal>verify</goal> </goals> </execution> </executions> <configuration> <systemPropertyVariables> <mysql.jdbc.url>jdbc:mysql://${docker.host.address}/testdb</mysql.jdbc.url> </systemPropertyVariables> </configuration> </plugin> </plugins> </build> </project>