11. Docker 微服务实战(将项目打包生成镜像,在 Docker 当中作为容器实例运行)
@
目录
- [11. Docker 微服务实战(将项目打包生成镜像,在 Docker 当中作为容器实例运行)](#11. Docker 微服务实战(将项目打包生成镜像,在 Docker 当中作为容器实例运行))
- [2. 最后:](#2. 最后:)

- 建 Module - docker_boot
2. 编辑 pom
xml<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rainbowsea</groupId> <artifactId>docker_boot</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.18</lombok.version> <mysql.version>5.1.47</mysql.version> <druid.version>1.1.16</druid.version> <mapper.version>4.1.5</mapper.version> <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version> </properties> <dependencies> <!--SpringBoot通用依赖模块--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> </plugins> </build> </project>
3. 写 yml 文件
propertiesserver.port=6001
4. 编写业务类
javapackage com.rainbowsea.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; @RestController public class OrderController { @Value("${server.port}") private String port; @RequestMapping("/order/docker") public String helloDocker() { return "hello docker"+"\t"+port+"\t"+ UUID.randomUUID().toString(); } @RequestMapping(value ="/order/index",method = RequestMethod.GET) public String index() { return "服务端口号: "+"\t"+port+"\t"+UUID.randomUUID().toString(); } }
5. 编写启动类:
javapackage com.rainbowsea.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; @RestController public class OrderController { @Value("${server.port}") private String port; @RequestMapping("/order/docker") public String helloDocker() { return "hello docker"+"\t"+port+"\t"+ UUID.randomUUID().toString(); } @RequestMapping(value ="/order/index",method = RequestMethod.GET) public String index() { return "服务端口号: "+"\t"+port+"\t"+UUID.randomUUID().toString(); } }
6. 启动程序。浏览器地址栏上访问:http://localhost:6001/order/docker
浏览器地址栏上访问:http://localhost:6001/order/index
通过 dockerfile 发布微服务部署到docker容器:

- IDEA工具里面搞定微服务
jar
包。


- 编写Dockerfile:
- 将微服务 jar 包 和Dockerfile文件上传到同 一个目录下/mydocker
这里我们使用 Xftp 工具进行上传到 虚拟机的 Linux 系统当中的 mydocker 路径

- 编写Dockerfile内容
dockerfile
# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER rainbowsea
# VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为rainbowsea_docker.jar
ADD docker_boot-0.0.1-SNAPSHOT.jar rainbowsea_docker.jar
# 运行jar包
RUN bash -c 'touch /rainbowsea_docker.jar'
ENTRYPOINT ["java","-jar","/rainbowsea_docker.jar"]
#暴露6001端口作为微服务
EXPOSE 6001
sh
[root@localhost mydocker]# vim Dockerfile
# 注意:首字母大写的D,文件名不可以修改。


- 构建镜像
sh
docker build -t rainbowsea_docker:1.6 .
# 注意:最后有一个"空格" 和 "."
打包成镜像文件:

- 运行容器:
sh
docker run -d -p 6001:6001 zzyy_docker:1.6

- 访问测试:

2. 最后:
"在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。"