一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

云服務器|WEB服務器|FTP服務器|郵件服務器|虛擬主機|服務器安全|DNS服務器|服務器知識|Nginx|IIS|Tomcat|

服務器之家 - 服務器技術 - 服務器知識 - 使用Docker部署Spring Boot項目的實現步驟

使用Docker部署Spring Boot項目的實現步驟

2022-01-23 23:20初念初戀 服務器知識

本文主要介紹了使用Docker部署Spring Boot項目的實現步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

創建一個簡單的springboot項目

一、在 pom.xml 中 ,使用 Spring Boot 2.2.10 相關依賴

?
1
2
3
4
5
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.10.RELEASE</version>
</parent>

二、添加 web 和測試依賴

?
1
2
3
4
5
6
7
8
9
10
11
<dependencies>
     <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
</dependencies>

三、創建一個 DockerController,在其中有一個hello()方法,訪問時返回:hello,nihao

?
1
2
3
4
5
6
7
8
@RestController
public class DockerController {
 
    @RequestMapping("/hello")
    public String hello() {
        return "hello,nihao";
    }
}

四、啟動類

?
1
2
3
4
5
6
7
@SpringBootApplication
public class DockerApplication {
 
 public static void main(String[] args) {
  SpringApplication.run(DockerApplication.class, args);
 }
}

添加完畢后啟動項目,啟動成功后瀏覽器訪問:http://localhost:8080/hello,頁面返回:hello,nihao,說明 Spring Boot 項目配置正常。

使用 Docker 部署 Spring Boot 項目

一、將項目打成jar包,拷貝到服務器上,測試一下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@jiangwang springbootDemo]# ls
demo-0.0.1-SNAPSHOT.jar  Dockerfile
[root@jiangwang springbootDemo]# java -jar demo-0.0.1-SNAPSHOT.jar
 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.2.10.RELEASE)
 
2021-03-18 14:49:18.241  INFO 12886 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT on jiangwang with PID 12886 (/home/springbootDemo/demo-0.0.1-SNAPSHOT.jar started by root in /home/springbootDemo)
2021-03-18 14:49:18.244  INFO 12886 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2021-03-18 14:49:19.924  INFO 12886 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-18 14:49:19.938  INFO 12886 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-18 14:49:19.938  INFO 12886 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2021-03-18 14:49:20.013  INFO 12886 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-18 14:49:20.014  INFO 12886 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1657 ms
2021-03-18 14:49:20.321  INFO 12886 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-18 14:49:20.520  INFO 12886 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-18 14:49:20.523  INFO 12886 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.899 seconds (JVM running for 3.369)

二、看到 Spring Boot 的啟動日志后表明環境配置沒有問題,編輯Dockerfile文件:

?
1
2
3
4
5
6
7
8
FROM java:8
COPY *.jar /app.jar
 
CMD ["--server.port=8080"]
 
EXPOSE 8080
 
ENTRYPOINT ["java","-jar","/app.jar"]

三、接下來我們使用 Dockerfile 構建鏡像:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
## 構建鏡像
[root@jiangwang springbootDemo]# docker build -t springboot-demo .
Sending build context to Docker daemon  17.72MB
Step 1/5 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
 ---> f4d6aeabd3f0
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in a6311f7cf7b5
Removing intermediate container a6311f7cf7b5
 ---> d8117b10cefa
Step 4/5 : EXPOSE 8080
 ---> Running in ae180be637bb
Removing intermediate container ae180be637bb
 ---> f16702c75ab6
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in fafa00625666
Removing intermediate container fafa00625666
 ---> d4c3e225699d
Successfully built d4c3e225699d
Successfully tagged springboot-demo:latest

四、運行鏡像:

?
1
2
3
4
5
6
7
8
# 運行鏡像
[root@jiangwang springbootDemo]# docker run -d -p 39005:8080 --name my-springboot springboot-demo
7ac35852cb91cb10612cd28fdbe7c50c7c59df4cccf19b2f1d30dcabbfe501f4
[root@jiangwang springbootDemo]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                     NAMES
7ac35852cb91   springboot-demo       "java -jar /app.jar …"   33 seconds ago   Up 32 seconds   0.0.0.0:39005->8080/tcp   my-springboot
[root@jiangwang springbootDemo]# curl localhost:39005/hello
hello,nihao[root@jiangwang springbootDemo]#

五、瀏覽器輸入外網網址訪問一下:

這里你的外網39005端口首先要開放了,可以去安全組設置

使用Docker部署Spring Boot項目的實現步驟

說明使用 Docker 部署 Spring Boot 項目成功!

到此這篇關于使用Docker部署Spring Boot項目的實現步驟的文章就介紹到這了,更多相關Docker部署Spring Boot內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://juejin.cn/post/6941155134026022920

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女禁区视频免费观看精选 | 成人高辣h视频一区二区在线观看 | 桃乃木香奈作品在线观看 | 西野翔全部作品在线观看 | 99久久www免费 | 美女禁18| 男女交性特一级 | 视频一区二区三区欧美日韩 | 女人zooxx禽交 | 国产亚洲毛片在线 | youporn在线 | 久久精品视在线观看85 | 男人的天堂日本 | 亚洲乱码尤物193yw在线播放 | 情侣奴伺候女王第2部分小说 | 66j8影院xxxx深夜 | 嫩草影院地址一地址二 | 成人一级黄色大片 | 99福利影院 | 韩国一大片a毛片女同 | 草草影院免费 | 好涨好爽好大视频免费 | 91精品国产免费久久国语蜜臀 | 国产精品99爱免费视频 | 变态人shou交小说 | 日韩欧美一区二区在线观看 | 久久久久久免费高清电影 | 校花在公车上被内射好舒 | 91亚洲一区二区在线观看不卡 | 桃乃木香奈作品在线 | 免费理伦片手机在线播放 | 国产一卡2卡3卡4卡公司科普 | 蜜桃久久久亚洲精品成人 | 日本三级免费看 | 女仆掀起蕾丝裙被打屁股作文 | 男人天堂资源 | 蜜桃视频一区二区三区四区 | 亚洲成在人线久久综合 | 美女被灌浣肠失禁视频 | 动漫女性扒开尿口羞羞漫画 | 福利社在线免费观看 |