Skip to content

Commit 2817fc3

Browse files
committed
Suggestion for how to have customizable Dockerfile names.
1 parent 51418e5 commit 2817fc3

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

Ductus.FluentDocker/Builders/FileBuilder.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public FileBuilder WithHealthCheck(string cmd, string interval = null, string ti
149149
/// <param name="dest">To directory.</param>
150150
/// <param name="chownUserAndGroup">Optional --chown user:group.</param>
151151
/// <param name="fromAlias">
152-
/// Optional source location from earlier buildstage FROM ... AS alias. This will
152+
/// Optional source location from earlier buildstage FROM ... AS alias. This will
153153
/// generate --from=aliasname in the _COPY_ command and hence reference a earlier
154154
/// _FROM ... AS aliasname_ buildstep as source.
155155
/// </param>
@@ -331,7 +331,12 @@ private void RenderDockerfile(TemplateString workingFolder)
331331
Directory.CreateDirectory(workingFolder);
332332
}
333333

334-
var dockerFile = Path.Combine(workingFolder, "Dockerfile");
334+
if (string.IsNullOrWhiteSpace(_parent.Config.Params.File))
335+
{
336+
_parent.Config.Params.File = "Dockerfile";
337+
}
338+
339+
var dockerFile = Path.Combine(workingFolder, _parent.Config.Params.File);
335340

336341
var contents = !string.IsNullOrEmpty(_config.UseFile) ?
337342
_config.UseFile.FromFile() :

Ductus.FluentDocker/Builders/ImageBuilder.cs

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Ductus.FluentDocker.Common;
33
using Ductus.FluentDocker.Extensions;
44
using Ductus.FluentDocker.Model.Builders;
5+
using Ductus.FluentDocker.Model.Common;
56
using Ductus.FluentDocker.Model.Containers;
67
using Ductus.FluentDocker.Services;
78
using Ductus.FluentDocker.Services.Extensions;
@@ -11,7 +12,7 @@ namespace Ductus.FluentDocker.Builders
1112
{
1213
public sealed class ImageBuilder : BaseBuilder<IContainerImageService>
1314
{
14-
private readonly ImageBuilderConfig _config = new ImageBuilderConfig();
15+
internal readonly ImageBuilderConfig Config = new ImageBuilderConfig();
1516
private FileBuilder _fileBuilder;
1617

1718
internal ImageBuilder(IBuilder parent) : base(parent)
@@ -20,40 +21,40 @@ internal ImageBuilder(IBuilder parent) : base(parent)
2021

2122
public override IContainerImageService Build()
2223
{
23-
if (string.IsNullOrEmpty(_config.ImageName))
24+
if (string.IsNullOrEmpty(Config.ImageName))
2425
throw new FluentDockerException("Cannot create or verify an image without a name");
2526

2627
var host = FindHostService();
2728
if (!host.HasValue)
2829
throw new FluentDockerException(
29-
$"Cannot build Dockerfile for image {_config.ImageName} since no host service is defined");
30+
$"Cannot build Dockerfile for image {Config.ImageName} since no host service is defined");
3031

31-
var tag = null == _config.Params.Tags ? "latest" : _config.Params.Tags[0];
32-
var image = host.Value.GetImages().FirstOrDefault(x => x.Name == _config.ImageName && x.Tag == tag);
32+
var tag = null == Config.Params.Tags ? "latest" : Config.Params.Tags[0];
33+
var image = host.Value.GetImages().FirstOrDefault(x => x.Name == Config.ImageName && x.Tag == tag);
3334

34-
if (_config.VerifyExistence && null != image)
35+
if (Config.VerifyExistence && null != image)
3536
return image;
3637

37-
if (null == _config.Params.Tags)
38-
_config.Params.Tags = new[] { "latest" };
38+
if (null == Config.Params.Tags)
39+
Config.Params.Tags = new[] { "latest" };
3940

4041
// Render docker file and copy all resources
4142
// to a working directory
4243
var workingdir = _fileBuilder.PrepareBuild();
4344

44-
var id = host.Value.Build(_config.ImageName, _config.Params.Tags[0], workingdir, new ContainerBuildParams
45-
{
46-
BuildArguments = _config.Params.BuildArguments,
47-
Tags = _config.Params.Tags.Except(new[] { _config.Params.Tags[0] }).ToArray(),
48-
Quiet = true
49-
});
45+
var id = host.Value.Build(Config.ImageName, Config.Params.Tags[0], workingdir, new ContainerBuildParams
46+
{
47+
BuildArguments = Config.Params.BuildArguments,
48+
Tags = Config.Params.Tags.Except(new[] {Config.Params.Tags[0]}).ToArray(),
49+
Quiet = true
50+
});
5051

5152
if (id.IsFailure)
5253
throw new FluentDockerException(
53-
$"Could not build image {_config.ImageName} due to error: {id.Error} log: {id.Log}");
54+
$"Could not build image {Config.ImageName} due to error: {id.Error} log: {id.Log}");
5455

55-
return new DockerImageService(_config.ImageName, id.Value.ToPlainId(), _config.Params.Tags[0], host.Value.Host,
56-
host.Value.Certificates, _config.IsWindowsHost);
56+
return new DockerImageService(Config.ImageName, id.Value.ToPlainId(), Config.Params.Tags[0], host.Value.Host,
57+
host.Value.Certificates, Config.IsWindowsHost);
5758
}
5859

