示例#1
0
        private CreateContainerParametersBuilder AddPortBinding(string containerPort, string hostPort)
        {
            if (!PortBindings.ContainsKey(containerPort))
            {
                PortBindings.Add(containerPort, new List <PortBinding>());
            }

            PortBindings[containerPort].Add(new PortBinding
            {
                HostIP   = "0.0.0.0",
                HostPort = hostPort
            });

            return(this);
        }
        private CreateContainerParameters ApplyConfiguration()
        {
            var config = new Config
            {
                Image        = DockerImageName,
                Env          = Env.Select(kvp => $"{kvp.Key}={kvp.Value}").ToList(),
                ExposedPorts = ExposedPorts.ToDictionary(
                    e => string.Format(TcpExposedPortFormat, e),
                    e => default(EmptyStruct)),
                Labels       = Labels,
                WorkingDir   = WorkingDirectory,
                Cmd          = Command,
                Tty          = true,
                AttachStderr = true,
                AttachStdout = true,
            };

            return(new CreateContainerParameters(config)
            {
                HostConfig = new HostConfig
                {
                    AutoRemove = AutoRemove,
                    PortBindings = PortBindings.ToDictionary(
                        e => string.Format(TcpExposedPortFormat, e.Key),
                        e => (IList <PortBinding>) new List <PortBinding>
                    {
                        new PortBinding
                        {
                            HostPort = e.Value.ToString()
                        }
                    }),
                    Mounts = BindMounts.Select(m => new Mount
                    {
                        Source = m.HostPath,
                        Target = m.ContainerPath,
                        ReadOnly = m.AccessMode == AccessMode.ReadOnly,
                        Type = "bind"
                    })
                             .ToList(),
                    PublishAllPorts = true,
                    Privileged = IsPrivileged
                }
            });
        }
示例#3
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));
        }
 public void RemovePortBinding(PortBindingListItemModel portBinding)
 {
     PortBindings.Remove(portBinding);
 }
 public void AddPortBinding()
 {
     PortBindings.Add(new PortBindingListItemModel());
 }
示例#6
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}"
                            );
                    }
                }
            }
        }