最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Docker部署Spring-boot項(xiàng)目的示例代碼

 更新時間:2018年12月24日 11:36:54   作者:泥稱已被棧用  
這篇文章主要介紹了Docker部署Spring-boot的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、基礎(chǔ)Spring-boot快速啟動

 1.1 快速啟動 pom.xml加入如下依賴

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
  </parent>

  <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </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-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>spring-docker</finalName>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

Spring-boot啟動類

@SpringBootApplication
public class DockerApplication {

  public static void main(String[] args) {
    SpringApplication.run(DockerApplication.class, args);
  }
}

測試API

@RestController
public class DockerStarterApi {

  @GetMapping("/api/docker/hello")
  public String hello() {
    return "hello docker";
  }
}

配置啟動配置文件 application.yml

server:
 port: 9090 # 為了展示效果, 這里改了默認(rèn)端口8080

檢查Spring啟動

.  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v2.0.2.RELEASE)

...

2018-12-17 17:26:13.385 INFO 48740 --- [      main] o.s.j.e.a.AnnotationMBeanExporter    : Registering beans for JMX exposure on startup
2018-12-17 17:26:13.448 INFO 48740 --- [      main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9090 (http) with context path ''
2018-12-17 17:26:13.453 INFO 48740 --- [      main] pers.study.docker.DockerApplication   : Started DockerApplication in 1.982 seconds (JVM running for 2.602)

檢查API是否生效

$ curl -XGET 'http://localhost:9090/api/docker/hello'
hello docker

瀏覽器檢查

http://localhost:9090/api/docker/hello

1.2 打包啟動

項(xiàng)目打包

完成上面步驟之后,執(zhí)行打包命令:

$ mvn clean -U -Dmaven.test.skip compile package

因?yàn)樯厦娴膒om文件里面定義了 finalName ,所以在這里會看到編譯打包之后 target 目錄下會生成 spring-docker.jar

<finalName>spring-docker</finalName>

測試運(yùn)行

$ java -jar target/spring-docker.jar

不出意外(有問題留言~)運(yùn)行結(jié)果同上并檢查API是否生效即可.

二、Docker快速安裝

接下來開始準(zhǔn)備Docker

安裝

官網(wǎng)下載安裝

檢查安裝、查看幫助

$ docker --version
Docker version 18.06.0-ce, build 0ffa825

$ docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
...

鏡像加速

中國官方鏡像加速

三、配置Spring-boot + Docker

pom.xml 添加docker plugin

<properties>
    <docker.image.prefix>springboot</docker.image.prefix>
  </properties>

  <build>
    <plugins>
      <!-- Docker maven plugin -->
      <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>1.0.0</version>
        <configuration>
          <imageName>${docker.image.prefix}/${project.build.finalName}</imageName>
          <dockerDirectory>src/main/docker</dockerDirectory>
          <resources>
            <resource>
              <targetPath>/</targetPath>
              <directory>${project.build.directory}</directory>
              <include>${project.build.finalName}.jar</include>
            </resource>
          </resources>
        </configuration>
      </plugin>
    </plugins>
  </build>

創(chuàng)建 Dockerfile 文件

根據(jù)上面 pom.xml 文件配置 <dockerDirectory>src/main/docker</dockerDirectory> ,這里配置了docker配置文件的目錄,所以需要再 src/main 下面創(chuàng)建docker文件夾,同時創(chuàng)建 Dockerfile 文件。

目錄機(jī)構(gòu)如圖:

docker配置文件結(jié)構(gòu).png

編輯 Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD spring-docker.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

FROM 表示以Java8為基礎(chǔ)鏡像

VOLUME 表示掛載目錄

ADD 拷貝打包文件并重命名為 app.jar

ENTRYPOINT 根據(jù)下面的官方文檔解釋大致是為了縮短tomcat啟動時間而添加的一個系統(tǒng)屬性。

We added a VOLUME pointing to /tmp because that is where a Spring Boot application creates working directories for Tomcat by default. The effect is to create a temporary file on your host under /var/lib/docker and link it to the container under /tmp . This step is optional for the simple app that we wrote here but can be necessary for other Spring Boot applications if they need to actually write in the filesystem.

To reduce Tomcat startup time we added a system property pointing to "/dev/urandom" as a source of entropy. This is not necessary with more recent versions of Spring Boot, if you use the "standard" version of Tomcat (or any other web server).

配置完成!

四、Docker啟動Spring-boot

進(jìn)入 module 執(zhí)行:

$ mvn package docker:build

[INFO] Scanning for projects...

...

 ---> Running in e1f8aba72bdf
Removing intermediate container e1f8aba72bdf
 ---> 36a61c09f09a
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 36a61c09f09a
Successfully tagged springboot/spring-docker:latest
[INFO] Built springboot/spring-docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.367 s
[INFO] Finished at: 2018-12-17T20:48:21+08:00
[INFO] ------------------------------------------------------------------------

查看鏡像

$ docker images
REPOSITORY         TAG         IMAGE ID      CREATED       SIZE
springboot/spring-docker  latest       36a61c09f09a    2 minutes ago    123MB

運(yùn)行鏡像

$ docker run -p 9090:9090 -t springboot/spring-docker
 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v2.0.2.RELEASE)

2018-12-17 12:53:21.502 INFO 1 --- [      main] pers.study.docker.DockerApplication   : Starting DockerApplication v1.0-SNAPSHOT on 94991c04be5d with PID 1 (/app.jar started by root in /)
2018-12-17 12:53:21.509 INFO 1 --- [      main] pers.study.docker.DockerApplication   : No active profile set, falling back to default profiles: default

