跳到主要內容

Spring boot with docker

Spring boot with docker

Spring-boot 官方教學:https://spring.io/guides/gs/spring-boot-docker/,
做法跟設定跟官方教學有些不太一樣, 不考慮用 maven plugin 維護 docker images, 方便 devOps 統一管理。

Set up Spring-boot

  1. 先建立 spring-boot 專案, 專案命名為 demo-docker-spring
  2. 建立 controller, 簡單 echo Hello Docker World


package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by jerry on 2018/1/15.
 *
 * @author jerry
 */
@Slf4j
@RestController
public class Controller {

    @RequestMapping("/")
    public String home() {
        return "Hello Docker World";
    }
}


Dockerfile

docker 官方不支援 oracle-java, 所以找了一個 star 較高的 image 來用。 Dockerfile 就直接放在專案底下。


# https://hub.docker.com/_/java/ 不支援 oracle-java
# FROM: 指定 base image (https://hub.docker.com/r/cogniteev/oracle-java/)
FROM cogniteev/oracle-java

# expose 8080 port
EXPOSE 8080

# 複製當前路徑所有資料, 到容器裡面
COPY . /usr/app/demo-docker-spring

# WORKDIR: 指定 docker 執行起來時候的預設目錄位置
WORKDIR /usr/app/demo-docker-spring

# 在容器裡面編譯
RUN ./mvnw clean package

# 指定容器啟動後執行的命令,並且不會被 docker run 提供的參數覆蓋。
ENTRYPOINT ["java","-jar","target/demo-docker-spring-0.0.1.jar"]

Docker build

# 以本地的 Dockerfile 為基礎, 建立 image
docker build -t jerry80409/demo-docker-spring:0.0.1 .

Docker run

docker run -d \
    --name demo-docker-spring \
    -p 8080:8080 \
    jerry80409/demo-docker-spring:0.0.1

心得

在容器裡面執行 ./mvnw clean package 會延長 container 啟動的時間, 萬一編譯失敗還要去看 docker logs 過濾, 比較好的做法還是在 host 處理相關的編譯與測試, 修改為

Dockerfile

# https://hub.docker.com/_/java/ 不支援 oracle-java
# FROM: 指定 base image (https://hub.docker.com/r/cogniteev/oracle-java/)
FROM cogniteev/oracle-java

# expose 8080 port
EXPOSE 8080

# 版本管理, 接收 docker build --build-arg 指定的參數
ARG APP_VERSION

# 複製編譯後的 jar , 到容器裡面
COPY target/${APP_VERSION} /usr/app/demo-docker-spring/app.jar

# WORKDIR: 指定 docker 執行起來時候的預設目錄位置
WORKDIR /usr/app/demo-docker-spring

# 在容器裡面編譯
# RUN ./mvnw clean package

# 指定容器啟動後執行的命令,並且不會被 docker run 提供的參數覆蓋。
ENTRYPOINT ["java","-jar","app.jar"]

Docker build

透過 --build-arg 動態指定編譯好的 jar 檔, 方便在不同版本號使用。

# 以本地的 Dockerfile 為基礎, 建立 image
docker build \
  --build-arg APP_VERSION=demo-docker-spring-0.0.1.jar \
  -t jerry80409/demo-docker-spring:0.0.1 .

Docker run

docker run -d \
    --name demo-docker-spring \
    -p 8080:8080 \
    jerry80409/demo-docker-spring:0.0.1

留言

這個網誌中的熱門文章

Google Compute Engine‎ - AccessDeniedExceptions 403

原因 打算從 instance 打包 logs 到 google cloud storage 發生了 AccessDeniedException: 403 Insufficient OAuth2 scope to perform this operation. , 看起來是 instance 沒有 storage 權限 解決 Reference: https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#changeserviceaccountandscopes 重新設定 service account 權限 instance 上內建有 gcloud , 就直接用現有的工具查詢一下 instance 的 account. $ gsutil info 或者在本機直接 gcloud compute instances describe INSTANCE_NAMES Account: [alpha-number-compute@developer.gserviceaccount.com] Project: [our-project-name] 會看到 instance 的一些狀態, 接下來就簡單多了, 按照下列的說明, 要先 stop instance, 更改 storage scope 再重新 start 。 To change an instance's service account and access scopes, the instance must be temporarily stopped. To stop your instance, read the documentation for Stopping an instance. After changing the service account or access scopes, remember to restart the instance. # Stop Instance gcloud compute instances stop INSTANCE_NAMES # 設定 storage scope 為 full (Read, Write) gcloud co...

Spring-boot Thymeleaf Html5 SAXParseException 解析錯誤

thymeleaf 解析 html5 出錯 <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title>SB Admin - Start Bootstrap Template</title> <!-- Bootstrap core CSS--> <link href="../static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom fonts for this template--> <link href="../static/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"> <!-- Page level plugin CSS--> <link href="../static/vendor/datatables/dataTables.bootstrap4.css" rel="stylesheet"> <!-- Custom styles for this template--> <li...

Spring boot Https 設定

1. 產生 SSL certificate keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650 Enter keystore password: Re-enter new password: What is your first and last name? [Unknown]: jerry What is the name of your organizational unit? [Unknown]: td What is the name of your organization? [Unknown]: com What is the name of your City or Locality? [Unknown]: taipei What is the name of your State or Province? [Unknown]: taiwan What is the two-letter country code for this unit? [Unknown]: tw Is CN=jarvis, OU=td, O=urad, L=taipei, ST=taiwan, C=tw correct? [no]: yes 這個 certificate 是 self-signed certificate 沒有經過第三方認證, 所以沒有公信力, 正式上線會在瀏覽器看到 連線不被信任 要有公信力的 certificate 最簡單的是 Lets Encrypt , 其他就是花一些錢找簽發 certificate 的組織 2. Enable HTTPS in Spring Boot Spring Boot 內建的 tomcat 預設 http 是 8080, Spring Boot 可以設定 http 跟 https, 但沒辦法同時存在這兩個設定, 如果要同時存在兩種 connection, 建議依照 文件 建議設定 https, 再透過 programmatically 去設定 http 會比較容易。 相關範例可以參考 : https://...