在 SAP Kyma 上部署一個 Go MSSQL API Endpoint main.go dockerfile

鏈接:https://developers.sap.com/tutorials/cp-kyma-api-mssql-golang.html

本地文件:C:\Code\referenceCode\SAP Kyma教程例子\api-mssql-go

main.go

go 應用的入口:

dockerfile

該 dockerfile 定義了兩個階段來構造 docker 鏡像文件。

(1)In the first stage, a Go image is used. It copies the related content of the project into the image and builds the application.

(2)The built application is then copied into the Docker scratch image and exposed on port 8000. The scratch image is an empty image containing no other tools within it so obtaining a shell/bash session is not possible.

FROM scratch
WORKDIR /app
COPY --from=builder /app/api-mssql-go /app/

如果上面三行代碼刪除,對最後構建好的 docker 鏡像文件的影響就是,尺寸會比不註釋後的鏡像文件尺寸大。這三行代碼的作用是,從一個空的 scratch 鏡像開始構建,僅將之前 go 鏡像 /app/api-mssql-go 文件夾拷貝到新鏡像的 app 目錄,這樣尺寸大大減小。

apirule:


從上圖可以看到,這個 GO MSSQL API endpoint,支持對訂單的增刪改查。

本地運行 go 應用:

go run ./cmd/api/main.go

本地 endpoint 如下:

http://localhost:8000/orders

讀取訂單:curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:8000/orders

創建訂單:curl --data "{"order_id":"10000003","description":"test from curl"}" http://localhost:8000/orders

進入 api-mssql-go 文件夾,構建 docker 鏡像:

docker build -t i042416/api-mssql-go -f docker/Dockerfile .

上傳鏡像:
docker push <your-docker-id>/api-mssql-go

本地啓動鏡像:

docker run -p 8000:8000  --name api-mssql-go \
-e MYAPP_username="sa" \
-e MYAPP_password="Yukon900" \
-e MYAPP_database="DemoDB" \
-e MYAPP_host="host.docker.internal" \
-e MYAPP_port="1433" \
-d <your-docker-id>/api-mssql-go:latest

最後,把 k8s 文件夾下所有 yaml 文件,部署到 SAP Kyma 即可。

更多Jerry的原創文章,盡在:"汪子熙":


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章