title: Execute command in docker on azure with azure-libraries-for-net
body: <p><strong>Problem</strong></p>
  
  <p>My question is about sending a command to a docker container with C#. In the current state of my program, the command doesn't execute in my docker instance. This docker container runs in Azure. The image runs stable native with the subject container instances. The base of my docker image is ubuntu. I'm using the nuget packets with name <a href="https://github.com/Azure/azure-libraries-for-net" rel="nofollow noreferrer">azure-libraries-for-net</a>. The function creation of the docker instance works. My program has all the authentication credentials what it needs. Only if I run the ExecuteCommmand functions, then I got a ContainerExecResponse back with a websocket url and password property. The command doesn't execute in my container and nothing happens there. (The execution is also too fast i think). If I run the command manually in the docker container, then the command works.  </p>
  
  <p><strong>My question is:</strong></p>
  
  <p>How can I execute a command in docker what runs in azure with azure-libraries-for-net? </p>
  
  <p><strong>Context</strong><br>
  I want to change a password for a system account with a random password in the docker instance. That password is only in the memory of my program. When my program doesn't need the docker anymore, then my program stops automatically the docker instance. The last function works also.     </p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>I searched for documentation or question about this problem, but i couldn't find any of it. </p>
  
  <p>Except:
  <a href="https://docs.microsoft.com/en-us/dotnet/api/overview/azure/containerinstance?view=azure-dotnet" rel="nofollow noreferrer">Microsoft Doc - Container Instances Dotnet</a></p>
  
  <p><strong>Edit:</strong></p>
  
  <p>When I give this string in .WithStartingCommandLine():</p>
  
  <pre><code>string startupCommand = "/bin/sh -c \"echo 'root:hoi1234!' | chpasswd; /sbin/my_init\"";
  </code></pre>
  
  <p>Then I got this error back:</p>
  
  <p>Error: failed to start container "imago-sp-1": Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"/bin/sh -c \\"echo 'root:hoi1234!' | chpasswd; /sbin/my_init\\"\": stat /bin/sh -c \"echo 'root:hoi1234!' | chpasswd; /sbin/my_init\": no such file or directory"</p>
  
  <p>When I put this command in docker locally then it works:</p>
  
  <pre><code>docker run -p 26:22 image /bin/sh -c "echo 'root:hoi1234!' | chpasswd; /sbin/my_init"
  </code></pre>
  
  <p><strong>Edit 2: (My solution now)</strong></p>
  
  <p>I have now a temporarily solution. I made a premade shell script for the startup and I gave  the docker a global variable for the input.</p>
  
  <p>The shell script:</p>
  
  <pre><code>#!/bin/bash
  echo 'root:'${RandomPassword}'' | chpasswd
  "/sbin/my_init"
  </code></pre>
  
  <p>At containergroup I added a global variable for a generated password and the startup command will be: <code>/startup.sh</code> I changed the rights for the script in docker with: <code>chmod +x /startup.sh</code></p>
  
  <p>In my program I added this piece of code:</p>
  
  <pre><code>.WithEnvironmentVariable(envName, generatedPassword)
  </code></pre>