在 Docker 裡執行 Microsoft SQL 伺服器
連結:https://developers.sap.com/tutorials/cp-kyma-mssql-deployment.html
參考程式碼:https://github.com/SAP-samples/kyma-runtime-extension-samples
本地路徑:C:\Code\referenceCode\SAP Kyma教程例子
This sample provides the MS SQL database configured with a sample
DemoDB
database which contains oneOrders
table populated with two rows of sample data.
這個例子展示瞭如何建立名為 DemoDB 的MSSQL 資料庫,以及名為 Orders 的資料庫表,以及兩行測試資料。
The app/setup.sql
file handles the generation of the database, table, and data.
app 資料夾下的 setup.sql 負責建立資料庫,資料庫表和測試資料。
Within the
app/init-db.sh
file, you can also configure the database user and password.
init-db.sh 檔案用於配置資料庫使用者名稱和密碼。
docker 資料夾
- FROM:The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.
Dockerfile 用於建立 docker 映象。最後一行命令,執行 app 資料夾下面的 entrypoint.sh 檔案。
Build the Docker image
根據 Dockerfile 構建一個映象:
進入如下資料夾:
C:\Code\referenceCode\SAP Kyma教程例子\database-mssql
執行命令列:
docker build -t i042416/mssql -f docker/Dockerfile .
注意,因為基於的映象名稱為 microsoft/mssql-server-linux, 故這個命令應該在 linux 作業系統裡完成:
映象成功製作完畢:
docker 映象製作完畢後,上傳到 docker hub:
docker push i042416/mssql
上傳成功:
本地執行這個映象:
sudo docker run -e ACCEPT_EULA=Y -e SA_PASSWORD=Yukon900 -p 1433:1433 --name sql1 -d i042416/mssql
進入映象內部,開啟 bash shell:
docker exec -it sql1 "bash"
Start the sqlcmd tool, which allows you to run queries against the database, by running this command: /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P Yukon900
輸入如下 query 命令:
1> USE DemoDB
2> SELECT * FROM ORDERS
3> GO
結果:成功讀取到兩條訂單資料:
在 Docker 內部的 /usr/src/app 資料夾下,確實發現了我製作 docker 映象時的檔案:
Microsoft SQL 伺服器,安裝在 /opt 目錄下:
更多Jerry的原創文章,盡在:"汪子熙":