一般制作docker鏡像,都是基於網絡同道精心打造的鏡像。如果想自己構建一個基礎的鏡像可以通過此次示例來學習。
學習之路
- alpine-minirootfs-3.10.2-x86_64.tar.gz alpine(linux環境)
- aspnetcore-runtime-3.0.0-linux-musl-x64.tar.gz linux環境下的netcore runtime
- dockerfile 構建鏡像的命令集
下載源碼並構建docker鏡像
說明:
- 需要切換到項目目錄下執行構建命令 docker build -t webapi .
不要忘記命令后的 .
- 構建命令中的 -t webapi 是給構建鏡像起別名
查看制作鏡像信息
PS C:\WINDOWS\system32> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
webapi latest 5462b957a3e2 About an hour ago 161MB
運行鏡像
PS E:\My\docker> docker run -i -t --name webapi -p 6002:6001 webapi /bin/sh
/ #
說明:
- -it 開啟容器shell終端
- --name 給容器起別名
- -p 主機與容器之間的端口映射
- /bin/sh 運行容器后進入的工作目錄
運行netcore webapi
/ # ls #查看容器目錄
bin etc lib mnt opt publish run srv tmp var
dev home media netcore proc root sbin sys usr
/ # cd publish #webapi publish 目錄
/publish # ls
Google.Protobuf.dll System.Data.SqlClient.dll
Microsoft.Data.Sqlite.dll System.Security.Cryptography.ProtectedData.dll
MySql.Data.dll appsettings.Development.json
Newtonsoft.Json.dll appsettings.json
Npgsql.dll runtimes
Oracle.ManagedDataAccess.dll web.config
SQLitePCLRaw.batteries_green.dll webapi.deps.json
SQLitePCLRaw.batteries_v2.dll webapi.dll
SQLitePCLRaw.core.dll webapi.exe
SQLitePCLRaw.provider.e_sqlite3.dll webapi.pdb
SqlSugar.dll webapi.runtimeconfig.json
System.Configuration.ConfigurationManager.dll
說明:
- 端口映射后就可以通過主機 localhost:6001 服務webapi站點了
- 此次webapi示例使用 默認的netcore webapi項目,只是修改了運行示例的接口路徑為 api
鏡像源碼:https://github.com/eric-projects/docker-net-core
歡迎打賞