Skip to content

Instantly share code, notes, and snippets.

@relyky
Last active December 26, 2023 07:17
Show Gist options
  • Save relyky/9e2b6681e090c8f29475bb4e27e9c84b to your computer and use it in GitHub Desktop.
Save relyky/9e2b6681e090c8f29475bb4e27e9c84b to your computer and use it in GitHub Desktop.
Docker CLI 常用指令紀錄 WSL, for .NET6 Blazor App
#※注意:請用 Powershell 執行 Docker CLI 指令。
步驟
1)Clone (取得原始碼)
cd <building_worker_folder> ------- 切換到編輯用途的工作目錄。
docker run --name repo alpine/git clone https://github.com/docker/getting-started.git
------ 自remote repo 複製專案原始碼到 local repo。並將此container取名為repo。
docker cp repo:/git/getting-started/ .
------ 自local repo 複製專案原始碼到工作目錄。
docker run --name repoDemo alpine/git clone http://192.168.0.111/Bonobo.Git.Server/MyCloudDm.git
------ 自remote repo 複製專案原始碼到 local repo。並將此container取名為repoDemo。
docker cp repoDemo:/git/MyCloudDm/ .
2)Build (CI)
cd <project_folder> ------- 切換到專案目錄。
docker build -t <output_image:tag> . ------- 依Dockerfile編譯,tag標籤預設:latest。
docker build --no-cache --force-rm -t <output_image:tag> . ------- 依Dockerfile編譯,編譯後不留CACHE。
cd ./getting-started
docker build -t docker101tutorial . ------- 依Dockerfile編譯,tag值latest。
cd ./MyCloudDm
docker build -t my-cloud-dm:1.0 . ------- 依Dockerfile編譯,tag值1.0。
3)Run (CD)
docker run -d -p 8088:80 --name docker-tutorial docker101tutorial
docker run -d -p 8080:80 --name MyCloudDm my-cloud-dm:latest
docker run -d -p <o_port>:<i_port> --name <container_idname> <image_idname>
--- container 指令
$ docker container ls --all 列出全部container
$ docker container rm <container> 移除container
--- IMAGE 指令
$ docker image list 列出IMAGE
$ docker save --output <image.tar> <image> 匯出IMAGE
$ docker load --input <image.tar> 匯入IMAGE
-- 執行 IMAGE
$ docker run -d -p 8088:80 --name <container> IMAGE
$ docker run -d -p 8088:80 -p 8433:433 --name MyCloudDm5.0 my-cloud-dm:5.0
同時開啟80與443 port 對映,並指定部署5.0版本,預設IMAGE是latest版本。
--============================
§§ 步驟統整
## 切換到工作目錄
PS> cd C:\Temp\Build
## clone, 取得程式碼(注意 git 可能需輸入帳號密碼)
PS> docker run --name repoDemo alpine/git clone http://192.168.0.111/Bonobo.Git.Server/MyCloudDm.git
PS> docker cp repoDemo:/git/MyCloudDm/ .
## build
PS> cd .\MyCloudDm\
PS> docker build --no-cache --force-rm -t my-cloud-dm:latest --file Release.Dockerfile .
## run
PS> docker run -d -p 8080:80 --name MyCloudDm my-cloud-dm:latest
## 重置工作資源
PS> docker container rm repoDemo
PS> cd ..
PS> del .\MyCloudDm\
## Docker on ubuntu 手札
## docker 指令
檢查版本
$ sudo docker --version
$ docker version
## 工作管理員 GUI
$ gnome-system-monitor 系統監控
## 當用cmd
$ cmd 終端機
## 當用bash指令:
$ ps 列出正在執行指令
$ kill [pid] 結束單個程序。
$ kill -9 [pid] 強制中止單個程序。
$ pkill <程序名> 結束整個程序族。
$ killall <程序名> 結束整個程序族。
$ xkill 在圖形介面中點殺程序。
## 列出及查詢已安裝的套件
$ sudo dpkg -l
$ sudo dpkg -l | grep php
## 更新 Update your Ubuntu box
$ sudo apt update
$ sudo apt upgrade
## install curl command
$ sudo apt install curl
$ curl --version
## Docker@Unbutu 快速安裝法(建議使用)
$ curl -sSL https://get.docker.com/ | sudo sh
##
https://docs.docker.com/get-docker/
https://stackoverflow.com/questions/72281976/docker-is-running-docker-desktop-says-docker-desktop-stopped
C:\Program Files\VMware\VMware Player>vmware-kvm F:\Ubuntu_20.04_64\Ubuntu_20.04_64.vmx
Docker Desktop installation on Ubuntu 22.04
https://www.youtube.com/watch?v=Vplj9b0L_1Y&ab_channel=Abstractprogrammer
Install Docker Desktop on Ubuntu
https://docs.docker.com/desktop/linux/install/ubuntu/
How To Install Docker CE on Linux Systems
https://computingforgeeks.com/install-docker-ce-on-linux-systems/
KVM virtualization support
https://docs.docker.com/desktop/linux/install/#kvm-virtualization-support
## Docker on windows 手札
開啟專案時就需先指定要部署到Docker。
使用 PowerShell 操作docker
準備動作
因部署標的是 Docker@Ubuntu
1)所以需安裝:Docker Desktop 與 WSL(Windows subsystem Linux)
2)安裝最新的 PowerShell 以取得新功能和改進功能!https://aka.ms/PSWindows
========== 正在檢查容器必要條件 ==========
正在確認已安裝 Docker Desktop...
正在下載 Docker Desktop...
Manual installation steps for older versions of WSL
https://docs.microsoft.com/en-us/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package
步驟
1)Clone (取得原始碼)
docker run --name repo alpine/git clone https://github.com/docker/getting-started.git
docker run --name repoDemo alpine/git clone http://192.168.0.111/Bonobo.Git.Server/AsvtCloudDm.git
docker cp repo:/git/getting-started/ .
docker cp repoDemo:/git/AsvtCloudDm/ .
2.prefix)
cd /AsvtCloudDm
cd /getting-started
2)Build (CI)
cd getting-started
cd <project_sln_folder>
docker build -t docker101tutorial . ------- 專案原始碼目錄。
docker build --no-cache --force-rm -t asvt-cloud-dm .
docker build -t <output_image_name> . ------- 將依Dockerfile編譯。
3)Run (CD)
docker run -d -p 8088:80 --name docker-tutorial docker101tutorial
docker run -d -p 8080:80 --name AsvtCloudDm asvt-cloud-dm:latest
docker run -d -p <o_port>:<i_port> --name <container_idname> <image_idname>
-d ------ deamon 服務模式,如:web service等應用
-p ------ port number 對映
--name ------ 指定來源 image 與目標對象
4)清除 alpine/git 中繼檔
docker container rm repoDemo ------- 移除名為repoDemo的container。
docker container rm <container> ------- 移除名為repoDemo的container。
docker container ls --all ------- 列出所有container。
--- image 指令
$ docker image list 列出IMAGE
$ docker save IMAGE > IMAGE.tar 匯出IMAGE
$ docker load --input IMAGE.tar 匯入IMAGE
C:\Temp\Temp>
$ docker save --output AsvtCloudDm.tar asvtclouddm
$ docker load --input AsvtCloudDm.tar
$ docker run -d -p 8088:80 --name AsvtCloudDm asvtclouddm
$ docker run -d -p 8080:80 -p 8433:433 --name AsvtDM asvtdm:dev
同時開啟80與443 port 對映,並指定部署dev版本,預設是latest版本。
--=======
PS> cd C:\Temp\Temp
## 取得程式碼
PS> docker run --name repoDemo alpine/git clone http://192.168.0.111/Bonobo.Git.Server/AsvtCloudDm.git
PS> docker cp repoDemo:/git/AsvtCloudDm/ .
## build
PS> cd .\AsvtCloudDm\
PS> docker build --no-cache --force-rm -t asvt-cloud-dm:latest --file Release.Dockerfile .
## run
PS> docker run -d -p 8080:80 --name AsvtCloudDm2 asvt-cloud-dm:latest
## 重置工作資源
PS> docker container rm repoDemo
PS> cd ..
PS> del .\AsvtCloudDm\
# https://hub.docker.com/_/microsoft-dotnet
# https://aka.ms/containerfastmode
# 多專案模組: (此例為 B
# MyCloudDm 主專案模組
# MyBiz 次專案模組 (實務上可能有多個)
#
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
# copy csproj and restore as distinct layers
COPY *.sln .
COPY MyCloudDm/*.csproj ./MyCloudDm/
COPY MyBiz/*.csproj ./MyBiz/
RUN dotnet restore
# copy everything else and build app
COPY MyCloudDm/. ./MyCloudDm/
COPY MyBiz/. ./MyBiz/
WORKDIR /src/MyCloudDm
RUN dotnet publish -c release -o /app --no-restore
# final stage/image
FROM base AS final
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "MyCloudDm.dll"]
註: WSL (windows subsystem Linux)
在 Windows 上跑 Linux。
# 安裝
wsl --install ------ 安裝
wsl --update ------ 更新版本
wsl --version ------ 取得版號
wsl --help ------ 指令說明
wsl --list --verbose ------ 安裝在 WSL 的『發佈』與明細
wsl --distribution <Distro> ------ 執行指定的發佈。
wsl -d <Distro> ------ 執行指定的發佈。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment