Skip to content

Commit 5e0aac5

Browse files
authored
Merge branch 'master' into pullReq626
2 parents b6bb1af + 2ff5bd4 commit 5e0aac5

File tree

10 files changed

+130
-12
lines changed

10 files changed

+130
-12
lines changed

doc/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
- Support docker SHELL setting for runCmds (#1157)
1414
- Added 'autoRemove' option for running containers (#1179)
1515
- Added support for AWS EC2 instance roles when pushing to AWS ECR (#1186)
16+
- Introduce `contextDir` configuration option which would be used to specify docker build context (#1189)
1617
- Add support for auto-pulling multiple base image for multi stage builds (#1057)
1718
- Fix usage of credential helper that do not support 'version' command (#1159)
19+
20+
Please note that `dockerFileDir` is now deprecated in favor of `contextDir` which also allows absolute paths to Dockerfile with
21+
`dockerFile` and it will be removed in 1.0.0. It's still supported in this release but users are suggested to migrate to
22+
`contextDir` instead.
1823

1924
* **0.28.0** (2018-12-13)
2025
- Update to JMockit 1.43

samples/dockerfile/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<alias>dockerfile</alias>
5959
<build>
6060
<!-- filter>@</filter-->
61-
<dockerFileDir>${project.basedir}/src/main/docker</dockerFileDir>
61+
<contextDir>${project.basedir}/src/main/docker</contextDir>
6262
<assembly>
6363
<descriptorRef>rootWar</descriptorRef>
6464
</assembly>

src/main/asciidoc/inc/build/_overview.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ When using this mode, the Dockerfile is created on the fly with all instructions
1111
.External Dockerfile or Docker archive
1212
Alternatively an external Dockerfile template or Docker archive can be used. This mode is switched on by using one of these three configuration options within
1313

14-
* *dockerFileDir* specifies a directory containing a Dockerfile that will be used to create the image. The name of the Dockerfile is `Dockerfile` by default but can be also set with the option `dockerFile` (see below).
15-
* *dockerFile* specifies a specific Dockerfile path. The Docker build context directory is set to `dockerFileDir` if given. If not the directory by default is the directory in which the Dockerfile is stored.
14+
* *contextDir* specifies docker build context if an external dockerfile is located outside of Docker build context. If not specified, Dockerfile's parent directory is used as build context.
15+
* *dockerFile* specifies a specific Dockerfile path.
1616
* *dockerArchive* specifies a previously saved image archive to load directly. Such a tar archive can be created with `docker save`. If a `dockerArchive` is provided, no `dockerFile` or `dockerFileDir` must be given.
17+
* *dockerFileDir* (*deprecated*, use *contextDir*) specifies a directory containing a Dockerfile that will be used to create the image. The name of the Dockerfile is `Dockerfile` by default but can be also set with the option `dockerFile` (see below).
1718
1819
All paths can be either absolute or relative paths (except when both `dockerFileDir` and `dockerFile` are provided in which case `dockerFile` must not be absolute). A relative path is looked up in `${project.basedir}/src/main/docker` by default. You can make it easily an absolute path by using `${project.basedir}` in your configuration.
1920

src/main/java/io/fabric8/maven/docker/assembly/DockerAssemblyManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public File createDockerTarArchive(String imageName, final MojoParameters params
130130
archiveCustomizers.add(new ArchiverCustomizer() {
131131
@Override
132132
public TarArchiver customize(TarArchiver archiver) throws IOException {
133-
DefaultFileSet fileSet = DefaultFileSet.fileSet(dockerFile.getParentFile());
133+
DefaultFileSet fileSet = DefaultFileSet.fileSet(buildConfig.getAbsoluteContextDirPath(params));
134134
addDockerIncludesExcludesIfPresent(fileSet, params);
135135
// Exclude non-interpolated dockerfile from source tree
136136
// Interpolated Dockerfile is already added as it was created into the output directory when

src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.fabric8.maven.docker.config;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.io.Serializable;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
58
import java.util.*;
69

710
import io.fabric8.maven.docker.util.*;
@@ -18,15 +21,23 @@ public class BuildImageConfiguration implements Serializable {
1821
public static final String DEFAULT_FILTER = "${*}";
1922
public static final String DEFAULT_CLEANUP = "try";
2023

24+
/**
25+
* Directory is used as build context.
26+
* If not specified, dockerfile's parent directory is used as build context.
27+
*/
28+
@Parameter
29+
private String contextDir;
30+
2131
/**
2232
* Directory holding an external Dockerfile which is used to build the
2333
* image. This Dockerfile will be enriched by the addition build configuration
2434
*/
2535
@Parameter
36+
@Deprecated
2637
private String dockerFileDir;
2738

2839
/**
29-
* Path to a dockerfile to use. Its parent directory is used as build context (i.e. as <code>dockerFileDir</code>).
40+
* Path to a dockerfile to use.
3041
* Multiple different Dockerfiles can be specified that way. If set overwrites a possibly givem
3142
* <code>dockerFileDir</code>
3243
*/
@@ -153,6 +164,14 @@ public boolean isDockerFileMode() {
153164
return dockerFileFile != null;
154165
}
155166

167+
public File getContextDir() {
168+
return contextDir != null ? new File(contextDir) : getDockerFile().getParentFile();
169+
}
170+
171+
public String getContextDirRaw() {
172+
return contextDir;
173+
}
174+
156175
public File getDockerFile() {
157176
return dockerFileFile;
158177
}
@@ -313,6 +332,10 @@ public Map<String, String> getArgs() {
313332
return args;
314333
}
315334

335+
public File getAbsoluteContextDirPath(MojoParameters mojoParams) {
336+
return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getContextDir().getPath());
337+
}
338+
316339
public File getAbsoluteDockerFilePath(MojoParameters mojoParams) {
317340
return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getDockerFile().getPath());
318341
}
@@ -336,6 +359,11 @@ public Builder(BuildImageConfiguration that) {
336359
}
337360
}
338361

362+
public Builder contextDir(String dir) {
363+
config.contextDir = dir;
364+
return this;
365+
}
366+
339367
public Builder dockerFileDir(String dir) {
340368
config.dockerFileDir = dir;
341369
return this;
@@ -568,21 +596,41 @@ private void initDockerFileFile(Logger log) {
568596
}
569597

570598
private File findDockerFileFile(Logger log) {
599+
if(dockerFileDir != null && contextDir != null) {
600+
log.warn("Both contextDir (%s) and deprecated dockerFileDir (%s) are configured. Using contextDir.", contextDir, dockerFileDir);
601+
}
602+
571603
if (dockerFile != null) {
604+
if (EnvUtil.isWindows() && !EnvUtil.isValidWindowsFileName(dockerFile)) {
605+
throw new IllegalArgumentException(String.format("Invalid Windows file name %s for <dockerFile>", dockerFile));
606+
}
607+
572608
File dFile = new File(dockerFile);
573-
if (dockerFileDir == null) {
609+
if (dockerFileDir == null && contextDir == null) {
574610
return dFile;
575611
} else {
576-
if (dFile.isAbsolute()) {
577-
throw new IllegalArgumentException("<dockerFile> can not be absolute path if <dockerFileDir> also set.");
612+
if(contextDir != null) {
613+
if (dFile.isAbsolute()) {
614+
return dFile;
615+
}
616+
return new File(contextDir, dockerFile);
578617
}
579-
if (EnvUtil.isWindows() && !EnvUtil.isValidWindowsFileName(dockerFile)) {
580-
throw new IllegalArgumentException(String.format("Invalid Windows file name %s for <dockerFile>", dockerFile));
618+
619+
if (dockerFileDir != null) {
620+
if (dFile.isAbsolute()) {
621+
throw new IllegalArgumentException("<dockerFile> can not be absolute path if <dockerFileDir> also set.");
622+
}
623+
log.warn("dockerFileDir parameter is deprecated, please migrate to contextDir");
624+
return new File(dockerFileDir, dockerFile);
581625
}
582-
return new File(dockerFileDir, dockerFile);
583626
}
584627
}
585628

629+
630+
if (contextDir != null) {
631+
return new File(contextDir, "Dockerfile");
632+
}
633+
586634
if (dockerFileDir != null) {
587635
return new File(dockerFileDir, "Dockerfile");
588636
}

src/main/java/io/fabric8/maven/docker/config/ConfigHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static String getExternalConfigActivationProperty(MavenProject project) {
114114
*
115115
* @param images the images to check
116116
* @param apiVersion the original API version intended to use
117-
* @param nameFormatter formmatter for image names
117+
* @param nameFormatter formatter for image names
118118
* @param log a logger for printing out diagnostic messages
119119
* @return the minimal API Docker API required to be used for the given configuration.
120120
*/

src/main/java/io/fabric8/maven/docker/config/handler/property/ConfigKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public enum ConfigKey {
4848
NOCACHE,
4949
OPTIMISE,
5050
CMD,
51+
CONTEXT_DIR,
5152
DEPENDS_ON,
5253
DOMAINNAME,
5354
DNS,

src/main/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ private boolean buildConfigured(BuildImageConfiguration config, ValueProvider va
110110
return true;
111111
}
112112

113+
if (isStringValueNull(valueProvider, config, CONTEXT_DIR, () -> config.getContextDirRaw())) {
114+
return true;
115+
}
116+
113117
if (isStringValueNull(valueProvider, config, DOCKER_FILE_DIR, () -> config.getDockerFileDirRaw())) {
114118
return true;
115119
}
@@ -151,6 +155,7 @@ private BuildImageConfiguration extractBuildConfiguration(ImageConfiguration fro
151155
.workdir(valueProvider.getString(WORKDIR, config == null ? null : config.getWorkdir()))
152156
.skip(valueProvider.getBoolean(SKIP_BUILD, config == null ? null : config.getSkip()))
153157
.imagePullPolicy(valueProvider.getString(IMAGE_PULL_POLICY_BUILD, config == null ? null : config.getImagePullPolicy()))
158+
.contextDir(valueProvider.getString(CONTEXT_DIR, config == null ? null : config.getContextDirRaw()))
154159
.dockerArchive(valueProvider.getString(DOCKER_ARCHIVE, config == null ? null : config.getDockerArchiveRaw()))
155160
.dockerFile(valueProvider.getString(DOCKER_FILE, config == null ? null : config.getDockerFileRaw()))
156161
.dockerFileDir(valueProvider.getString(DOCKER_FILE_DIR, config == null ? null : config.getDockerFileDirRaw()))

src/test/java/io/fabric8/maven/docker/config/BuildImageConfigurationTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
import java.io.File;
20+
import java.io.IOException;
2021

2122
import io.fabric8.maven.docker.util.Logger;
2223
import mockit.Expectations;
@@ -55,6 +56,7 @@ public void simpleDockerfile() {
5556
config.initAndValidate(logger);
5657
assertTrue(config.isDockerFileMode());
5758
assertEquals(config.getDockerFile(),new File("src/docker/Dockerfile"));
59+
assertEquals(config.getContextDir(),new File("src/docker"));
5860
}
5961

6062
@Test
@@ -65,6 +67,7 @@ public void simpleDockerfileDir() {
6567
config.initAndValidate(logger);
6668
assertTrue(config.isDockerFileMode());
6769
assertEquals(config.getDockerFile(),new File("src/docker/Dockerfile"));
70+
assertEquals(config.getContextDir(),new File("src/docker"));
6871
}
6972

7073
@Test
@@ -87,6 +90,53 @@ public void DockerfileDirAndDockerfileAlsoSetButDockerfileIsAbsoluteExceptionThr
8790
config.initAndValidate(logger);
8891
}
8992

93+
@Test
94+
public void contextDir() {
95+
BuildImageConfiguration config =
96+
new BuildImageConfiguration.Builder().
97+
contextDir("target").build();
98+
config.initAndValidate(logger);
99+
assertEquals(new File("target"), config.getContextDir());
100+
}
101+
102+
@Test
103+
public void contextDirAndDockerfile() {
104+
BuildImageConfiguration config =
105+
new BuildImageConfiguration.Builder().
106+
dockerFile("src/docker/Dockerfile").
107+
contextDir("target").build();
108+
config.initAndValidate(logger);
109+
assertEquals(new File("target/src/docker/Dockerfile"), config.getDockerFile());
110+
assertEquals(new File("target"), config.getContextDir());
111+
}
112+
113+
@Test
114+
public void contextDirAndDockerfileDir() {
115+
BuildImageConfiguration config =
116+
new BuildImageConfiguration.Builder().
117+
dockerFileDir("src/docker").
118+
contextDir("target").build();
119+
config.initAndValidate(logger);
120+
assertEquals(new File("target/Dockerfile"), config.getDockerFile());
121+
assertEquals(new File("target"), config.getContextDir());
122+
}
123+
124+
@Test
125+
public void contextDirAndAbsoluteDockerfile() throws IOException {
126+
File tempDockerFile = File.createTempFile("Dockerfile", "");
127+
tempDockerFile.deleteOnExit();
128+
BuildImageConfiguration config = new BuildImageConfiguration.Builder()
129+
.dockerFile(tempDockerFile.getAbsolutePath())
130+
.contextDir("target")
131+
.build();
132+
133+
// If contextDir is given and the dockerFile is an absolute path.
134+
// The Dockerfile should then be copied over.
135+
config.initAndValidate(logger);
136+
assertEquals(new File(tempDockerFile.getAbsolutePath()), config.getDockerFile());
137+
assertEquals(new File("target"), config.getContextDir());
138+
}
139+
90140
@Test
91141
public void deprecatedDockerfileDir() {
92142
AssemblyConfiguration assemblyConfig = new AssemblyConfiguration.Builder().dockerFileDir("src/docker").build();

src/test/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandlerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,14 @@ public void testDockerFileDir() {
738738
assertNotNull(config.getBuildConfiguration());
739739
}
740740

741+
@Test
742+
public void testContextDir() {
743+
String[] testData = new String[] {k(ConfigKey.NAME), "image", k(ConfigKey.CONTEXT_DIR), "dir" };
744+
745+
ImageConfiguration config = resolveExternalImageConfig(testData);
746+
assertNotNull(config.getBuildConfiguration());
747+
}
748+
741749
@Test
742750
public void testFilterDefault() {
743751
String[] testData = new String[] {k(ConfigKey.NAME), "image", k(ConfigKey.FROM), "base" };

0 commit comments

Comments
 (0)