Ejemplo n.º 1
0
        public override string ToString()
        {
            List <string> options = new List <string>();

            if (!string.IsNullOrEmpty(ContainerName))
            {
                options.Add($"--name \"{ContainerName}\"");
            }

            if (PortBindings.Any())
            {
                options.AddRange(PortBindings.Select(portBinding => $"-p {portBinding.HostPort}:{portBinding.ContainerPort}"));
            }

            if (EnvironmentVariables.Any())
            {
                options.AddRange(EnvironmentVariables.Select(environmentVariable =>
                {
                    if (string.IsNullOrEmpty(environmentVariable.Value))
                    {
                        return($"-e {environmentVariable.Key}");
                    }
                    return($"-e {environmentVariable.Key}={environmentVariable.Value}");
                }));
            }

            if (RestartAutomatically)
            {
                options.Add("--restart=unless-stopped");
            }

            if (!string.IsNullOrEmpty(AdditionalOptions))
            {
                options.Add(AdditionalOptions);
            }

            return(string.Join(" ", options));
        }
Ejemplo n.º 2
0
        public void LogParameters(ILogger logger)
        {
            using var contentsScope = logger.BeginScope("Using create container parameters");

            logger.LogInformation($"Container name '{ContainerName}'");
            logger.LogInformation($"Container image: '{ImageId.FullName}");
            logger.LogInformation($"Network name: {ImageId.FullName}");

            using (logger.BeginScope("Labels"))
            {
                if (!Labels.Any())
                {
                    logger.LogError("No labels are configured for this container!");
                }

                foreach (var(name, value) in Labels)
                {
                    logger.LogInformation($"{name}: {value}");
                }
            }

            using (logger.BeginScope("Environment variables"))
            {
                if (!EnvironmentVariables.Any())
                {
                    logger.LogInformation("No environment variables are set");
                }

                foreach (var variable in EnvironmentVariables)
                {
                    logger.LogInformation(variable);
                }
            }

            using (logger.BeginScope("Volume mounts"))
            {
                if (!Mounts.Any())
                {
                    logger.LogWarning("There are no volume mounts specified for this container");
                }

                foreach (var mount in Mounts)
                {
                    logger.LogInformation($"Volume '{mount.Source}' mounted to '{mount.Target}'");
                }
            }

            using (logger.BeginScope("Port bindings"))
            {
                if (!PortBindings.Any())
                {
                    logger.LogWarning("There are no port bindings specified for this container");
                }

                foreach (var(containerPort, hostBindings) in PortBindings)
                {
                    foreach (var binding in hostBindings)
                    {
                        logger.LogInformation(
                            $"Container port {containerPort} is bound to {binding.HostIP}:{binding.HostPort}"
                            );
                    }
                }
            }
        }