首先启动docker 然后在idea中推送镜像到docker上
具体操作
![](https://i-blog.csdnimg.cn/blog_migrate/65066015784f2b53e88045fdeeac8753.png)
pom增加插件
打jar包然后docker build打镜像
docker images 查看是否推送成功或者去docker软件上看
首先项目的pom文件增加插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<!--<version>1.2.2</version>-->
<configuration>
<!-- Docker路径 localhost改成自己的服务器公网地址-->
<dockerHost>http://localhost:2375</dockerHost>
<!-- Dockerfile定义 -->
<baseImage>openjdk:8</baseImage>
<!-- 作者 -->
<maintainer>ysx</maintainer>
<resources>
<resource>
<!-- 复制jar包到docker容器指定目录 -->
<targetPath>/</targetPath>
<!-- 从哪个包拷贝文件,target包 -->
<directory>${project.build.directory}</directory>
<!-- 拷贝哪个文件 -->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<workdir>/</workdir>
<entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint>
<forceTags>true</forceTags>
<!-- 镜像名 -->
<imageName>${project.artifactId}</imageName>
<!-- 镜像版本 -->
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags>
</configuration>
</plugin>
</plugins>
</build>
![](https://i-blog.csdnimg.cn/blog_migrate/95a4bfdbfae2882013f564cf9538431f.png)
docker启动image 命令
先查询images 有没有推上去 这里可以看到bare 已经推了 所以直接运行
docker run -d -p 8080:8621 bare:0.0.1-SNAPSHOT
8080是docker的端口 8621是服务的端口类似代理端口
MacBook-Pro:~ duke$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bare 0.0.1-SNAPSHOT 7b663f95bfdb 2 minutes ago 562MB
bare latest 7b663f95bfdb 2 minutes ago 562MB
<none> <none> 1e7ce6bb3e7a 37 minutes ago 141MB
<none> <none> 8bd0c07630ed 2 hours ago 141MB
<none> <none> 03f0fdcff457 2 hours ago 141MB
<none> <none> fd6c7a955db3 3 hours ago 141MB
openjdk 8 b273004037cc 7 months ago 526MB
docker/getting-started latest 26d80cd96d69 15 months ago 28.5MB
openjdk 8-jdk-alpine a3562aa0b991 3 years ago 105MB
MacBook-Pro:~ duke$ docker run -d -p 8080:8621 bare:0.0.1-SNAPSHOT
978b624eef5a3213d846118561acdb9b20ec7f3a24aa771928bec98740a10953
返回了个id 说明镜像启动成功了
访问也成功
![](https://i-blog.csdnimg.cn/blog_migrate/7895b98980975b99ca50f34c2b391df0.png)