使用powshell构建使.NetCore控制台程序的docker镜像

首先你要确保自己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文件不存在

具体原因就是构建时发现用到的文件在你当前目录没有

有疑问可以咨询我,我愿意详细解答

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个月太忙没时间看C++

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值