我创建了一个示例ASP.Net Core Web应用程序,并尝试通过Docker运行。 (Windows 7和Docker Toolbox安装)
这是我的Docker文件(为Linux VM配置)
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:5000
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["SampleDockerWebApp.csproj", "./"]
RUN dotnet restore "./SampleDockerWebApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "SampleDockerWebApp.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "SampleDockerWebApp.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "SampleDockerWebApp.dll"]
构建成功,并获得以下输出:
e:\Other Projects\SampleProjects\DockerSample\SampleDockerWebApp>docker build -t cog/aspnetcore .
Sending build context to Docker daemon 4.741MB
Step 1/16 : FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
---> ce06b36fcba4
Step 2/16 : WORKDIR /app
---> Using cache
---> 184385dc16fb
Step 3/16 : ENV ASPNETCORE_URLS=http://+:5000
---> Running in cc7cf2932649
Removing intermediate container cc7cf2932649
---> 33c2ffc6311d
Step 4/16 : FROM microsoft/dotnet:2.2-sdk AS build
---> a4974ac66bfc
Step 5/16 : WORKDIR /src
---> Using cache
---> 7f9a2990f973
Step 6/16 : COPY ["SampleDockerWebApp.csproj", "./"]
---> Using cache
---> 454d49d40a80
Step 7/16 : RUN dotnet restore "./SampleDockerWebApp.csproj"
---> Using cache
---> 004067a08191
Step 8/16 : COPY . .
---> Using cache
---> e592a6eaf72d
Step 9/16 : WORKDIR "/src/."
---> Using cache
---> 0429435802ea
Step 10/16 : RUN dotnet build "SampleDockerWebApp.csproj" -c Release -o /app
---> Using cache
---> be154237597e
Step 11/16 : FROM build AS publish
---> be154237597e
Step 12/16 : RUN dotnet publish "SampleDockerWebApp.csproj" -c Release -o /app
---> Using cache
---> 0d0e5b7d2fba
Step 13/16 : FROM base AS final
---> 33c2ffc6311d
Step 14/16 : WORKDIR /app
此后,我运行 Docker Run 命令创建一个容器
e:\ Other Projects \ SampleProjects \ DockerSample \ SampleDockerWebApp> docker run -d -p 8080:5000 cog / aspnetcore
在这里,容器创建成功,我可以看到以下日志:
E:\Other Projects\SampleProjects\DockerSample\SampleDockerWebApp>docker logs -f
a67a1d48f6a0089c4a6e92c2b56a095203290b09c679db172fb2b955bcfd424a
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {77e3802b-efee-4a06-8daa-831a0e0ef771} may be persisted to storage in unencrypted form.
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.
根据日志,容器已准备就绪,正在听 http://[::]:5000 。但是,当我尝试使用 http://localhost:5000 或 http://localhost:8080 浏览网站时,两个URL均无法正常工作。