Skip to content

Commit

Permalink
Support for restart policy when doing docker run:
Browse files Browse the repository at this point in the history
For example now it is possible to do eg:
```
var cmd = _docker.Run("postgres:9.6-alpine", new ContainerCreateParams
        {
          PortMappings = new[] {"40001:5432"},
          RestartPolicy= RestartPolicy.Always
          Environment = new[] {"POSTGRES_PASSWORD=mysecretpassword"}
        }, _certificates);```
  • Loading branch information
Mario Toffia committed Jul 20, 2018
1 parent 27c89d6 commit 3f469ae
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 86 deletions.
4 changes: 2 additions & 2 deletions Ductus.FluentDocker.MsTest/Ductus.FluentDocker.MsTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;netcoreapp2.0;net452</TargetFrameworks>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.3.1</VersionPrefix>
<AssemblyTitle>Ductus.FluentDocker.MsTest</AssemblyTitle>
<Authors>Mario Toffia</Authors>
<PackageLicenseUrl>https://github.com/mariotoffia/FluentDocker/blob/master/LICENSE</PackageLicenseUrl>
Expand All @@ -19,7 +19,7 @@ Documentation: https://github.com/mariotoffia/FluentDocker
<Copyright>© 2016, 2017, 2018 Mario Toffia</Copyright>
<DefineConstants Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">$(DefineConstants);COREFX</DefineConstants>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>2.3.0</Version>
<Version>2.3.1</Version>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
Expand Down
12 changes: 4 additions & 8 deletions Ductus.FluentDocker/Ductus.FluentDocker.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">


<PropertyGroup>
<TargetFrameworks>netstandard1.6;netstandard2.0;net452</TargetFrameworks>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.3.1</VersionPrefix>
<AssemblyTitle>Ductus.FluentDocker</AssemblyTitle>
<Authors>Mario Toffia</Authors>
<PackageLicenseUrl>https://github.com/mariotoffia/FluentDocker/blob/master/LICENSE</PackageLicenseUrl>
Expand All @@ -18,19 +16,17 @@
<DefineConstants Condition=" '$(TargetFramework)' == 'netstandard1.6' ">$(DefineConstants);COREFX</DefineConstants>
<DefineConstants Condition=" '$(TargetFramework)' == 'netstandard2.0' ">$(DefineConstants);COREFX</DefineConstants>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>2.3.0.0</AssemblyVersion>
<FileVersion>2.3.0.0</FileVersion>
<Version>2.3.0</Version>
<AssemblyVersion>2.3.1.0</AssemblyVersion>
<FileVersion>2.3.1.0</FileVersion>
<Version>2.3.1</Version>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="SharpCompress" Version="0.15.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="1.1.1" />
Expand Down
149 changes: 74 additions & 75 deletions Ductus.FluentDocker/Model/Containers/ContainerCreateParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ public sealed class ContainerCreateParams
public string[] Groups { get; set; }

/// <summary>
/// Connect a container to a network
/// Connect a container to a network
/// </summary>
/// <remarks>
/// --network
/// </remarks>
public string Network { get; set; }

public RestartPolicy RestartPolicy { get; set; }

/// <summary>
/// Renders the argument string from this instance.
/// </summary>
Expand All @@ -203,16 +205,10 @@ public override string ToString()
if (null != HostIpMappings && 0 != HostIpMappings.Count)
{
sb.Append(" --add-host=");
foreach (var mapping in HostIpMappings)
{
sb.Append($"--add-host={mapping.Item1}:{mapping.Item2}");
}
foreach (var mapping in HostIpMappings) sb.Append($"--add-host={mapping.Item1}:{mapping.Item2}");
}

if (null != BlockIoWeight)
{
sb.Append($" --blkio-weight {BlockIoWeight.Value}");
}
if (null != BlockIoWeight) sb.Append($" --blkio-weight {BlockIoWeight.Value}");

sb.OptionIfExists("--blkio-weight-device=", BlockIoWeightDevices);
sb.OptionIfExists("--cap-add=", CapabilitiesToAdd);
Expand All @@ -221,95 +217,98 @@ public override string ToString()
sb.OptionIfExists("-e ", Environment);
sb.OptionIfExists("--env-file=", EnvironmentFiles);

if (Interactive)
{
sb.Append(" -i");
}
if (Interactive) sb.Append(" -i");

if (Tty)
{
sb.Append(" -t");
}
if (Tty) sb.Append(" -t");

sb.OptionIfExists("--name ", Name);
sb.OptionIfExists("-u ", AsUser);

if (AutoRemoveContainer)
{
sb.Append(" --rm");
}
if (AutoRemoveContainer) sb.Append(" --rm");

sb.OptionIfExists("-v ", Volumes);
sb.OptionIfExists("--volume-driver ", VolumeDriver);
sb.OptionIfExists("--volumes-from=", VolumesFrom);
sb.OptionIfExists("-w ", WorkingDirectory);

if (!PublishAllPorts)
{
sb.OptionIfExists("-p ", PortMappings);
}
else
{
sb.Append(" -P");
}

sb.OptionIfExists("--link=", Links);
sb.OptionIfExists("-l ", Labels);
sb.OptionIfExists("--group-add=", Groups);
sb.OptionIfExists("--network ", Network);

if (RestartPolicy.No != RestartPolicy)
switch (RestartPolicy)
{
case RestartPolicy.Always:
sb.Append(" --restart always");
break;
case RestartPolicy.OnFailure:
sb.Append(" --restart on-failure");
break;
case RestartPolicy.UnlessStopped:
sb.Append(" --restart unless-stopped");
break;
default:
sb.Append(" --restart no");
break;
}

