运行 定义端口上的 RC2 项目
Running RC2 project on defined port
好的,我创建了空的 RC2 项目,运行在本地使用 VS 2015 就可以了。
现在我想使用 docker 将其部署到 linux 服务器 - 那么我的 docker 文件应该如何显示?我一直在关注 these instructions,这就是我的结局:
FROM microsoft/dotnet:1.0.0-preview1
COPY . /app
WORKDIR /app
RUN dotnet restore
EXPOSE 5004
ENTRYPOINT dotnet run
然后我将我的应用程序构建为图像:
docker build -t my_app .
和 运行 使用:
docker 运行 -t -p 8080:5004 my_app
在那之后我得到信息说图像是 运行ning 并且它正在侦听 localhost:5000。不幸的是,我一直在尝试使用这些地址中的 xxxx:5000、xxxx:5004 和 xxxx:8080 和 none 连接到该服务器(xxxx 是服务器地址)。
我是不是做错了什么?
您可以使用 UseUrls()
扩展方法告诉 kestrel 监听哪个端口,如下所示:
(这对我来说通常是 Program.Main()
入口点方法)
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls("http://0.0.0.0:5004")
.Build();
host.Run();
在这种情况下,您会 运行 像这样的 docker 图片:
$ docker run -d -p 8080:5004 my_app
我选择 -d
选项 运行 作为守护进程。只需确保 Dockerfile 中的 EXPOSED 端口与 UseUrls
中指定的端口相匹配。有关此的完整示例,请随时查看我的 github 示例项目:https://github.com/mw007/adventure-works
您还可以在 Dockerfile 级别指定 Urls(如果您想重用 Container 则更好)。这是完整的 Dockerfile:
FROM microsoft/dotnet
RUN printf "deb http://ftp.us.debian.org/debian jessie main\n" >> /etc/apt/sources.list
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 5000/tcp
ENTRYPOINT ["dotnet", "run", "--server.urls=http://0.0.0.0:5000"]
您还需要修改Program.cs文件以从主要参数读取配置:
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
您有分步教程以及为什么在此博客中 post:
https://www.sesispla.net/blog/language/en/2016/05/running-asp-net-core-1-0-rc2-in-docker/
好的,我创建了空的 RC2 项目,运行在本地使用 VS 2015 就可以了。
现在我想使用 docker 将其部署到 linux 服务器 - 那么我的 docker 文件应该如何显示?我一直在关注 these instructions,这就是我的结局:
FROM microsoft/dotnet:1.0.0-preview1
COPY . /app
WORKDIR /app
RUN dotnet restore
EXPOSE 5004
ENTRYPOINT dotnet run
然后我将我的应用程序构建为图像: docker build -t my_app .
和 运行 使用: docker 运行 -t -p 8080:5004 my_app
在那之后我得到信息说图像是 运行ning 并且它正在侦听 localhost:5000。不幸的是,我一直在尝试使用这些地址中的 xxxx:5000、xxxx:5004 和 xxxx:8080 和 none 连接到该服务器(xxxx 是服务器地址)。
我是不是做错了什么?
您可以使用 UseUrls()
扩展方法告诉 kestrel 监听哪个端口,如下所示:
(这对我来说通常是 Program.Main()
入口点方法)
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls("http://0.0.0.0:5004")
.Build();
host.Run();
在这种情况下,您会 运行 像这样的 docker 图片:
$ docker run -d -p 8080:5004 my_app
我选择 -d
选项 运行 作为守护进程。只需确保 Dockerfile 中的 EXPOSED 端口与 UseUrls
中指定的端口相匹配。有关此的完整示例,请随时查看我的 github 示例项目:https://github.com/mw007/adventure-works
您还可以在 Dockerfile 级别指定 Urls(如果您想重用 Container 则更好)。这是完整的 Dockerfile:
FROM microsoft/dotnet
RUN printf "deb http://ftp.us.debian.org/debian jessie main\n" >> /etc/apt/sources.list
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 5000/tcp
ENTRYPOINT ["dotnet", "run", "--server.urls=http://0.0.0.0:5000"]
您还需要修改Program.cs文件以从主要参数读取配置:
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
您有分步教程以及为什么在此博客中 post: https://www.sesispla.net/blog/language/en/2016/05/running-asp-net-core-1-0-rc2-in-docker/