Пример #1
0
        public override IContainerService Build()
        {
            var host = FindHostService();

            if (!host.HasValue)
            {
                throw new FluentDockerException(
                          $"Cannot build container {_config.Image} since no host service is defined");
            }

            // Login on private repo if needed.
            _repositoryBuilder?.Build(host.Value);

            if (_config.VerifyExistence && !string.IsNullOrEmpty(_config.CreateParams.Name))
            {
                // Since filter on docker is only prefix filter
                var existing =
                    host.Value.GetContainers(true, $"name={_config.CreateParams.Name}")
                    .FirstOrDefault(x => IsNameMatch(x.Name, _config.CreateParams.Name));

                if (null != existing)
                {
                    existing.RemoveOnDispose = _config.DeleteOnDispose;
                    existing.StopOnDispose   = _config.StopOnDispose;

                    return(existing);
                }
            }

            var container = host.Value.Create(_config.Image, _config.CreateParams, _config.StopOnDispose,
                                              _config.DeleteOnDispose,
                                              _config.DeleteVolumeOnDispose,
                                              _config.DeleteNamedVolumeOnDispose,
                                              _config.Command, _config.Arguments);

            AddHooks(container);

            foreach (var network in (IEnumerable <INetworkService>)_config.Networks ?? new INetworkService[0])
            {
                network.Attach(container, true /*detachOnDisposeNetwork*/);
            }

            if (null == _config.NetworkNames)
            {
                return(container);
            }

            var nw = host.Value.GetNetworks();

            foreach (var network in (IEnumerable <string>)_config.NetworkNames ?? new string[0])
            {
                var nets = nw.First(x => x.Name == network);
                nets.Attach(container, true /*detachOnDisposeNetwork*/);
            }

            return(container);
        }
