【发布时间】:2019-04-26 04:04:07
【问题描述】:
问题
我的问题是关于使用 C# 向 docker 容器发送命令。在我的程序的当前状态下,该命令不会在我的 docker 实例中执行。此 docker 容器在 Azure 中运行。图像使用主题容器实例在本地稳定运行。我的 docker 镜像的基础是 ubuntu。我正在使用名称为 azure-libraries-for-net 的 nuget 数据包。 docker 实例的函数创建工作。我的程序具有所需的所有身份验证凭据。只有当我运行 ExecuteCommmand 函数时,我才会得到一个带有 websocket url 和密码属性的 ContainerExecResponse。该命令不在我的容器中执行,并且没有任何反应。 (我认为执行也太快了)。如果我在 docker 容器中手动运行该命令,则该命令有效。
我的问题是:
如何使用 azure-libraries-for-net 在 docker 中执行在 azure 中运行的命令?
上下文
我想在 docker 实例中使用随机密码更改系统帐户的密码。该密码仅在我的程序的内存中。当我的程序不再需要 docker 时,我的程序会自动停止 docker 实例。最后一个功能也有效。
using Microsoft.Azure.Management.ContainerInstance.Fluent;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
public async Task DeployDocker(string startupCommand, string runningCommand)
{
IAzure azure = Azure.Authenticate(credFile).WithDefaultSubscription();
IResourceGroup resGroup = azure.ResourceGroups.GetByName(DockerSetting.ResourceGroup);
Region azureRegion = resGroup.Region;
string containerName = DockerSetting.ContainerGroup + "-1";
// Create the container group
var containerGroup = azure.ContainerGroups.Define(DockerSetting.ContainerGroup)
.WithRegion(azureRegion)
.WithExistingResourceGroup(DockerSetting.ResourceGroup)
.WithLinux()
.WithPublicImageRegistryOnly()
.WithoutVolume()
.DefineContainerInstance(containerName)
.WithImage(DockerSetting.Image)
.WithExternalTcpPort(DockerSetting.Port)
.WithCpuCoreCount(1.0)
.WithMemorySizeInGB(1)
.WithStartingCommandLine(startupCommand)
.Attach()
.WithDnsPrefix(DockerSetting.ContainerGroup)
.Create();
var result = await containerGroup.ExecuteCommandAsync(containerName, runningCommand, runningCommand.Length, 1);
}
我搜索了有关此问题的文档或问题,但找不到任何内容。
除了: Microsoft Doc - Container Instances Dotnet
编辑:
当我在 .WithStartingCommandLine() 中给出这个字符串时:
string startupCommand = "/bin/sh -c \"echo 'root:hoi1234!' | chpasswd; /sbin/my_init\"";
然后我得到了这个错误:
错误:无法启动容器“imago-sp-1”:来自守护进程的错误响应:oci 运行时错误:container_linux.go:247:启动容器进程导致“exec:\”/bin/sh -c \\”回声“根:hoi1234!” | chpasswd; /sbin/my_init\\"\": stat /bin/sh -c \"echo 'root:hoi1234!' |密码; /sbin/my_init\": 没有这样的文件或目录"
当我将此命令放在本地 docker 中时,它可以工作:
docker run -p 26:22 image /bin/sh -c "echo 'root:hoi1234!' | chpasswd; /sbin/my_init"
编辑 2:(我现在的解决方案)
我现在有一个临时解决方案。我为启动制作了一个预制的 shell 脚本,并为 docker 提供了一个全局变量作为输入。
shell脚本:
#!/bin/bash
echo 'root:'${RandomPassword}'' | chpasswd
"/sbin/my_init"
在容器组中,我为生成的密码添加了一个全局变量,启动命令将是:/startup.sh 我在 docker 中更改了脚本的权限:chmod +x /startup.sh
在我的程序中我添加了这段代码:
.WithEnvironmentVariable(envName, generatedPassword)
【问题讨论】:
-
+1 用于提出与编程和开发有关的 Azure 问题,甚至还有一些代码。
-
您是否尝试添加重启策略并将startupCommand拆分为文档显示的串行字符串?
-
先添加重启策略并不能解决问题。其次,我只有一个 .WithStartingCommandLine 和 .WithStartingCommandLines 不存在。 .WithStartingCommandLine 仅接受普通字符串。我现在正在尝试在一个字符串中创建两个命令: /bin/sh -c "echo 'root:hoi1234!' | chpasswd;/sbin/my_init"。该命令适用于我的本地 docker,但这不适用于 azure。
标签: c# linux azure docker .net-core