5960
protected override IBuilder InternalCreate()
@@ -103,13 +104,13 @@ public FileBuilder FromString(string dockerfileString)
103104

104105
public ImageBuilder IsWindowsHost()
105106
{
106-
_config.IsWindowsHost = true;
107+
Config.IsWindowsHost = true;
107108
return this;
108109
}
109110

110111
public ImageBuilder ReuseIfAlreadyExists()
111112
{
112-
_config.VerifyExistence = true;
113+
Config.VerifyExistence = true;
113114
return this;
114115
}
115116

@@ -118,69 +119,82 @@ public ImageBuilder AsImageName(string name)
118119
if (name == null) {
119120
return this;
120121
}
121-
122+
122123
var s = name.Split(':');
123124
if (s.Length == 2)
124125
{
125-
_config.ImageName = s[0];
126-
_config.Params.Tags = _config.Params.Tags.ArrayAdd(s[1]);
126+
Config.ImageName = s[0];
127+
Config.Params.Tags = Config.Params.Tags.ArrayAdd(s[1]);
127128
}
128129
else
129130
{
130-
_config.ImageName = name;
131-
_config.Params.Tags = _config.Params.Tags.ArrayAdd("latest");
131+
Config.ImageName = name;
132+
Config.Params.Tags = Config.Params.Tags.ArrayAdd("latest");
132133
}
133134

134135
return this;
135136
}
136137

137138
public ImageBuilder ImageTag(params string[] tags)
138139
{
139-
_config.Params.Tags = _config.Params.Tags.ArrayAddDistinct(tags);
140+
Config.Params.Tags = Config.Params.Tags.ArrayAddDistinct(tags);
140141
return this;
141142
}
142143

143144
public ImageBuilder BuildArguments(params string[] args)
144145
{
145-
_config.Params.BuildArguments = _config.Params.BuildArguments.ArrayAdd(args);
146+
Config.Params.BuildArguments = Config.Params.BuildArguments.ArrayAdd(args);
147+
return this;
148+
}
149+
150+
/// <summary>
151+
/// Sets the file name of the rendered Dockerfile to the specified <paramref name="dockerfileName"/>.
152+
/// Default of <see cref="ContainerBuildParams.File"/> is 'PATH/Dockerfile'.
153+
/// </summary>
154+
/// <param name="dockerfileName"></param>
155+
/// <returns></returns>
156+
/// <remarks><inheritdoc cref="ContainerBuildParams.File"/></remarks>
157+
public ImageBuilder DockerfileName(TemplateString dockerfileName)
158+
{
159+
Config.Params.File = dockerfileName;
146160
return this;
147161
}
148162

149163
public ImageBuilder NoVerifyImage()
150164
{
151-
_config.Params.SkipImageVerification = true;
165+
Config.Params.SkipImageVerification = true;
152166
return this;
153167
}
154168

155169
public ImageBuilder Label(params string[] labels)
156170
{
157-
_config.Params.Labels = _config.Params.Labels.ArrayAdd(labels);
171+
Config.Params.Labels = Config.Params.Labels.ArrayAdd(labels);
158172
return this;
159173
}
160174

161175
public ImageBuilder NoCache()
162176
{
163-
_config.Params.NoCache = true;
177+
Config.Params.NoCache = true;
164178
return this;
165179
}
166180

167181
public ImageBuilder AlwaysPull()
168182
{
169-
_config.Params.AlwaysPull = true;
183+
Config.Params.AlwaysPull = true;
170184
return this;
171185
}
172186

173187
public ImageBuilder WithIsolation(ContainerIsolationTechnology isolation)
174188
{
175-
_config.Params.Isolation = isolation;
189+
Config.Params.Isolation = isolation;
176190
return this;
177191
}
178192

179193
public ImageBuilder RemoveIntermediate(bool force = false)
180194
{
181-
_config.Params.RemoveIntermediateContainersOnSuccessfulBuild = true;
195+
Config.Params.RemoveIntermediateContainersOnSuccessfulBuild = true;
182196
if (force)
183-
_config.Params.ForceRemoveIntermediateContainers = true;
197+
Config.Params.ForceRemoveIntermediateContainers = true;
184198
return this;
185199
}
186200
}

0 commit comments

Comments
 (0)