参考:
https://blog.csdn.net/begin1013/article/details/80860224
https://blog.csdn.net/qq_34997906/article/details/83094767
FROM java:8 #基础镜像
MAINTAINER JY # 作者签名
ADD demo-0.0.1-SNAPSHOT.jar app.jar #将编译生成的jar添加到镜像中去
RUN bash -c "touch /app.jar" #以下两句是运行jar包
CMD ["java","-jar","app.jar"]
EXPOSE 8080 #指定容器需要映射到主机的端口
上面就是Dockerfile文件的内容。
然后使用sudo docker build -t="imagesName" . #引号中填写的是要生存的镜像名称
来构建镜像,注意最后的点号,构建过程不出错就可以通过sudo docker images 看到新镜像了
然后启动镜像:
sudo docker run --net=host --name=XXX -d -p 8080:8080 springboot:latest
此处注意一定要使用host网络连接方式,否则会连不上mysql数据库,我刚开始没有设置网络连接方式,报的错是:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
就是类似上面的错误。
最终可以进行测试看是否运行正常