return sb.ToString();
}
}

/*
--cpu-shares CPU shares (relative weight)
--cidfile Write the container ID to the file
--cpu-period Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota Limit CPU CFS (Completely Fair Scheduler) quota
--cpuset-cpus CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems MEMs in which to allow execution (0-3, 0,1)
--detach-keys Override the key sequence for detaching a container
--device=[] Add a host device to the container
--device-read-bps=[] Limit read rate (bytes per second) from a device
--device-read-iops=[] Limit read rate (IO per second) from a device
--device-write-bps=[] Limit write rate (bytes per second) to a device
--device-write-iops=[] Limit write rate (IO per second) to a device
--disable-content-trust=true Skip image verification
--dns=[] Set custom DNS servers
--dns-opt=[] Set DNS options
--dns-search=[] Set custom DNS search domains
--entrypoint Overwrite the default ENTRYPOINT of the image
--expose=[] Expose a port or a range of ports
-h, --hostname Container host name
--ip Container IPv4 address (e.g. 172.30.100.104)
--ip6 Container IPv6 address (e.g. 2001:db8::33)
--ipc IPC namespace to use
--isolation Container isolation level
--kernel-memory Kernel memory limit
--label-file=[] Read in a line delimited file of labels
--log-driver Logging driver for container
--log-opt=[] Log driver options
-m, --memory Memory limit
--mac-address Container MAC address (e.g. 92:d0:c6:0a:29:33)
--memory-reservation Memory soft limit
--memory-swap Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness=-1 Tune container memory swappiness (0 to 100)
--net=default Connect a container to a network
--net-alias=[] Add network-scoped alias for the container
--oom-kill-disable Disable OOM Killer
--oom-score-adj Tune host's OOM preferences (-1000 to 1000)
--pid PID namespace to use
--privileged Give extended privileges to this container
--read-only Mount the container's root filesystem as read only
--restart=no Restart policy to apply when a container exits
--security-opt=[] Security Options
--shm-size Size of /dev/shm, default value is 64MB
--sig-proxy=true Proxy received signals to the process
--stop-signal=15 Signal to stop a container, 15 by default
--tmpfs=[] Mount a tmpfs directory
--ulimit=[] Ulimit options
--uts UTS namespace to use
--network Connect a container to a network
*/
/*
--cpu-shares CPU shares (relative weight)
--cidfile Write the container ID to the file
--cpu-period Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota Limit CPU CFS (Completely Fair Scheduler) quota
--cpuset-cpus CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems MEMs in which to allow execution (0-3, 0,1)
--detach-keys Override the key sequence for detaching a container
--device=[] Add a host device to the container
--device-read-bps=[] Limit read rate (bytes per second) from a device
--device-read-iops=[] Limit read rate (IO per second) from a device
--device-write-bps=[] Limit write rate (bytes per second) to a device
--device-write-iops=[] Limit write rate (IO per second) to a device
--disable-content-trust=true Skip image verification
--dns=[] Set custom DNS servers
--dns-opt=[] Set DNS options
--dns-search=[] Set custom DNS search domains
--entrypoint Overwrite the default ENTRYPOINT of the image
--expose=[] Expose a port or a range of ports
-h, --hostname Container host name
--ip Container IPv4 address (e.g. 172.30.100.104)
--ip6 Container IPv6 address (e.g. 2001:db8::33)
--ipc IPC namespace to use
--isolation Container isolation level
--kernel-memory Kernel memory limit
--label-file=[] Read in a line delimited file of labels
--log-driver Logging driver for container
--log-opt=[] Log driver options
-m, --memory Memory limit
--mac-address Container MAC address (e.g. 92:d0:c6:0a:29:33)
--memory-reservation Memory soft limit
--memory-swap Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness=-1 Tune container memory swappiness (0 to 100)
--net=default Connect a container to a network
--net-alias=[] Add network-scoped alias for the container
--oom-kill-disable Disable OOM Killer
--oom-score-adj Tune host's OOM preferences (-1000 to 1000)
--pid PID namespace to use
--privileged Give extended privileges to this container
--read-only Mount the container's root filesystem as read only
--security-opt=[] Security Options
--shm-size Size of /dev/shm, default value is 64MB
--sig-proxy=true Proxy received signals to the process
--stop-signal=15 Signal to stop a container, 15 by default
--tmpfs=[] Mount a tmpfs directory
--ulimit=[] Ulimit options
--uts UTS namespace to use
--network Connect a container to a network
*/
}
22 changes: 22 additions & 0 deletions Ductus.FluentDocker/Model/Containers/RestartPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Ductus.FluentDocker.Model.Containers
{
public enum RestartPolicy
{
/// <summary>
/// Do not automatically restart the container. (the default).
/// </summary>
No = 0,
/// <summary>
/// Restart the container if it exits due to an error, which manifests as a non-zero exit code.
/// </summary>
OnFailure = 1,
/// <summary>
/// Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted.
/// </summary>
UnlessStopped = 2,
/// <summary>
/// Always restart the container if it stops.
/// </summary>
Always = 3
}
}
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#version: 0.0.0.{build}
version: 2.3.0
version: 2.3.1
skip_non_tags: true
image: Visual Studio 2017
configuration: Release
Expand Down

0 comments on commit 3f469ae

Please sign in to comment.