···

2018-12-17 12:53:25.255 INFO 1 --- [      main] o.s.j.e.a.AnnotationMBeanExporter    : Registering beans for JMX exposure on startup
2018-12-17 12:53:25.337 INFO 1 --- [      main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9090 (http) with context path ''
2018-12-17 12:53:25.353 INFO 1 --- [      main] pers.study.docker.DockerApplication   : Started DockerApplication in 4.485 seconds (JVM running for 5.346)

查看容器

$ docker ps
CONTAINER ID    IMAGE           COMMAND         CREATED       STATUS       PORTS                                  NAMES
94991c04be5d    springboot/spring-docker  "java -Djava.securit…"  53 seconds ago   Up 52 seconds    0.0.0.0:9090->9090/tcp                          quizzical_bhabha

驗(yàn)證啟動,訪問API

$ curl -XGET 'http://localhost:9090/api/docker/hello'
hello docker

至此Docker部署spring-boot搭建完成。

五、移除鏡像

 停止容器

$ docker stop 94991c04be5d
94991c04be5d

刪除容器

$ docker rm 94991c04be5d
94991c04be5d

刪除鏡像

$ docker image rm springboot/spring-docker
Untagged: springboot/spring-docker:latest
Deleted: sha256:36a61c09f09ab88cfe5a05f564deb57498682f4a6f3ec01d2a8c4fdc80ac1e41
Deleted: sha256:3f9aef70be6d4d43c205454d8874f10bc2f7280f70eb88cd1f04937b7965dd27
Deleted: sha256:9a5800e93615bb4c5128bb36d31ec494327c01f1a9a768c1ff538badf76628b9
Deleted: sha256:d9c66f907448fa9e61fd5f9267d7fcf8e1f4b52d0a20466414f2f45777261284

六、其他配置功能

添加環(huán)境屬性

$ docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 9090:9090 -t springbooot/spring-docker

后臺啟動運(yùn)行

$ docker run -p 9090:9090 -d springboot/spring-docker

開啟容器Debug 修改 Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD spring-docker.jar app.jar
ENV JAVA_OPTS ''
CMD java -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar app.jar

docker run

復(fù)制代碼 代碼如下:
$ docker run -e "JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n" -p 9090:9090 -p 5005:5005 -t springboot/spring-docker

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • docker如何對已經(jīng)啟動的容器添加目錄映射(掛載目錄)

    docker如何對已經(jīng)啟動的容器添加目錄映射(掛載目錄)

    當(dāng)我們創(chuàng)建容器之后,不可避免會遇到修改配置文件的操作,下面這篇文章主要給大家介紹了關(guān)于docker如何對已經(jīng)啟動的容器添加目錄映射(掛載目錄)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • docker中安裝elasticsarch 等鏡像的過程

    docker中安裝elasticsarch 等鏡像的過程

    這篇文章主要介紹了docker中安裝elasticsarch 等鏡像,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • Docker部署nginx并修改配置文件的實(shí)現(xiàn)方法

    Docker部署nginx并修改配置文件的實(shí)現(xiàn)方法

    這篇文章主要介紹了Docker部署nginx并修改配置文件的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • docker 如何添加證書

    docker 如何添加證書

    這篇文章主要介紹了docker 如何添加證書的操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • docker 查看容器日志命令的實(shí)現(xiàn)

    docker 查看容器日志命令的實(shí)現(xiàn)

    這篇文章主要介紹了docker 查看容器日志命令的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • linux 詳解useradd 命令基本用法

    linux 詳解useradd 命令基本用法

    這篇文章主要介紹了linux 詳解useradd 命令基本用法的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Docker中的文件/文件夾的掛載映射方式

    Docker中的文件/文件夾的掛載映射方式

    通過命令行在宿主機(jī)與Docker容器間掛載和映射文件夾,實(shí)現(xiàn)數(shù)據(jù)共享,具體步驟包括:在宿主機(jī)創(chuàng)建文件夾并使用Docker命令將其掛載到容器指定位置,需注意容器名稱與ID的使用差異
    2024-11-11
  • docker安裝redis并以配置文件方式啟動詳解

    docker安裝redis并以配置文件方式啟動詳解

    這篇文章主要介紹了docker安裝redis并以配置文件方式啟動詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Docker Machine創(chuàng)建Azure虛擬主機(jī)

    Docker Machine創(chuàng)建Azure虛擬主機(jī)

    這篇文章主要為大家詳細(xì)介紹了Docker Machine創(chuàng)建Azure虛擬主機(jī)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 詳解docker中使用systemctl啟動服務(wù)報(bào)錯的解決辦法

    詳解docker中使用systemctl啟動服務(wù)報(bào)錯的解決辦法

    這篇文章主要介紹了詳解docker中使用systemctl啟動服務(wù)報(bào)錯的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評論

禹城市| 苍南县| 松桃| 梁河县| 伊吾县| 循化| 德安县| 全南县| 兰溪市| 南汇区| 甘洛县| 尼勒克县| 太谷县| 拉萨市| 怀来县| 绥江县| 女性| 榆中县| 乌鲁木齐县| 唐河县| 正安县| 马龙县| 平谷区| 拉萨市| 徐汇区| 鹤山市| 兴城市| 桂东县| 仪征市| 普格县| 通河县| 怀来县| 股票| 兴文县| 乌什县| 泊头市| 芜湖县| 安化县| 黄冈市| 高密市| 崇左市|