Пример #2
0
        public override IContainerService Build()
        {
            var host = FindHostService();

            if (!host.HasValue)
            {
                throw new FluentDockerException(
                          $"Cannot build container {_config.Image} since no host service is defined");
            }

            // Login on private repo if needed.
            _repositoryBuilder?.Build(host.Value);

            if (_config.VerifyExistence && !string.IsNullOrEmpty(_config.CreateParams.Name))
            {
                // Since filter on docker is only prefix filter
                var existing =
                    host.Value.GetContainers(true, $"name={_config.CreateParams.Name}")
                    .FirstOrDefault(x => IsNameMatch(x.Name, _config.CreateParams.Name));

                if (null != existing)
                {
                    existing.RemoveOnDispose = _config.DeleteOnDispose;
                    existing.StopOnDispose   = _config.StopOnDispose;

                    // Run hooks will not be run since they have already met the requirements
                    // (since container was found running).
                    AddHooks(existing);

                    return(existing);
                }
            }

            // If we have networks, the first is supplied as --network option on docker create
            // TODO: This is a ugly hack that needs to be cleaned up.
            var cfgNw = (IList <INetworkService>)_config.Networks ?? new INetworkService[0];

            var firstNw = null != _config.NetworkNames
        ? _config.NetworkNames[0]
        : (0 == cfgNw.Count ? string.Empty : cfgNw[0].Name);

            if (string.Empty != firstNw)
            {
                _config.CreateParams.Network = firstNw;
            }

            var container = host.Value.Create(_config.Image, _config.ImageFocrePull, _config.CreateParams, _config.StopOnDispose,
                                              _config.DeleteOnDispose,
                                              _config.DeleteVolumeOnDispose,
                                              _config.DeleteNamedVolumeOnDispose,
                                              _config.Command, _config.Arguments);

            AddHooks(container);

            foreach (var network in cfgNw)
            {
                if (network.Name != firstNw)
                {
                    network.Attach(container, true /*detachOnDisposeNetwork*/);
                }
            }

            if (null == _config.NetworkNames)
            {
                return(container);
            }

            var nw = host.Value.GetNetworks();

            foreach (var network in (IEnumerable <string>)_config.NetworkNames ?? Array.Empty <string>())
            {
                if (network == firstNw)
                {
                    continue;
                }

                var nets = nw.First(x => x.Name == network);
                nets.Attach(container, true /*detachOnDisposeNetwork*/);
            }

            return(container);
        }
        public override IContainerService Build()
        {
            var host = FindHostService();

            if (!host.HasValue)
            {
                throw new FluentDockerException(
                          $"Cannot build container {_config.Image} since no host service is defined");
            }

            // Login on private repo if needed.
            _repositoryBuilder?.Build(host.Value);

            // Destroy the container if it already exists (if enabled).
            if (_config.DestroyIfExists != null &&
                !string.IsNullOrEmpty(_config.CreateParams.Name))
            {
                host.Value.Host.RemoveContainer(
                    _config.CreateParams.Name,
                    _config.DestroyIfExists.Force,
                    _config.DestroyIfExists.RemoveVolumes,
                    _config.DestroyIfExists.LinkToRemove,
                    host.Value.Certificates);
            }

            if (_config.VerifyExistence && !string.IsNullOrEmpty(_config.CreateParams.Name))
            {
                // Since filter on docker is only prefix filter
                var existing =
                    host.Value.GetContainers(true, $"name={_config.CreateParams.Name}")
                    .FirstOrDefault(x => IsNameMatch(x.Name, _config.CreateParams.Name));

                if (null != existing)
                {
                    existing.RemoveOnDispose = _config.DeleteOnDispose;
                    existing.StopOnDispose   = _config.StopOnDispose;

                    // Run hooks will not be run since they have already met the requirements
                    // (since container was found running).
                    AddHooks(existing);

                    return(existing);
                }
            }

            var firstNetwork = _config.FindFirstNetworkNameAndAlias();

            if (string.Empty != firstNetwork.Network)
            {
                _config.CreateParams.Network = firstNetwork.Network;

                if (string.Empty != firstNetwork.Alias)
                {
                    _config.CreateParams.Alias = firstNetwork.Alias;
                }
            }

            var container = host.Value.Create(_config.Image, _config.ImageForcePull, _config.CreateParams, _config.StopOnDispose,
                                              _config.DeleteOnDispose,
                                              _config.DeleteVolumeOnDispose,
                                              _config.DeleteNamedVolumeOnDispose,
                                              _config.Command, _config.Arguments,
                                              _config.CustomResolver);

            AddHooks(container);

            foreach (var network in (IEnumerable <INetworkService>)_config.Networks ?? Array.Empty <INetworkService>())
            {
                if (network.Name != firstNetwork.Network)
                {
                    network.Attach(container, true /*detachOnDisposeNetwork*/);
                }
            }

            foreach (var networkWithAlias in (IEnumerable <NetworkWithAlias <INetworkService> >)_config.NetworksWithAlias ?? Array.Empty <NetworkWithAlias <INetworkService> >())
            {
                var network = networkWithAlias.Network;
                if (network.Name != firstNetwork.Network)
                {
                    network.Attach(container, true /*detachOnDisposeNetwork*/, networkWithAlias.Alias);
                }
            }

            var nw = host.Value.GetNetworks();

            foreach (var network in (IEnumerable <string>)_config.NetworkNames ?? Array.Empty <string>())
            {
                if (network == firstNetwork.Network)
                {
                    continue;
                }

                var nets = nw.First(x => x.Name == network);
                nets.Attach(container, true /*detachOnDisposeNetwork*/);
            }

            foreach (var networkWithAlias in (IEnumerable <NetworkWithAlias <string> >)_config.NetworkNamesWithAlias ?? Array.Empty <NetworkWithAlias <string> >())
            {
                var network = networkWithAlias.Network;
                if (network == firstNetwork.Network)
                {
                    continue;
                }

                var nets = nw.First(x => x.Name == network);
                nets.Attach(container, true /*detachOnDisposeNetwork*/, networkWithAlias.Alias);
            }

            return(container);
        }