首先你要确保自己pc安装了netcore的sdk,docker具体安装步骤我不介绍了
另外熟悉docker基本命令
构建镜像 docker build -t myconsole/v1 .
运行 docker run -d --name my myconsole/v1 .
查看容器日志 docker logs --since 30m my
一、创建控制台程序(不做介绍)
二、容器业务流程协调程序支持
选择确定这个时候会出现
这个时候需要修改Dokerfile文件
这里的路径一定要跟自己的对照好,我也是这里卡了好几天,一直搞不对
- FROM:指定要使用的docker镜像,在第1行中我们指定了用于运行应用程序的映像。
- WORKDIR:指定image的工作目录。我们这里使用/ app作为我们的运行程序目录、/src编译目录。
- COPY:将文件从本地文件系统复制到映像中。第一个COPY是将本地的csproj文件复制到image中,第二个COPY是将当前所有文件复制到image中。 复制我们将最初复制csproj文件并运行restore然后复制所有剩余文件并运行dotnet publish来构建我们的应用程序。
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-nanoserver-1809 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 AS build
WORKDIR /src
COPY ["ConsoleAppDockerWindows2.csproj", "ConsoleAppDockerWindows2/"]
RUN dotnet restore "ConsoleAppDockerWindows2/ConsoleAppDockerWindows2.csproj"
COPY . .
WORKDIR "/src"
RUN dotnet build "ConsoleAppDockerWindows2.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ConsoleAppDockerWindows2.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ConsoleAppDockerWindows2.dll"]
然后就是使用powshell 命令构建镜像了
1、cd到当前的Dokerfile目录
2、使用构建命令docker build -t myconsole/v1 .
3、 docker run -d --name my myconsole/v1 .
4、查看日志 看下是否允运行成功
我强调一下我发现的问题吧
如果dockerfile中路径改的不对会出现几种情况
第一种:
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [C:\src\ConsoleAppDockerWindows2\ConsoleAppDockerWindows2.csproj]
Build FAILED.
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [C:\src\ConsoleAppDockerWindows2\ConsoleAppDockerWindows2.csproj]
0 Warning(s)
1 Error(s)
第二种:
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\app'.
Failed to run as a self-contained app. If this should be a framework-dependent app, add the C:\app\app5.runtimeconfig.json file specifying the appropriate framework.
第三种:
就是XX/XX文件不存在
具体原因就是构建时发现用到的文件在你当前目录没有
有疑问可以咨询我,我愿意详细解答