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

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

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

问题

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

我已成功创建了一个使用Node和React的工作容器。
现在我尝试创建一个Docker容器来运行一个简单的C#可执行文件,该文件将侦听端口8000并返回“hello world”。如果我在本地访问http://localhost:8000/运行它,它可以工作。
以下是代码:

using System;
using System.Net;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        string url = "http://localhost:8000/";
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add(url);
        listener.Start();

        Console.WriteLine("Listening on {0}", url);

        while (true)
        {
            HttpListenerContext context = listener.GetContext();
            HttpListenerRequest request = context.Request;
            HttpListenerResponse response = context.Response;

            Console.WriteLine("{0} {1}", request.HttpMethod, request.Url);

            byte[] buffer = Encoding.UTF8.GetBytes("Hello, World!");
            response.ContentLength64 = buffer.Length;
            response.OutputStream.Write(buffer, 0, buffer.Length);
            response.OutputStream.Close();
        }
    }
}

我已经构建了该程序并将其移动到桌面的一个文件夹中:

如您所见,我已创建了Dockerfile,其中包含以下内容:

FROM mcr.microsoft.com/dotnet/runtime:6.0

WORKDIR /HelloWorld

COPY .

EXPOSE 8000

CMD ["/HelloWorld/DockerTest.exe"]

然后,我使用以下命令构建了镜像:

docker build -t imagename:tagname .

然后通过以下命令运行容器:

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

容器立即退出并显示退出代码1,表示出现错误,但没有生成日志。
我认为可能出错的地方是使用了错误的.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:

using System;
using System.Net;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        string url = "http://localhost:8000/";
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add(url);
        listener.Start();

        Console.WriteLine("Listening on {0}", url);

        while (true)
        {
            HttpListenerContext context = listener.GetContext();
            HttpListenerRequest request = context.Request;
            HttpListenerResponse response = context.Response;

            Console.WriteLine("{0} {1}", request.HttpMethod, request.Url);

            byte[] buffer = Encoding.UTF8.GetBytes("Hello, World!");
            response.ContentLength64 = buffer.Length;
            response.OutputStream.Write(buffer, 0, buffer.Length);
            response.OutputStream.Close();
        }
    }
}

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:

FROM mcr.microsoft.com/dotnet/runtime:6.0      

WORKDIR /HelloWorld

COPY . .

EXPOSE 8000

CMD ["/HelloWorld/DockerTest.exe"]

I've then built the image using:

docker build -t imagename:tagname .

And then run the container by using:

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,类似于:

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:

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:

确定