使用 Docker 构建应用程序时打开控制台窗口 [英] App opens console window when being build with Docker

查看:39
本文介绍了使用 Docker 构建应用程序时打开控制台窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 .Net Core WPF 应用程序在启动时自动打开控制台窗口的问题.这仅在在 Docker 容器内构建时发生.当我直接在我的 PC 上构建它时,只会打开实际的应用程序窗口.

I'm facing the issue that a .Net Core WPF application automatically opens a console window when started. This only happens when build inside a Docker container. When I build it directly on my PC, only the actual application window opens.

我最好的猜测是,这是 .Net Core 映像所基于的操作系统的问题..Net Core SDK Docker Hub Repo 知道以下标签:3.1-nanoserver-1809、3.1-nanoserver-1903、3.1-nanoserver-1909、3.1-nanoserver-2004、3.1-nanoserver-2009.我能够确认前三个标签的问题,但是 2004 和 2009 标签没有在我的机器上运行,所以我需要有人来试试这个,或者确认我的理论(这意味着它不应该发生在至少在这些图像中)或更好地解释为什么会发生这种情况.

My best guess is that this is an issue with the operating system the .Net Core image is based on. The .Net Core SDK Docker Hub Repo knows the following tags: 3.1-nanoserver-1809, 3.1-nanoserver-1903, 3.1-nanoserver-1909, 3.1-nanoserver-2004, 3.1-nanoserver-2009. I was able to confirm the issue with the first three tags, but the 2004 and 2009 tags do not run on my machine, so I need someone to try this out and either confirm my theory (which would mean that it should not happen on at least on of these images) or to come up with a better explanation of why this is happening.

这可以通过 Visual Studio 为您创建的默认 .Net Core WPF 应用程序重现.这是一个用于测试的 Dockerfile:

This is reproducible with the default .Net Core WPF app Visual Studio creates for you. Here is a Dockerfile to test it out:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src

COPY . ./
RUN dotnet build -c Debug -o out

FROM stefanscherer/chocolatey
WORKDIR /app

RUN choco install -y 7zip

# Depending on your project setup it might be src/[project name]/out
COPY --from=build /src/out ./test 

RUN 7z a -y test.zip ./test/*

您可以使用以下命令构建映像并提取已编译的程序:

You can build the image and extract the compiled program with the following commands:

docker build -t testimage .
docker run -d --name testcontainer testimage
docker stop testcontainer
docker cp testcontainer:app/test.zip .

推荐答案

我能够在 3.1-nanoserver-20093.1-nanoserver-2004 上重现这个问题> 给你.

I was able to reproduce this problem on both 3.1-nanoserver-2009 and 3.1-nanoserver-2004 for you.

我认为问题与构建期间打印的警告有关:

I think the problem is related to the warning printed out during build:

警告 NETSDK1074:应用程序主机可执行文件将不会被自定义,因为添加资源需要在 Windows(不包括 Nano Server)上执行构建.

warning NETSDK1074: The application host executable will not be customized because adding resources requires that the build be performed on Windows (excluding Nano Server).

如果是这样的话,那么似乎是nanoserver基础镜像的限制,不幸的是这个问题看起来仍然没有解决,因为在构建时它仍然存在<代码>mcr.microsoft.com/dotnet/nightly/sdk:5.0.

If that's the case, then it seems that it is a limitation of the nanoserver base image, and unfortunately it looks like this problem still has not been resolved, because it's still present when building in mcr.microsoft.com/dotnet/nightly/sdk:5.0.

这是一个相关的拉取请求,可能会对该主题有所了解.

Here's a related pull request that might shed some light on the subject.

话虽如此,我认为现在唯一的选择是使用 nanoserver 以外的 windows 映像(替代方法可以找到 此处).我没有找到任何预装 .NET Core SDK 的图像(虽然我没有花太多精力去寻找它),但设置它应该相当简单.在以下示例中,我使用了 servercore 图像,因为它比 windows 图像轻得多.

Having said that, I think the only option for now is to use windows image other than nanoserver (alternatives can be found here). I didn't find any image that would come with .NET Core SDK preinstalled (I didn't put much effort into finding it though), but it should be fairly simple to set it up. In the following example I used servercore image since it is much more lightweight than windows image.

FROM mcr.microsoft.com/windows/servercore:20H2 AS sdk
WORKDIR /dotnet
# Download the official .NET Core install script
RUN powershell -c "Invoke-WebRequest -Uri https://dot.net/v1/dotnet-install.ps1 -OutFile dotnet-install.ps1"
# Run the install script
RUN powershell -c "& ./dotnet-install.ps1 -InstallDir ."
# Add the installed executable to PATH
RUN setx PATH "%PATH%;/dotnet"

FROM sdk AS build
# Do your stuff here

在这里你会发现安装脚本的文档.

Here you'll find the documentation for the install script.

我还确认生成的应用程序在运行时没有生成控制台窗口.

I also did confirm that the produced application did not spawn console window when run.

这篇关于使用 Docker 构建应用程序时打开控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