Running Docker Exec containing a quote #3157
Answered
by
gitfool
pascalberger
asked this question in
Extension Q&A
-
I try to run a docker exec 8e233d28502a36f6490439c9fd68e8f49a29c8de548ae820618cdb7f25587b58 sh -c 'terraform validate -json > /terraform/dist/logs/validate.json' I tried the following in my build script: context.DockerExec(
containerId,
"sh -c 'terraform validate -json > /terraform/dist/logs/validate.json'"); Which ends with:
|
Beta Was this translation helpful? Give feedback.
Answered by
gitfool
Feb 17, 2021
Replies: 2 comments 8 replies
-
Try splitting the arguments from the command? (Try both with and without the single quotes.) context.DockerExec(
containerId,
"sh", "-c", "terraform validate -json > /terraform/dist/logs/validate.json"); |
Beta Was this translation helpful? Give feedback.
8 replies
Answer selected by
pascalberger
-
This is a workaround using IEnumerable<string> dockerToolName;
if (context.Environment.Platform.IsUnix())
{
dockerToolName = new[] { "docker", "docker.exe" };
}
else
{
dockerToolName = new[] { "docker.exe", "docker" };
}
var dockerPath = context.Tools.Resolve(dockerToolName);
IEnumerable<string> dockerOutput;
var exitCode =
context.StartProcess(
dockerPath,
new ProcessSettings {
Arguments =
new ProcessArgumentBuilder()
.Append("exec")
.Append(containerId)
.Append("terraform")
.Append("validate")
.Append("-json"),
RedirectStandardOutput = true,
},
out dockerOutput);
if (exitCode != 0)
{
throw new System.Exception($"Docker exec failed. Exit code: {exitCode}");
}
var logDirectoryPath = context.RepositoryRootPath.Combine("dist").Combine("logs");
var logFilePath = logDirectoryPath.CombineWithFilePath("validate.json");
context.EnsureDirectoryExists(logDirectoryPath);
if (context.FileExists(logFilePath))
{
context.DeleteFile(logFilePath);
}
File.WriteAllLines(logFilePath.FullPath, dockerOutput); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try splitting the arguments from the command? (Try both with and without the single quotes.)