Skip to content

Commit

Permalink
Support for docker build --pull option
Browse files Browse the repository at this point in the history
Within the docker build options there is now --pull. This enables an attempt to pull a newer version of the image always.

Additional information can be found here:
https://docs.docker.com/engine/reference/commandline/build/
  • Loading branch information
rhuss authored and rohanKanojia committed Apr 7, 2019
1 parent dc82c82 commit b6bb1af
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Obtain container ip address from custom network for tcp/http wait
- Fix http (SSL) ping with 'allowAllHosts' flag enabled
- Update to jnr-unixsocket 0.22
- Support for new docker build --pull option (#1191)
- Enhance @sha256 digest for tags in FROM (image_name:image_tag@sha256<digest>) ([#541](https://github.com/fabric8io/docker-maven-plugin/issues/541))
- Support docker SHELL setting for runCmds (#1157)
- Added 'autoRemove' option for running containers (#1179)
Expand Down
4 changes: 4 additions & 0 deletions src/main/asciidoc/inc/build/_configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ A provided `<from>` takes precedence over the name given here. This tag is usefu
| *maintainer*
| The author (`MAINTAINER`) field for the generated image

| *pull*
| Always attempt to pull a newer version of the image. This can be overwritten by setting a system property `docker.pull` when running Maven.

| *nocache*
| Don't use Docker's build cache. This can be overwritten by setting a system property `docker.nocache` when running Maven.

Expand Down Expand Up @@ -125,6 +128,7 @@ remote API.
<build>
<from>java:8u40</from>
<maintainer>[email protected]</maintainer>
<pull>true</pull>
<tags>
<tag>latest</tag>
<tag>${project.version}</tag>
Expand Down
5 changes: 4 additions & 1 deletion src/main/asciidoc/inc/external/_property_configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ when a `docker.from` or a `docker.fromExt` is set.
| *docker.assembly.dockerFileDir*
| specifies a directory containing an external Dockerfile that will be used to create the image. This is deprecated please use `docker.dockerFileDir` or `docker.dockerFile` instead.

| *docker.pull*
| Always attempt to pull a newer version of the image, This can be overwritten by setting a system property `docker.pull` when running Maven.

| *docker.nocache*
| Don't use Docker's build cache.This can be overwritten by setting a system property `docker.nocache` when running Maven.
| Don't use Docker's build cache. This can be overwritten by setting a system property `docker.nocache` when running Maven.

| *docker.bind.idx*
| Sets a list of paths to bind/expose in the container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public BuildOptions noCache(boolean noCache) {
return this;
}

public BuildOptions pull(boolean pull) {
options.put("pull", pull ? "1" : "0");
return this;
}

public BuildOptions buildArgs(Map<String, String> buildArgs) {
if (buildArgs != null && buildArgs.size() > 0) {
options.put("buildargs", JsonFactory.newJsonObject(buildArgs).toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ public class BuildImageConfiguration implements Serializable {
private Boolean nocache;

@Parameter
private Boolean optimise;
private Boolean pull = false;

@Parameter
private Boolean optimise = false;

@Parameter
private List<String> volumes;
Expand Down Expand Up @@ -249,6 +252,10 @@ public CleanupMode cleanupMode() {
return CleanupMode.parse(cleanup != null ? cleanup : DEFAULT_CLEANUP);
}

public boolean pull() {
return pull;
}

public boolean nocache() {
return nocache != null ? nocache : false;
}
Expand Down Expand Up @@ -457,6 +464,20 @@ public Builder nocache(Boolean nocache) {
return this;
}

public Builder pull(Boolean pull) {
if (pull != null) {
config.pull = pull;
}
return this;
}

public Builder nocache(String nocache) {
if (nocache != null) {
config.nocache = Boolean.valueOf(nocache);
}
return this;
}

public Builder optimise(Boolean optimise) {
config.optimise = optimise;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum ConfigKey {
CPUSHARES,
CPUS,
CPUSET,
PULL,
NOCACHE,
OPTIMISE,
CMD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private BuildImageConfiguration extractBuildConfiguration(ImageConfiguration fro
return new BuildImageConfiguration.Builder()
.cmd(extractArguments(valueProvider, CMD, config == null ? null : config.getCmd()))
.cleanup(valueProvider.getString(CLEANUP, config == null ? null : config.getCleanup()))
.pull(valueProvider.getBoolean(PULL, config == null ? null : config.pull()))
.nocache(valueProvider.getBoolean(NOCACHE, config == null ? null : config.getNoCache()))
.optimise(valueProvider.getBoolean(OPTIMISE, config == null ? null : config.getOptimise()))
.entryPoint(extractArguments(valueProvider, ENTRYPOINT, config == null ? null : config.getEntryPoint()))
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/io/fabric8/maven/docker/service/BuildService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void buildImage(ImageConfiguration imageConfig, ImagePullManager imagePul
autoPullBaseImage(imageConfig, imagePullManager, buildContext);
}

buildImage(imageConfig, buildContext.getMojoParameters(), checkForNocache(imageConfig), addBuildArgs(buildContext));
buildImage(imageConfig, buildContext.getMojoParameters(), checkForNoPull(imageConfig), checkForNocache(imageConfig), addBuildArgs(buildContext));
}

public void tagImage(String imageName, ImageConfiguration imageConfig) throws DockerAccessException {
Expand All @@ -86,12 +86,13 @@ public void tagImage(String imageName, ImageConfiguration imageConfig) throws Do
*
* @param imageConfig the image configuration
* @param params mojo params for the project
* @param pull if not null, always attempt to pull a newer version of the image.
* @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
* @param buildArgs
* @throws DockerAccessException
* @throws MojoExecutionException
*/
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean noCache, Map<String, String> buildArgs)
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean pull, boolean noCache, Map<String, String> buildArgs)
throws DockerAccessException, MojoExecutionException {

String imageName = imageConfig.getName();
Expand Down Expand Up @@ -125,6 +126,7 @@ protected void buildImage(ImageConfiguration imageConfig, MojoParameters params,
.dockerfile(getDockerfileName(buildConfig))
.forceRemove(cleanupMode.isRemove())
.noCache(noCache)
.pull(pull)
.buildArgs(mergedBuildMap);
String newImageId = doBuildImage(imageName, dockerArchive, opts);
log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);
Expand Down Expand Up @@ -278,6 +280,16 @@ private List<String> extractBaseFromDockerfile(BuildImageConfiguration buildConf
return fromImage;
}

private boolean checkForNoPull(ImageConfiguration imageConfig) {
String pull = System.getProperty("docker.pull");
if (pull != null) {
return pull.length() == 0 || Boolean.valueOf(pull);
} else {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
return buildConfig.pull();
}
}

private boolean checkForNocache(ImageConfiguration imageConfig) {
String nocache = System.getProperty("docker.nocache");
if (nocache != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void setup() throws Exception {
public void testBuildImageWithCleanup() throws Exception {
givenAnImageConfiguration(true);
givenImageIds(OLD_IMAGE_ID, NEW_IMAGE_ID);
whenBuildImage(true,false);
whenBuildImage(true, false, false);
thenImageIsBuilt();
thenOldImageIsRemoved();
}
Expand All @@ -90,7 +90,7 @@ public void testBuildImageWithCleanup() throws Exception {
public void testBuildImageWithNoCleanup() throws Exception {
givenAnImageConfiguration(false);
givenImageIds(OLD_IMAGE_ID, NEW_IMAGE_ID);
whenBuildImage(false,false);
whenBuildImage(false, false, false);
thenImageIsBuilt();
thenOldImageIsNotRemoved();
}
Expand All @@ -99,7 +99,7 @@ public void testBuildImageWithNoCleanup() throws Exception {
public void testCleanupCachedImage() throws Exception {
givenAnImageConfiguration(true);
givenImageIds(OLD_IMAGE_ID, OLD_IMAGE_ID);
whenBuildImage(false, false);
whenBuildImage(false, false, false);
thenImageIsBuilt();
thenOldImageIsNotRemoved();
}
Expand All @@ -108,7 +108,7 @@ public void testCleanupCachedImage() throws Exception {
public void testCleanupNoExistingImage() throws Exception {
givenAnImageConfiguration(true);
givenImageIds(null, NEW_IMAGE_ID);
whenBuildImage(false, false);
whenBuildImage(false, false, false);
thenImageIsBuilt();
thenOldImageIsNotRemoved();
}
Expand Down Expand Up @@ -190,7 +190,7 @@ private void thenOldImageIsRemoved() throws DockerAccessException {
}};
}

private void whenBuildImage(boolean cleanup, boolean nocache) throws DockerAccessException, MojoExecutionException {
private void whenBuildImage(boolean cleanup, boolean pull, boolean nocache) throws DockerAccessException, MojoExecutionException {
new Expectations() {{
docker.buildImage(withEqual(imageConfig.getName()), (File) any, (BuildOptions) any);
}};
Expand All @@ -200,7 +200,7 @@ private void whenBuildImage(boolean cleanup, boolean nocache) throws DockerAcces
}};
}

buildService.buildImage(imageConfig, params, nocache, Collections.<String, String>emptyMap());
buildService.buildImage(imageConfig, params, pull, nocache, Collections.<String, String>emptyMap());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void givenAnImageConfiguration() {
}

private void whenBuildImage() throws DockerAccessException, MojoExecutionException {
buildService.buildImage(imageConfig, params, false, Collections.<String, String>emptyMap());
buildService.buildImage(imageConfig, params, false, false, Collections.<String, String>emptyMap());
}

private void thenImageIsBuilt() throws DockerAccessException {
Expand Down

0 comments on commit b6bb1af

Please sign in to comment.