简体   繁体   English

如何通过 docker 运行将命令行 Arguments 传递到 .NET 控制台应用程序(Docker for Windows)

[英]How to pass Command Line Arguments to .NET Console App through docker run (Docker for Windows)

I'm quite new in Docker word and I would like to pass arguments to my script from docker run.我对 Docker 字很陌生,我想将 arguments 从 docker 运行传递给我的脚本。 My C# code looks like:我的 C# 代码如下所示:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello: " + args[0]);
        }
    }

And my Docker file is the following:我的 Docker 文件如下:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build
WORKDIR /app

COPY MediumDemo.csproj .
RUN dotnet restore MediumDemo.csproj

COPY . .
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/runtime:3.1 as runtime
WORKDIR /app
COPY --from=build /app/out ./

ENTRYPOINT ["dotnet", "Demo.dll"]

This is old, but for anyone who might be looking at it, you have to combine ENTRYPOINT with CMD in your Dockerfile, as follows:这是旧的,但对于任何可能正在查看它的人,您必须在 Dockerfile 中将 ENTRYPOINT 与 CMD 结合起来,如下所示:

ENTRYPOINT [ "dotnet", "Demo.dll" ]
CMD [ "arg0" ]

Then run it:然后运行它:

docker run -it Demo myarg

And you should see the output you expect.您应该会看到您期望的 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将命令行参数传递给Visual Studio容器工具中.NET Core控制台应用程序的Docker调试运行 - Pass command line args to a Docker debugging run of a .NET Core console application in Visual Studio Container Tools 添加应用程序参数以在Docker中运行.NET控制台应用程序 - Add application arguments to run .NET console app in Docker 如何将命令行参数传递给docker run并打印? - How to pass command line argument to docker run and print it? 运行 dot net core 控制台的映像时如何在启动时将参数传递给 docker 容器 - How to pass arguments to a docker container at start when running an image of dot net core console 如何从命令行运行 Visual Studio 生成的 ASP.NET Core 示例 Web 应用 Docker 映像? - How to run Visual Studio generated ASP.NET Core Sample Web App Docker image from command line? .NET控制台应用程序命令行解析文件参数 - .NET Console app command line parsing of file arguments .Net Core 控制台应用程序未在 Docker 容器中运行 - .Net Core Console App not running in Docker container 如何使用别名从命令行运行 dotnet 控制台应用程序 - How to run a dotnet console app from the command line using an alias 如何在 Docker 中运行 .NET Core 3.1.3 x86 应用程序 - How to run a .NET Core 3.1.3 x86 App in Docker 如何在.net Windows应用程序中传递命令行参数 - How to pass command line argument in .net windows application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM