如何在docker容器中部署應用?docker是開發者的開放平臺,容器是可擴展的,可更安全的使用和部署。docker容器能夠共享一個內核并共享應用程序庫,這里介紹在docker容器中部署簡單的spring boot應用。
一個簡單的hello world應用。
1.1 maven依賴
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version>1.5.4.release</version>
<relativepath/> <!-- lookup parent from repository -->
</parent>引入spring-boot-starter-web來創建一個web應用。
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>1.2 主應用程序
@springbootapplication
public class firtspringbootapplication {
public static void main(string[] args) {
springapplication.run(firtspringbootapplication.class, args);
}
}1.3 服務接口
restful服務接口:
@restcontroller
public class hellocontroller {
@requestmapping(/hello)
public string hello() {
return hello world;
}
}2 安裝docker,安裝完畢后,docker環境的虛擬ip是192.168.99.100。
3 部署應用
3.1 spring boot應用打包
在spring boot應用工程目錄下執行mvn 打包命令:
mvn package spring-boot:repackage
工程的target目錄里生成了一個jar包 firtspringboot-0.0.1-snapshot.jar。將jar包更名為firtspringboot-1.0.jar上傳到docker環境里。
3.2 dockerfile文件
dockfile原則上是一個批處理文件,包含了構建鏡像的命令。將這些命令放入文件中并不是絕對必要的,因為我們可以將它們傳遞到命令行,只是使用文件更簡單一些。
現在創建一個dockerfile:
from alpine:edge
maintainer peterwanghao.com
run apk add --no-cache openjdk8
volume /tmp
add firtspringboot-1.0.jar firtspringboot.jar
expose 8080
entrypoint [java,-jar,/firtspringboot.jar]3.3創建鏡像
在dockerfile文件目錄下運行docker build命令,這將下載基鏡像并一條接一條地運行dockerfile文件中的條目。創建的鏡像的名字是first,標簽1.0。–rm=true,在整個構建過程成功后刪除中間環節的容器。
$docker build -t first:1.0 --rm=true .
使用docker images命令列出所有的鏡像和它們的詳細信息。
docker@default:~/first-spring-boot$ docker images
repository tag image id created size
first 1.0 02f67d8c308d 17 hours ago 119mb
hello-world latest e38bc07ac18e 6 weeks ago 1.85kb
alpine edge 5c4fa780951b 4 months ago 4.15mb3.4 運行容器
接下來,我們將運行容器。使用下面的命令加載并運行容器。
docker run --name=first -p 8080:8080 -t first:1.0
使用docker ps命令列出正在運行的容器
docker@default:~/first-spring-boot$ docker ps
container id image command created status ports names
9fc3aef6e852 first:1.0 java -jar /firtspri 5 hours ago up 5 hours 0.0.0.0:8080->8080/tcp first測試訪問
教程作者:raisehead
原文鏈接:https://www.jianshu.com/p/94e8dbba5d1d
西部數碼的容器云產品是通過docker技術,在云計算集群服務器上部署容器云服務實現,功能強大、簡單易用,可作集群服務,自由搭建私有網絡,輕松組建服務集群。
高性價比的容器云產品鏈接 https://www.west.cn/paas/container/
騰訊云服務器需要備案么這個網站打不開請幫忙看一下什么情況鏈接無法打開-虛擬主機/數據庫問題能幫忙看下這個域名之前在咱這備案過嗎便利店要從消費者體驗重新定義“貨場人”谷歌瀏覽器怎么打印網頁 Chrome瀏覽器打印網頁的步驟ecs云服務器可以做什么去年上半年91家B2B電商融資逾85億元