给.Net Core添加Docker文件支持和运行
1、添加一个Dockerfile文件,将其移到解决方案文件夹,模板如下:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore
# copy everything else and build app
COPY aspnetapp/. ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app/aspnetapp/out ./
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS Build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY *.sln .
COPY NetCore-Learn-LocalMsSql/*.csproj ./NetCore-Learn-LocalMsSql/
RUN dotnet restore
# copy everything else and build app
COPY NetCore-Learn-LocalMsSql/. ./NetCore-Learn-LocalMsSql/
WORKDIR /app/NetCore-Learn-LocalMsSql
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app/NetCore-Learn-LocalMsSql/out ./
ENTRYPOINT ["dotnet", "NetCore-Learn-LocalMsSql.dll"]
2、切换到解决方案文件夹,PS运行开始Build,注意需要小写。
docker build -t netcore-learn-localmsql .
3、Docker Images查看镜像创建情况
4、指定端口,开始运行。
docker run -it -p 3000:80 --name netcorelearnlocalmssql netcorelearnlocalmssql:latest