premise
You have to install docker, if you haven't installed it, you can read this article
write dockerfile
This location is best to be the same as mine, otherwise there may be problems with packaging into a mirror later (the jar package cannot be found)
FROM openjdk:8-jdk-slim
MAINTAINER JacksonNing
COPY /target/iec104-1.0.0-SNAPSHOT.jar /iec104.jar
EXPOSE 8082
ENTRYPOINT ["java","-jar","/iec104.jar"]
application.yml
server:
port: 8082
idea packages the project into a jar
First clean and then package
Note that the program can only have one main method, otherwise the package will report an error
The following represents successful packaging
Use docker to package into a mirror
1. Run the terminal in the directory of the dockerfile
docker build -t iec104:v1.0 .
2. Check if there is a mirror image
docker images
3. Run the container
docker run -itd -p 8081:8081 --name iec104 iec104:v1.0
Or recommend this, because my project wants the container to be able to listen to the port of the host, I can use the following command
be268578aa6c is the image id
docker run --net=host -d be268578aa6c
export image
1. Package the running docker container into an image image
docker commit 4f2a09831d74 iec104:v1.0
docker commit 0a3b26324050[容器ID] mysql:1.0 [名称:版本号]
2. Save the newly packaged image as a tar file
docker save iec104:v1.0 -o /data/home/nsx/Documents/iec104.tar
docker save 镜像名:版本号 -o /路径/保存的包名.tar
3. Copy the packaged image to the new machine, and execute the load command to decompress it
docker load < iec104.tar #执行命令
Common commands
docker tag 95bfbad9e10c iec104:v1.0 #打标签
docker exec -it 669fd6e56323 bash #进入容器
docker logs 52e57ee5e7c6 #打印容器的日志
docker stop 52e57ee5e7c6 #暂停容器
docker rm -f 52e57ee5e7c6 #删除容器
docker build -t iec104:v1.0 . #打包镜像
docker run -itd -p 8082:8082 --name iec104 iec104:v1.0 #启动镜像
docker commit 4f2a09831d74 iec104:v1.0 #将正在运行的docker容器打包成image镜像
docker save iec104:v1.0 -o /data/home/nsx/Documents/iec104.tar #把打包好的镜像保存成 tar 文件
docker run --net=host -d be268578aa6c #使用host网络模式启动,解决docker容器内无法访问宿主机的资源的问题
Reference:
https://blog.csdn.net/qq_28880087/article/details/114575736?spm=1001.2014.3001.5506
https://blog.csdn.net/springhub/article/details/127482319?spm=1001.2014.3001.5506