Exemplo n.º 1
0
        /// <summary> Starts a container and optionally waits for a readyness log phrase. </summary>
        /// <param name="options">Options</param>
        /// <returns>Docker container</returns>
        public static DockerContainer StartAndWaitForReadynessLogPhrase(DockerContainerOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(options.Name))
            {
                throw new ArgumentException("options.Name must not be null or empty.");
            }

            if (options.ImageName == null)
            {
                throw new ArgumentNullException("options.ImageName");
            }

            var theDockerArguments = new StringBuilder($"run --detach --name ").Append(options.Name);

            if (options.Hostname != null)
            {
                theDockerArguments.Append($" --hostname {options.Hostname}");
            }

            foreach ((int hostPort, int containerPort) in options.Ports)
            {
                theDockerArguments.Append($" --publish {hostPort}:{containerPort}");
            }

            foreach ((string key, string value) in options.EnvironmentVariables)
            {
                theDockerArguments.Append($" -e \"{key}={value}\"");
            }

            theDockerArguments.Append(' ').Append(options.ImageName);

            ExecuteDockerCli($"rm --volumes --force {options.Name}");

            string theContainerId = ExecuteDockerCli(theDockerArguments.ToString()).Substring(0, 8);

            var theContainer = new DockerContainer(theContainerId, options);

            WaitForReadyContainer(theContainer);

            return(theContainer);
        }
Exemplo n.º 2
0
 private DockerContainer(string id, DockerContainerOptions options)
 {
     Id           = id;
     StartOptions = options;
 }