简体   繁体   中英

Run docker-compose command from C# .Net 4.7

I'm wishing to run a command from C# to a container set up via docker-compose. In Powershell, I run this command and the file is created:

docker-compose exec database sh -c "mysqldump -u((username)) -p((password)) ((databasename)) > /backups/test.sql"

When I run the following code it seems to ignore the environment variables, even though I have them set. It only creates a file named backup.sql and the SQL outputted to the file indicates that no database was selected. I've verified the env variables are set by outputting the last parameters string to the console.

var exportPath = $"/backups/backup.sql {DateTime.Now}"; 
using (var runspace = RunspaceFactory.CreateRunspace())
{
    runspace.Open();
    runspace.SessionStateProxy.Path.SetLocation(Path.GetFullPath(".."));
    using (var pipeline = runspace.CreatePipeline())
    {

        var cmd = new Command("docker-compose");
        cmd.Parameters.Add("exec");
        cmd.Parameters.Add("database");
        cmd.Parameters.Add($"sh -c \"mysqldump - u{GetEnv("MYSQL_USERNAME")} " +
            $"-p{GetEnv("MYSQL_PASSWORD")} {GetEnv("MYSQL_DATABASE")} > {exportPath} \"");
        pipeline.Commands.Add(cmd);
        pipeline.Invoke();
    }
}

GetEnv is just a convenience method for Environment.GetEnvironmentVariable

I'm fairly certain that I am not setting the parameters right, but I don't know where to go from here.

I gave up and used CliWrap because it is easier to debug. I couldn't figure out how to set the current directory, but fortunately I could modify the rest of the program to look for stuff in the current directory.

var now = DateTime.Now.ToString().Replace("/", "-").Replace(" ", "-");
var exportPath = $"/backups/backup.sql-{now}";

using (var cli = new CliWrap.Cli("docker-compose"))
{
    var cmd = $"exec -T database sh -c \"mysqldump -u{GetEnv("MYSQL_USER")} " +
        $"-p{GetEnv("MYSQL_PASSWORD")} {GetEnv("MYSQL_DATABASE")} > {exportPath}\"";
    Console.WriteLine(cmd);
    var resp = cli.Execute(cmd);
    Console.WriteLine(resp.StandardOutput);
    Console.WriteLine(resp.StandardError);
}

The library can be found here: https://github.com/Tyrrrz/CliWrap

I came across to FluentDocker which seems like a nice way to run docker-compose from c#.

Here is an example from the project page:

using (
        var container =
          new Builder().UseContainer()
            .UseImage("kiasaki/alpine-postgres")
            .ExposePort(5432)
            .WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
            .WaitForPort("5432/tcp", 30000 /*30s*/)
            .Build()
            .Start())
      {
        var config = container.GetConfiguration(true);
        Assert.AreEqual(ServiceRunningState.Running, config.State.ToServiceState());
      }

Disclaimer: I am not affiliated with the project in any way nor have attempted to use it yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM