簡介
緣起想試一下Docker的掛載,
根據我對掛載的理解就是容器中的一個文件,
對應宿主機中的一個文件.
我就想一下什么場景中會使用到這種,
后來想到可以寫入日志,
就想着搞一個程序,就是專門用於新建日志.然后掛載測試.
第一想法其實是想搞一個Web應用,
容器運行起來之后,我們直接訪問Web的api,
然后api可以將請求路徑和參數,寫入日志文件中.
但是,以前在Docker中發布過Web應用,
所以這次想試一試在Docker中部署一個控制台程序.
控制台程序,發布,Dockerfile
准備一個簡單的控制台程序,
while
Console.ReadLine()
while在工作中已經很少用到了,"Console.ReadLine()"更是認識了之后一直沒用過...
當然程序中最重要的是要寫日志.
程序部分好說,然后發布.
我意外的是Dockerfile這次竟然也是一次成功了.
(現在編寫Dockerfile還是磕磕絆絆的...主要是復制粘貼)
將發布文件上傳到Linux服務器
記得上次上傳文件還是在用{Xshell}+一個插件,
來完成文件上傳的.
這次是用 {MobaXterm},功能更強大(使用更"傻瓜").
MobaXterm(按鈕欄)=>會話=>會話設置=>SFTP
然后就是文件互相拖動即可.
//Linux上經常會忘了目錄,比如我這次發布控制台程序,就忘了上次將Web程序發布到哪里去了...
鏡像生成.Docker build又忘了...
距離上次生成鏡像,已經過去了2個月左右了,只記得一個build,具體參數全忘...
#先確認下是否文件已經就位,別build了一個寂寞...
[root@VM_0_12_centos ~]# cd /usr/dockerPubTest/ConsolePro/
[root@VM_0_12_centos ConsolePro]# ls
dockerConsoleDemo.deps.json dockerConsoleDemo.dll dockerConsoleDemo.exe dockerConsoleDemo.pdb dockerConsoleDemo.runtimeconfig.json Dockerfile
#確認發布文件都在,開始生成鏡像.
[root@VM_0_12_centos ConsolePro]# docker build -t dc01 .
Sending build context to Docker daemon 189.4kB
Step 1/4 : FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
3.1-buster-slim: Pulling from dotnet/core/runtime
...
Step 2/4 : WORKDIR /app
---> Running in bad776943dae
Removing intermediate container bad776943dae
---> da7d0f6af7e8
Step 3/4 : COPY . /app
---> 8f79950e496a
Step 4/4 : ENTRYPOINT ["dotnet", "dockerConsoleDemo.dll"]
---> Running in 4b94880913ad
Removing intermediate container 4b94880913ad
---> 6141b3f2bf13
Successfully built 6141b3f2bf13
Successfully tagged dc01:latest
看到生成鏡像過程其實就是執行{Dockerfile}中的一行行命令.
#查看鏡像,確實已經成功生成了.
[root@VM_0_12_centos ConsolePro]# docker images
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
---|---|---|---|---|
dc01 | latest | 6141b3f2bf13 | 3 minutes ago | 190MB |
容器啟動.Docker run
#啟動鏡像
[root@VM_0_12_centos ConsolePro]# docker run --name dci01 -ti dc01
Hello World!
********************************************
請輸入第一數字
2
...
其中尷尬的程序第一次寫的時候,
沒有寫退出輸入,並且也忘了如何快捷鍵退出容器,
所以被自己寫的程序困在里面了....
這時候想起命令行,經常會出現的"Y/N?"重要性...
容器.退出.快捷鍵=>{Ctrl + P + Q}
容器.進入.查看日志
#容器.進入
[root@VM_0_12_centos ConsolePro]# docker exec -it dci01 /bin/bash
#容器.列表.查看
root@35995f2eea16:/app# ls
Dockerfile Log dockerConsoleDemo.deps.json dockerConsoleDemo.dll dockerConsoleDemo.exe dockerConsoleDemo.pdb dockerConsoleDemo.runtimeconfig.json
#容器.列表.查看.Log
root@35995f2eea16:/app# cd Log/
root@35995f2eea16:/app/Log# ls
2020-09-22.txt
#容器.txt文件查看
root@35995f2eea16:/app/Log# vim 2020-09-22.txt
bash: vim: command not found
root@35995f2eea16:/app/Log# vi 2020-09-22.txt
bash: vi: command not found
root@35995f2eea16:/app/Log# cat 2020-09-22.txt
09/22/2020 13:22:28 ********************************************
09/22/2020 13:22:28 請輸入第一數字
09/22/2020 13:22:33 輸入正確=>2
09/22/2020 13:22:33 請輸入第2數字
09/22/2020 13:22:34 輸入正確=>3
09/22/2020 13:22:34 請輸入算法["+","-","*","/"]!
09/22/2020 13:22:36 輸入正確=>+
09/22/2020 13:22:36 結果:5
09/22/2020 13:22:36 ********************************************
...
經驗+1...
記得以前查看文件都是用vim...
這次發現不好使了,只能再去搜了一下,
使用"cat"...
//cat,原來不是"貓"的意思,是"concatenate"縮寫...
//擴展,vim和cat的區別.什么時候用vim,什么時候用cat?
//為什么不能用vim查看txt ...
容器.進入.執行我的程序,
[root@VM_0_12_centos ConsolePro]# docker ps -a
CONTAINER ID IMAGE COMMAND NAMES
35995f2eea16 dc01 "dotnet dockerConsol…" dci01
#注意上面的那一列:COMMAND NAMES,進入控制台程序,
[root@VM_0_12_centos ConsolePro]# docker exec -ti dci01 dotnet dockerConsoleDemo.dll
Hello World!
********************************************
請輸入第一數字
1
輸入正確=>1
請輸入第2數字
1
輸入正確=>1
請輸入算法["+","-","*","/"]!
+
輸入正確=>+
結果:2
********************************************
...
附錄_1_控制台程序代碼
程序主體
class Program
{
/// <summary>
/// Game.1+1.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.WriteLine("Hello!開始游戲?Y/N?");
string tempLog = string.Empty;
try
{
while (Console.ReadLine().ToLower().Equals("y"))
{
ConsoleAndLog("********************************************");
ConsoleAndLog("請輸入第一數字");
var tempInput = Console.ReadLine();
int firstNum = 0;
while (!int.TryParse(tempInput, out firstNum))
{
ConsoleAndLog("請輸入數字!");
tempInput = Console.ReadLine();
ConsoleAndLog("輸入錯誤+1=>" + tempInput);
}
ConsoleAndLog("輸入正確=>" + tempInput);
ConsoleAndLog("請輸入第2數字");
int secondNum = 0;
tempInput = Console.ReadLine();
while (!int.TryParse(tempInput, out secondNum))
{
ConsoleAndLog("請輸入數字!");
tempInput = Console.ReadLine();
}
ConsoleAndLog("輸入正確=>" + tempInput);
ConsoleAndLog("請輸入算法[\"+\",\"-\",\"*\",\"/\"]!");
List<string> list = new List<string>() { "+", "-", "*", "/" };
tempInput = Console.ReadLine();
while (!list.Any(s => s.Equals(tempInput)))
{
ConsoleAndLog("警告:請手不要抖...然后輸入+-*/!!!");
tempInput = Console.ReadLine();
}
ConsoleAndLog("輸入正確=>" + tempInput);
switch (tempInput)
{
case "+":
var logStr = string.Format("結果:{0}", firstNum + secondNum);
ConsoleAndLog(logStr);
break;
case "-":
Console.WriteLine(string.Format("結果:{0}", firstNum - secondNum));
break;
default:
break;
}
ConsoleAndLog("下一局?Y/N?");
}
}
catch (Exception ex)
{
ConsoleAndLog(ex.Message);
}
}
/// <summary>
/// 輸出&記錄日志
/// </summary>
/// <param name="str"></param>
public static void ConsoleAndLog(string str)
{
Console.WriteLine(str);
LogCommon.WriteLog(str);
}
}
日志記錄代碼↓
/// <summary>
/// 日志
/// </summary>
public class LogCommon
{
/// <summary>
/// 記錄日志
/// </summary>
/// <param name="debugstr"></param>
public static void WriteLog(string debugstr)
{
FileStream fs = null;
StreamWriter sw = null;
try
{
string filename = DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
//日志目錄
string folder = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "/Log");
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
fs = new FileStream(folder + "/" + filename, System.IO.FileMode.Append, System.IO.FileAccess.Write);
sw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine(DateTime.Now.ToString() + " " + debugstr + "\r\n");
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sw != null)
{
sw.Flush();
sw.Dispose();
sw = null;
}
if (fs != null)
{
// fs.Flush();
fs.Dispose();
fs = null;
}
}
}
}
附錄_2_Dockerfile
#Dockerfile中的內容
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app
COPY . /app
#"dockerConsoleDemo.dll"根據自己項目的名稱修改
ENTRYPOINT ["dotnet", "dockerConsoleDemo.dll"]
結尾.
程序已經搞起,
現在要去搞掛載了....