利用Dockerfile文件和docker 命令 生成docker镜像
一、sprinboot部分
1、springboottest 的springboot项目,写了controller测试
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /* * *@Description TODO *@Author chenzhian *@Date 2022/9/21 16:55 */ @RestController() public class indexController { @Value("${spring.profiles.active:dev}") private String active; @Value("${smx:dev}") private String smx; @RequestMapping(method = RequestMethod.GET,value = "/getdate") public String getdate() { return "active is "+active+" ,"+ new Date().toString(); } }
2、SpringBootApplication启动类要注释不检查数据库
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.List; /* * *@Description 启动类 *@Author chenzhian *@Date 2021/11/11 17:36 */ @SpringBootApplication(scanBasePackages = {"com.zhianchen.mysqlremark.springboottest"},exclude = DataSourceAutoConfiguration.class) // @Slf4j public class SpringbootTestApplication { public static void main(String[] args) { SpringApplication.run(SpringbootTestApplication.class,args); } }
3、pom.xml
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> <spring-boot.version>2.3.9.RELEASE</spring-boot.version> <mybatis.version>1.3.2</mybatis.version> <mysql.version>8.0.18</mysql.version> <lombok.version>1.16.20</lombok.version> <fastjson.version>1.2.62</fastjson.version> <commons-lang3.version>3.9</commons-lang3.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <!-- <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!–fastjson–> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency>--> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.1</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.11.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <!-- mybatis代码生成插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
4、application.yml
server: port: 9021 spring: application: name: com-zhianchen-mysqlremark-springboottest
5、maven 打包成jar
二、docker
1、Docker文件内容
FROM docker.io/ascdc/jdk8 MAINTAINER docker_maven docker_maven@email.com WORKDIR /ROOT ADD com-zhianchen-mysqlremark-springboottest.jar /ROOT/ ["java", "-jar", "-Xms128m", "-Xmx256m", "-XX:+PrintGCDetails", "-XX:+PrintGCTimeStamps", "-XX:+PrintGCTimeStamps", "-XX:+HeapDumpOnOutOfMemoryError", "-XX:HeapDumpPath=/opt/infosec/cssp","-Dspring.profiles.active=test", "com-zhianchen-mysqlremark-springboottest.jar"] CMD ["java", "-version"]
然后把Dockerfile文件和 jar都放在同一个目录,执行
docker build -f dockerfile文件路径 -t 镜像名称:版本号 .
例如 docker build -f Dockerfile -t springboottest2:1.0.0 .
docker images 查看镜像
执行启动,因为我这里指定了版本,所以启动的时候要指定版本,不然会去远程拉last
]# docker run -d -m 512m -p 9022:9021 springboottest2:1.0.0
因为是我上面写了-d,进行异步执行,所以要看下查看是否运行成功
]# docker ps
网页查看效果