问题
我的问题是关于使用C#向Docker容器发送命令。在我程序的当前状态下,该命令不会在我的docker实例中执行。该Docker容器在Azure中运行。该图像与主题容器实例一起稳定运行。我的docker映像的基础是ubuntu。我正在使用名称为azure-libraries-for-net的nuget数据包。 docker实例的功能创建成功。我的程序具有所需的所有身份验证凭据。仅当我运行ExecuteCommmand函数时,我才会获得带有websocket url和password属性的ContainerExecResponse。该命令未在我的容器中执行,并且在那里什么也没有发生。 (我认为执行速度也太快)。如果我在Docker容器中手动运行命令,则该命令有效。
我的问题是:
我如何在docker中执行一个命令,该命令使用azure-libraries-for-net在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 \\”回声'root:hoi1234!' | chpasswd; / sbin / my_init \\“ \”:stat / bin / sh -c \“ echo'root:hoi1234!' | chpasswd; / 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"
在containergroup中,我为生成的密码添加了一个全局变量,并且启动命令将为:/startup.sh
我使用以下命令更改了docker中的脚本权限:chmod +x /startup.sh
在我的程序中,我添加了以下代码:
.WithEnvironmentVariable(envName, generatedPassword)
答案 0 :(得分:0)
旧线程,但对于某些仍然有用。 您可以使用以下语法使其起作用-
src/templates/
答案 1 :(得分:0)
在Suneet的回答中, .WithStartingCommandLine()可用于链接命令,如以下示例所示:
.WithStartingCommandLine("/bin/sh", "-c", "mkdir artifacts && cd artifacts && git clone https://$GIT_PWD@github.com/$REPO.git . && npm ci && npm test")