public async Task Setup()
        {
            using (var config = DockerClientConfigurationFactory.Create())
                using (var client = config.CreateClient())
                {
                    var containers = await client.Containers.ListContainersAsync(new ContainersListParameters()
                    {
                        All = true
                    });

                    var container = containers.FirstOrDefault(c => c.Names.Contains("/" + ContainerName));

                    if (container == null)
                    {
                        await CreateImage(client);

                        container = await CreateContainer(client);
                    }

                    this.ContainerId = container.ID;

                    if (container.State != "running")
                    {
                        var started = await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                        if (!started)
                        {
                            Assert.Fail("Cannot start the docker container");
                        }
                    }

                    await WaitForContainerReady();
                }
        }
Пример #2
0
        public static async Task <long> Execute(string containerId)
        {
            // container may be up but postgres might not quite be fully started yet
            using (var config = DockerClientConfigurationFactory.Create())
                using (var client = config.CreateClient())
                {
                    var createArgs = new ContainerExecCreateParameters
                    {
                        AttachStdin  = true,
                        AttachStdout = true,
                        Cmd          = new List <string>()
                        {
                            "pg_isready",
                            // "-h blah" // to test failures
                        },
                        Tty          = true,
                        AttachStderr = true,
                        Detach       = true
                    };

                    var created = await client.Containers.ExecCreateContainerAsync(containerId, createArgs);

                    await client.Containers.StartContainerExecAsync(created.ID);

                    // pg_isready returns 0 to the shell if the server is accepting connections normally,
                    // 1 if the server is rejecting connections (for example during startup),
                    // 2 if there was no response to the connection attempt,
                    // and 3 if no attempt was made (for example due to invalid parameters).
                    var result = await client.Containers.InspectContainerExecAsync(created.ID);

                    return(result.ExitCode);
                }
        }
Пример #3
0
        public static async Task <long> Execute(string containerId, string filename, string logFilename)
        {
            Trace.WriteLine($"Initializing docker client");

            using (var config = DockerClientConfigurationFactory.Create())
                using (var client = config.CreateClient())
                {
                    var createArgs = new ContainerExecCreateParameters
                    {
                        AttachStdin  = true,
                        AttachStdout = true,

                        // psql -U postgres -f home/path/script.sql -v ON_ERROR_STOP=1
                        Cmd = new List <string>()
                        {
                            //"bash",
                            //"/usr/lib/postgresql/11/bin/psql",
                            "touch /home/test.txt",
                            //"-U postgres",
                            //@"-c \'l'"
                            //"-e",
                            //$"-f {filename}",
                            //$"-L {logFilename}",
                            //"-v",
                            //"ON_ERROR_STOP=1"
                        },
                        Tty          = false,
                        AttachStderr = true,
                        Detach       = false,
                        //Privileged = true
                    };

                    Trace.WriteLine($"exec create for container {containerId}");
                    var created = await client.Containers.ExecCreateContainerAsync(containerId, createArgs);

                    Trace.WriteLine($"starting container execution for created id {created.ID}");
                    await client.Containers.StartContainerExecAsync(created.ID);

                    ContainerExecInspectResponse result;

                    do
                    {
                        /*
                         * psql returns 0 to the shell if it finished normally,
                         * 1 if a fatal error of its own occurs (e.g. out of memory, file not found),
                         * 2 if the connection to the server went bad and the session was not interactive,
                         * and 3 if an error occurred in a script and the variable ON_ERROR_STOP was set.
                         */
                        result = await client.Containers.InspectContainerExecAsync(created.ID);

                        Trace.WriteLine($"Inspect result => Pid: {result.Pid}, running: {result.Running}, exit code: {result.ExitCode}");

                        var logArgs = new ContainerLogsParameters
                        {
                            ShowStderr = true,
                            ShowStdout = true
                        };

                        using (var logStream = await client.Containers.GetContainerLogsAsync(containerId, logArgs))
                            using (var reader = new StreamReader(logStream))
                            {
                                var logs = reader.ReadToEnd();
                                Trace.WriteLine(logs);
                            }

                        await Task.Delay(250);
                    } while (result.Running);

                    return(result.ExitCode);
                }
        }