dotnet / Docker.DotNet

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

Basic scenario of running SQL Server 2017 #270

Open erick-thompson opened 6 years ago

erick-thompson commented 6 years ago

I'm trying to use this library to start a Linux based SQL Server 2017 instance and restore a database to it (this is for integration tests). It's not clear from readme how to achieve this. I've been poking around the various types, but the code version of:

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" --name "sql1" -p 1401:1433 -v sql1data:/var/opt/mssql ` -d microsoft/mssql-server-linux:2017-latest

isn't clear at all. The next thing is to restore the database via docker exec. Using this API, how would I achieve these tasks? Even knowing what types to use would be a big help. Thanks!

https://docs.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container

jterry75 commented 6 years ago

Well via the REST API's a docker run is really

static async Task MainAsync()
{
    using (var client = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine")).CreateClient())
    {
        var container = await client.Containers.CreateContainerAsync(
            new CreateContainerParameters
            {
                Name = "sql1",
                Image = "microsoft/mssql-server-linux:2017-latest",
                Env = new string[]{ "ACCEPT_EULA=y", "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd" },
                HostConfig = new HostConfig
                {
                    PortBindings = new Dictionary<string, IList<PortBinding>>
                    {
                        {
                            "1433/tcp",
                            new PortBinding[]
                            {
                                new PortBinding
                                {
                                    HostPort = "1401"
                                }
                            }
                        }
                    }
                }
            });

        /*  // If you want the output uncomment.
        var stream = await client.Containers.AttachContainerAsync(
            container.ID,
            false, // tty
            new ContainerAttachParameters
            {
            },
            default(CancellationToken));

        stream.CopyOutputToAsync(stdin, stdout, stderr, default(CancellationToken));
        */

        if (await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters()))
        {
            // Proceed with your exec.
        }

        await client.Containers.WaitContainerAsync(container.ID, default(CancellationToken));
    }
}

I hope this helps! If you need more insights I would recommend reading the Docker REST documentation here.

erick-thompson commented 6 years ago

This does help, thank you!

jehof commented 6 years ago

The portbinding in the example above has a small glitch. It needs to be "1433/tcp" instead of "1443/tcp"

PortBindings = new Dictionary<string, IList<PortBinding>>{
  {
    "1433/tcp",
    new PortBinding[]
    {
      new PortBinding
      {
        HostPort = "1401"
      }
    }
  }
}
jterry75 commented 6 years ago

@jehof - Thanks for catching the mistake. I updated my comment so they match.