[個人筆記] docker個人筆記

  • 3009
  • 0
  • 2021-03-16

純屬個人筆記

另外Docker如果是Windows Server最低要求是 2016 (2012無法使用)

 

建議直接看保哥的文章,弄了老半天的結果按照保哥的文章follow結果就解了

 

因為專案是使用當前最新的.net core 3.1 可能會遇到電腦預設安裝的sdk不是最新的

指令安裝最新3.1 sdk

這個是有成功的版本 (dockerfile要跟專案放一起)
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy everything else and build app
COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "DockerTest.dll"]



(windows版,有指定port)
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy everything else and build app
COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
ENV ASPNETCORE_URLS=http://+:5566
WORKDIR /app
EXPOSE 5566/tcp
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "DockerTest222.dll"]



如果是放到linux環境要改用
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build

 

如果是linux上跑的話,可能遇到SQL Server連不上的問題

#base image 更換成 
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build

#強制不使用 TLS 1.2
RUN sed -i 's/MinProtocol = TLSv1.2/MinProtocol = TLSv1/' /etc/ssl/openssl.cnf \
&& sed -i 's/CipherString = DEFAULT@SECLEVEL=2/CipherString = DEFAULT@SECLEVEL=1/' /etc/ssl/openssl.cnf

 

另外docker run 我用瀏覽器瀏覽 localhost會一直導到IIS的首頁 (兩邊打架了?) ,只能用127.0.0.1

其他指令

docker image ls顯示所有image清單
docker ps顯示哪些container正在run
docker build -t 指定名稱 .build並指定image名稱
docker rmi -f imageID刪除
docker run -p 8000:80 your_image_name_or_ID要看錯誤訊息的docker run版本
docker run -d --name=myapp --rm -p 8000:80 your_image_name_or_ID建立照著保哥的指令做
docker exec -it f63a9d64287d bash  (Ctrl + D 離開)進入某台container裡執行指令
docker build -f Dockerfile333 -t my_image . 
"C:\Program Files\PuTTY\plink.exe" -v your_account@192.168.123.123 -pw your_password "docker image ls"

遠端執行docker指令

(需安裝putty)

docker tag {image_name} {registry_server}/{user_name}/{other_image_name}推到自己的self host server
docker push {registry_server}/{user_name}/{other_image_name}推到自己的self host server
 docker images | awk '{print $1}' | awk 'NR==2'(linux?) 列出最新的那個image
  
  
  

 

如何讓特定的container使用某個port (聽說較新版 2.2+ .net core 要用程式碼那種才會生效)

(Dockerfile設定檔) ENV ASPNETCORE_URLS=http://+:5566

(程式碼端)  using UseUrls("http://*:5000") in Programs.cs file.

 

 

啟動後,本機連不上的問題

主要應該是下run指令的時候 指定的port的問題 

docker run -d --name=myapp_0120 --rm -p 4554:5566 test_0120_001

 

 

 

 

如何進入某台container裡面 並修改appSetting檔案

docker exec -it f63a9d64287d bash
apt-get update
apt-get install nano

 

其他雜項指令

docker pull
docker push

 

 

 

ubuntu上 抓git、build docker的 sh (bash檔案)


cd /home/folder_name/game.backend
git checkout origin/test_docker_20200122
git pull origin test_docker_20200122

cd /home/folder_name/game.backend
docker build .


 

這幾個連結 有幫助我一些

https://docs.microsoft.com/en-gb/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-3.1

(IT邦幫忙) https://ithelp.ithome.com.tw/m/articles/10202447

(保哥) https://blog.miniasp.com/post/2018/08/25/How-to-deploy-ASPNET-Core-to-Docker-Container

(遠端執行docker) https://stackoverflow.com/questions/44446472/docker-run-on-a-remote-host

 

 

 

下方版本是  我方的專案,專案結構稍微再多層一點

另外 sdk:3.1-bionic 這段是解決發布到 ubuntu上面後 SQL連線相關的程式碼就會timeout (google到似乎 .net core 3.0+ 才有的問題)

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR app

# copy csproj and restore as distinct layers
COPY PGS.API.AdminOny.sln .
COPY PGS.Models/*.csproj ./PGS.Models/
COPY PGS.DA/*.csproj ./PGS.DA/
COPY PGS.BL/*.csproj ./PGS.BL/
COPY PGS.Utility/*.csproj ./PGS.Utility/
COPY PGS.Web.Modules/*.csproj ./PGS.Web.Modules/
COPY PGS.API.AdminSys/*.csproj ./PGS.API.AdminSys/
RUN dotnet restore


COPY PGS.Models/. ./PGS.Models/
COPY PGS.DA/. ./PGS.DA/
COPY PGS.BL/. ./PGS.BL/
COPY PGS.Utility/. ./PGS.Utility/
COPY PGS.Web.Modules/. ./PGS.Web.Modules/
COPY PGS.API.AdminSys/. ./PGS.API.AdminSys/

WORKDIR /app/PGS.API.AdminSys
RUN dotnet publish -c Release -o out


FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS runtime
WORKDIR /app/PGS.API.AdminSys
COPY --from=build /app/PGS.API.AdminSys/out .
ENTRYPOINT ["dotnet", "PGS.API.AdminSys.dll"]

 

 

 

 

git source有多個的案例

- root

  - API.Sample

  - MySample.Utility

執行docker的位置 是在root那層、docker file則是放在 API.Sample

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR app

# copy csproj and restore as distinct layers

COPY ./API.Sample/API.Sample.sln ./API.Sample/
COPY ./API.Sample/WebApplication1/*.csproj ./API.Sample/WebApplication1/

COPY ./MySample.Utility/MySample.Utility.sln ./MySample.Utility/
COPY ./MySample.Utility/MySample.Utility/*.csproj ./MySample.Utility/MySample.Utility/

COPY ./API.Sample/WebApplication1/. ./API.Sample/WebApplication1/
COPY ./MySample.Utility/MySample.Utility/. ./MySample.Utility/MySample.Utility/


WORKDIR /app/API.Sample
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS runtime
ENV ASPNETCORE_ENVIRONMENT Dev
EXPOSE 3100/tcp
EXPOSE 3101/tcp
EXPOSE 3102/tcp
EXPOSE 3109/tcp

WORKDIR /app/API.Sample
COPY --from=build /app/API.Sample/out .
ENTRYPOINT ["dotnet", "API.Sample.dll"]

 

 

.dockerignore (如果Docker build遇到一些奇怪的nuget錯誤訊息的話)

# directories
**/bin/
**/obj/
**/out/

# files
Dockerfile*
**/*.trx
**/*.md
**/*.ps1
**/*.cmd
**/*.sh

 

 

dockerfile (要把其他檔案copy到 out資料夾)


WORKDIR /app/PGS.API.AdminSys
RUN dotnet publish -c Release -o out

WORKDIR /app/
COPY PGS.API.AdminSys/*.sh ./PGS.API.AdminSys/out