准备
VMware Workstation Pro 15安装Cent OS 8.2
环境
环境 | 版本 |
---|---|
Idea | 2020.1 |
Docker | 20.10.2 |
Cent OS | 8 |
VMware Workstation Pro | 15 |
pom
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dev</groupId>
<artifactId>docker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>docker</name>
<description>docker project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!--打包跳过测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
dockerFile
在项目目录下新建dockerFile文件,将下列内容复制进去
# 基础镜像,若不存在自动拉取
FROM openjdk:11
# 设置时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
# 容器数据卷
VOLUME /tmp
# 复制jar包
COPY target/*.jar springboot-docker.jar
# 映射端口
EXPOSE 8080
# 容器启动时要运行的命令
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/springboot-docker.jar"]
请求
启动类增加
@SpringBootApplication
@RestController
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
@GetMapping("hello")
public String get(){
return "hello docker!";
}
}
构建映像
name 映像名称,image tag 映像标签,container name 容器名称,context folder 为当前项目
bind ports 新增映射端口,host是容器内部端口,container port为映射外部端口
working directory 选择当前项目目录,command line 为 package
打开浏览器,输入docker虚拟机ip+映射端口
启动类新增请求
新增完点击运行docker image
浏览器请求新增的time接口
github:传送门