目录
1.2、修改application.properties配置文件
一、创建一个springboot项目
1.1、修改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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cjc</groupId> <artifactId>docker01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>docker01</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <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> </plugins> </build> </project>
1.2、修改application.properties配置文件
server.port=6001
1.3、创建controller层和类
HelloDocker类的内容为:
@RestController public class HelloController { //获取配置文件的参数 @Value("${server.port}") private String port; @RequestMapping("/hello/docker") public String helloDocker(){ return "hello docker"+"\t"+port+"\t"+ UUID.randomUUID().toString(); } @RequestMapping(value = "/hello/index",method = RequestMethod.GET) public String index(){ return "服务端口:"+"\t"+port+"\t"+UUID.randomUUID().toString(); } }
1.4、测试访问
1.5、将项目打包并上传到Linux
二、发布微服务到docker容器
2.1、编写Dockerfile文件
Dockerfile的文件内容:
#基础镜像使用Java8 FROM java:8 #作者 MAINTAINER cjc #VOLUME 指定临时文件目录为/tmp ,在主机/var/lib/docker目录下创建一个临时文件并链接到容器的/tmp VOLUME /tmp #将jar包添加到容器中并更名为cjc_docker.jar ADD docker01-0.0.1-SNAPSHOT.jar cjc_docker.jar #运行jar包 RUN bash -c 'touch /cjc_docker.jar' ENTRYPOINT ["java","jar","/cjc_docker.jar"] #暴露6001端口作为微服务 EXPOSE 6001
2.2、构建镜像
docker build -t cjc_docker:1.6 .
2.3、运行构建的镜像容器
docker run -d -p 6001:6001 ba117b253063
2.4、测试访问
原文链接:https://blog.csdn.net/qq_46093575/article/details/124897848?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171836884016800182130287%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=171836884016800182130287&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-10-124897848-null-null.nonecase&utm_term=docker%E9%83%A8%E7%BD%B2
© 版权声明
声明📢本站内容均来自互联网,归原创作者所有,如有侵权必删除。
本站文章皆由CC-4.0协议发布,如无来源则为原创,转载请注明出处。
THE END