微服务部署方式:
docker.service
文件的ExecStart=
后添加-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
vi /lib/systemd/system/docker.service
systemctl daemon-reload sudo systemctl restart docker docker start registry
pox.xml
文件中添加以下内容<build> <finalName>dockerDemo</finalName> <plugins> <!-- springboot maven插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- docker 插件 --> <plugin> <!-- 版本信息 --> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <!-- 配置信息 --> <configuration> <!-- docker 生成的镜像名称 --> <imageName>192.168.2.11:5000/${project.artifactId}:${project.version}</imageName> <!-- 基础镜像 == Dockerfile中的 FROM jdk1.8 --> <baseImage>jdk1.8</baseImage> <!-- 入口点 --> <entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <dockerHost>http://192.168.2.11:2375</dockerHost> </configuration> </plugin> </plugins> </build>
以上配置会自动生成 Dockerfile
文件
FROM jdk1.8 ADD dockerDemo.jar / ENTRIPOINT ["java","-jar","/dockerDemo.jar"]
mvn clean package docker:build -DpushImage
示例:
[INFO] Building image 192.168.2.11:5000/springboot-demo:0.0.1-SNAPSHOT Step 1/3 : FROM jdk1.8 ---> 79f8c221d4f6 Step 2/3 : ADD /dockerDemo.jar // ---> d0a85948e94f Step 3/3 : ENTRYPOINT ["java","-jar","/dockerDemo.jar"] ---> Running in ded9a19061e8 Removing intermediate container ded9a19061e8 ---> 61d57ebbc410 ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null} Successfully built 61d57ebbc410 Successfully tagged 192.168.2.11:5000/springboot-demo:0.0.1-SNAPSHOT [INFO] Built 192.168.2.11:5000/springboot-demo:0.0.1-SNAPSHOT [INFO] Pushing 192.168.2.11:5000/springboot-demo:0.0.1-SNAPSHOT The push refers to repository [192.168.2.11:5000/springboot-demo] 9f8f970adb5a: Pushed 5739a69f0bb7: Mounted from jdk1.8 f967d613a659: Mounted from jdk1.8 174f56854903: Mounted from jdk1.8 0.0.1-SNAPSHOT: digest: sha256:39f341edef265ddfbfab7227c63ffa12e41862e5601198f7a530cbc29061ab79 size: 1161 null: null [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 37.979 s [INFO] Finished at: 2021-11-01T23:16:33+08:00 [INFO] --------------------------------------------------------
docker images
docker run -di --name=dockerDemo -p 88:8080 192.168.2.11:5000/springboot-demo:0.0.1-SNAPSHOT
注:这里编写了一个SpringBoot的程序。
@RestController public class HiController { @RequestMapping("/hello") public String k8s(){ return "<h1>HELLO</h1> <h1>HUATHY</h1>"; } }
// TODO 未完待续