我可以使用ASP.NET核心构建一个简单的REST API端点:
$ rm -rf PPP && mkdir PPP && cd PPP # <--- make sure dir is empty
$ dotnet new web 1>/dev/null
$ dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5270
# ... omitted other info ...
当我发送http GET
请求时,一切正常:
$ curl http://localhost:5270/
Hello World! # <--- good ! (the default response generate in `dotnet new web`)
当我试着按照this guide来"停靠"它时,它不起作用:
# Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:8.0
RUN mkdir Properties
COPY Program.cs Program.cs
COPY PPP.csproj PPP.csproj
COPY appsettings.json appsettings.json
COPY appsettings.Development.json appsettings.Development.json
COPY Properties/launchSettings.json Properties/launchSettings.json
EXPOSE 5270
CMD ["dotnet", "run"]
下面是我如何构建和运行docker映像
$ docker build --tag host --file Dockerfile .
$ docker run -p 8000:5270 -d -t --name scfrontcs host
下面是我得到的错误:
$ curl http://localhost:8000/
curl: (52) Empty reply from server
我做错了什么?
EDIT个
- 我在我的MacOS上运行
8.0
遇到了困难,所以我把图像降到了7.0
- 我现在可以从within号 docker 发送
GET
个请求 - 但有not个来自外部(从我本地的机器)
$ cat Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:7.0
WORKDIR /App
COPY Program.cs Program.cs
COPY scfrontcs.csproj scfrontcs.csproj
COPY appsettings.json appsettings.json
EXPOSE 5000
# CMD ["dotnet", "run"] <--- commented, I will run it myself
$ docker build --tag host --file Dockerfile .
$ docker run -p 8000:5000 -d -t --name scfrontcs host
$ docker exec -it scfrontcs bash
root@fca567cab821:/App# dotnet run & # inside docker
[1] 97
root@fca567cab821:/App# info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5000
# ... omitted info ...
curl http://localhost:5000
Hello World # <--- works from within docker !