dotnet / Docker.DotNet

:whale: .NET (C#) Client Library for Docker API
https://www.nuget.org/packages/Docker.DotNet/
MIT License
2.23k stars 381 forks source link

Can't execute command with bash shell #637

Open dsaffie opened 1 year ago

dsaffie commented 1 year ago

I am trying to automate running commands with docker exec. I have an example of a command that works from the command line, but can't get working in Docker.DotNet. With my container already running I opened a command line and ran: docker exec -it containerId bash -c "apt-get update". This command is successful and I can see the output from the command in the terminal window.

Then, I tried doing the same as:

ContainerExecCreateParameters execParams = new()
{
    AttachStderr = true,
    AttachStdout = true,
    Cmd = new[] { "//bin//bash", "apt-get update" }
};

var exec = await this.dockerClient.Exec.ExecCreateContainerAsync(containerId, execParams);
var stream = await this.dockerClient.Exec.StartAndAttachContainerExecAsync(exec.ID, false);
var output = await stream.ReadOutputToEndAsync(new CancellationToken());

Instead of getting the exepected output of apt-get update, I get the error message: (OCI runtime exec failed: exec failed: unable to start container process: exec: "apt-get update": executable file not found in $PATH: unknown , ).

I believe this error may be due to trying to execute the command not in the bash shell, but haven't found any examples or docs that demonstrate how to properly execute a command in the bash shell using Docker.DotNet.

HofmeisterAn commented 1 year ago

Your are using two different commands (syntax) try:

Cmd = new[] { "/bin/bash", "-c", "apt-get update" }
harry-123 commented 4 months ago

I have the same issue. I need to run the multiple commands from the bash, but Cmd = new[] { "/bin/bash", "-c", "apt-get update" }

doesn't seems to be working.

hakimdotdev commented 4 months ago

I have the same issue. I need to run the multiple commands from the bash, but Cmd = new[] { "/bin/bash", "-c", "apt-get update" }

doesn't seems to be working.

What is the response you get? What are the commands you're trying to run?

Cmd = new[] { "/bin/bash", "-c", "apt-get update" }

Works for me. The only point it fails is from a certain complexity or number of piped commands.