如何使用.NET 6.0创建一个可工作的Docker容器?

huangapple go评论100阅读模式
英文:

How to create a working Docker Container With .NET 6.0?

问题

I've translated the code and relevant information for you:

  1. 我已成功创建了一个使用NodeReact的工作容器。
  2. 现在我尝试创建一个Docker容器来运行一个简单的C#可执行文件,该文件将侦听端口8000并返回“hello world”。如果我在本地访问http://localhost:8000/运行它,它可以工作。
  3. 以下是代码:
  4. using System;
  5. using System.Net;
  6. using System.Text;
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string url = "http://localhost:8000/";
  12. HttpListener listener = new HttpListener();
  13. listener.Prefixes.Add(url);
  14. listener.Start();
  15. Console.WriteLine("Listening on {0}", url);
  16. while (true)
  17. {
  18. HttpListenerContext context = listener.GetContext();
  19. HttpListenerRequest request = context.Request;
  20. HttpListenerResponse response = context.Response;
  21. Console.WriteLine("{0} {1}", request.HttpMethod, request.Url);
  22. byte[] buffer = Encoding.UTF8.GetBytes("Hello, World!");
  23. response.ContentLength64 = buffer.Length;
  24. response.OutputStream.Write(buffer, 0, buffer.Length);
  25. response.OutputStream.Close();
  26. }
  27. }
  28. }
  29. 我已经构建了该程序并将其移动到桌面的一个文件夹中:
  30. 如您所见,我已创建了Dockerfile,其中包含以下内容:
  31. FROM mcr.microsoft.com/dotnet/runtime:6.0
  32. WORKDIR /HelloWorld
  33. COPY .
  34. EXPOSE 8000
  35. CMD ["/HelloWorld/DockerTest.exe"]
  36. 然后,我使用以下命令构建了镜像:
  37. docker build -t imagename:tagname .
  38. 然后通过以下命令运行容器:
  39. docker run --name containername -p 8000:8000 imagename:tagname
  40. 容器立即退出并显示退出代码1,表示出现错误,但没有生成日志。
  41. 我认为可能出错的地方是使用了错误的.NET运行时映像(mcr.microsoft.com/dotnet/runtime:6.0是否正确?)或在Dockerfile中忘记了其他内容。如果您有任何问题,我将很乐意提供更多详细信息。

Please note that I've translated the code and information as requested. If you have any specific questions or need further assistance, feel free to ask.

英文:

I've already managed to create a working container using Node and React.
I'm now trying to create a docker container to operate a simple C# executable which will listen on port 8000 and send back "hello world". It works if I run it locally by visiting http://localhost:8000/
Here is the code:

  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string url = "http://localhost:8000/";
  9. HttpListener listener = new HttpListener();
  10. listener.Prefixes.Add(url);
  11. listener.Start();
  12. Console.WriteLine("Listening on {0}", url);
  13. while (true)
  14. {
  15. HttpListenerContext context = listener.GetContext();
  16. HttpListenerRequest request = context.Request;
  17. HttpListenerResponse response = context.Response;
  18. Console.WriteLine("{0} {1}", request.HttpMethod, request.Url);
  19. byte[] buffer = Encoding.UTF8.GetBytes("Hello, World!");
  20. response.ContentLength64 = buffer.Length;
  21. response.OutputStream.Write(buffer, 0, buffer.Length);
  22. response.OutputStream.Close();
  23. }
  24. }
  25. }

I have the built the program and moved it in a folder in the desktop:
如何使用.NET 6.0创建一个可工作的Docker容器?

As you can see I've created the Dockerfile, with this content:

  1. FROM mcr.microsoft.com/dotnet/runtime:6.0
  2. WORKDIR /HelloWorld
  3. COPY . .
  4. EXPOSE 8000
  5. CMD ["/HelloWorld/DockerTest.exe"]

I've then built the image using:

  1. docker build -t imagename:tagname .

And then run the container by using:

  1. docker run --name containername -p 8000:8000 imagename:tagname

The container exits immediately with exit code 1, so a error, and it produces no logs.
What i think I could be doing wrong is using the wrong image for the .NET runtime
(is mcr.microsoft.com/dotnet/runtime:6.0 correct?) or forgetting something else in the Dockerfile. If you have any questions I'll be happy to give you more details

答案1

得分: 2

mcr.microsoft.com/dotnet/runtime:6.0 是基于 Debian 11 的 Linux 容器 - 在 docker hub 查看详细信息:

> 6.0.16-bullseye-slim-amd64, 6.0-bullseye-slim-amd64, 6.0.16-bullseye-slim, 6.0-bullseye-slim, 6.0.16, 6.0 Dockerfile Debian 11 05/03/2023

因此,它无法运行专为 Windows 构建的 .exe 文件。

您需要切换到 Windows 容器或更改命令以适用于 Debian,类似于:

  1. ENTRYPOINT ["dotnet", "DockerTest.dll"]

此外,您可以尝试使用 VS 工具来添加 Docker 支持。这将创建一个多阶段构建的 Docker 文件,用于处理构建和生成结果镜像。

附言:

  • 我建议创建一个 ASP.NET Core 项目(例如,使用 Minimal APIs 来简化),而不是直接使用 HttpListener

  • "http://localhost:8000/" 中的 localhost 意味着它将仅接受来自本地主机的请求,对于容器化应用程序来说,本地主机将是容器本身 - 请参阅此答案了解详细信息和解决方案。

英文:

mcr.microsoft.com/dotnet/runtime:6.0 is a Linux container based on Debian 11 - check out info at docker hub:

> 6.0.16-bullseye-slim-amd64, 6.0-bullseye-slim-amd64, 6.0.16-bullseye-slim, 6.0-bullseye-slim, 6.0.16, 6.0 Dockerfile Debian 11 05/03/2023

So it can't run .exe files which are build for Windows.

You need to either switch to Windows container or change command to a valid one for Debian, something like:

  1. ENTRYPOINT ["dotnet", "DockerTest.dll"]

Also you can try using VS tools to add Docker support. This should create a multistage build Docker file which will handle both building and producing the resulting image.

P.S.

  • Also I would recommend creating an ASP.NET Core project (using Minimal APIs for simplicity for example) instead of HttpListener directly.

  • localhost in "http://localhost:8000/" will mean that it will accept only requests from localhost which in case of dockerized application will be the container itself - see this answer for details and solutions.

huangapple
  • 本文由 发表于 2023年5月11日 17:35:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定