Exemplo n.º 1
0
 /// <summary>
 /// Run the container
 /// </summary>
 public void StartContainer()
 {
     if (ContainerState == DockerContainerState.Running)
     {
         throw new InvalidOperationException($"Контейнер {Settings.ContainerName} уже запущен.");
     }
     else if (ContainerState == DockerContainerState.NotExits)
     {
         var arguments = GetDockerRunArguments();
         var res       = TerminalHelper.RunCommand("docker", arguments);
         if (res.HasError == true)
         {
             throw new InvalidOperationException("Не удалось запустить контейнер. Ошибка: " + res.ErrorOutput);
         }
         ContainerId    = res.StandardOutput;
         ContainerState = DockerContainerState.Running;
     }
     else if (ContainerState == DockerContainerState.Stopped)
     {
         throw new NotImplementedException("Не реализован запуск остановленного контейнера");
     }
     else
     {
         throw new InvalidOperationException("Что-то пошло не так...");
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Remove the container
        /// </summary>
        /// <param name="force">Ignore errors</param>
        public void RemoveContainer(bool force = true)
        {
            var idOrName = ContainerId ?? Settings.ContainerName;

            if (string.IsNullOrWhiteSpace(idOrName) == true)
            {
                throw new InvalidOperationException("The attempt to delete a container that has no id and name defined");
            }
            var res = TerminalHelper.RunCommand("docker", $"rm -f {idOrName}");

            if (res.HasError == true && force == false)
            {
                throw new InvalidOperationException("Error while deleting container: " + res.ErrorOutput);
            }
            ContainerState = DockerContainerState.NotExits;
        }