Skip to content

Commit

Permalink
Fix fabric8io#1264: Failure referencing a previous staged image in FR…
Browse files Browse the repository at this point in the history
…OM clause
  • Loading branch information
rohanKanojia committed Oct 22, 2019
1 parent 15f97d0 commit 1fa42c1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Treat bridged and default network mode the same #1234
- Fix NPE when cacheFrom is missing from config #1274
- Fix healthy option regression introduced in 0.25.0 #1279
- Failure referencing a previous staged image in FROM clause #1264

* **0.31.0** (2019-08-10)
- Fix test cases on Windows ([#1220](https://github.com/fabric8io/docker-maven-plugin/issues/1220))
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -53,9 +54,17 @@ private DockerFileUtil() {}
public static List<String> extractBaseImages(File dockerFile, FixedStringSearchInterpolator interpolator) throws IOException {
List<String[]> fromLines = extractLines(dockerFile, "FROM", interpolator);
LinkedList<String> result = new LinkedList<>();
Map<String, String> fromAlias = new HashMap<>();
for (String[] fromLine : fromLines) {
if (fromLine.length > 1) {
result.add(fromLine[1]);
if (fromLine.length == 2) { // FROM image:tag use case
result.add(fromLine[1]);
} else if (fromLine.length == 4) { // FROM image:tag AS alias use case
if (!fromAlias.containsKey(fromLine[1])) {
result.add(fromLine[1]);
}
fromAlias.put(fromLine[3], fromLine[1]);
}
}
}
return result;
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public void testMultiStage() throws Exception {
assertEquals(false, fromClauses.hasNext());
}

@Test
public void testMultiStageNamed() throws Exception {
File toTest = copyToTempDir("Dockerfile_multi_stage_named_build_stages");
Iterator<String> fromClauses = DockerFileUtil.extractBaseImages(
toTest, FixedStringSearchInterpolator.create()).iterator();

assertEquals("fabric8/s2i-java", fromClauses.next());
assertEquals(false, fromClauses.hasNext());
}

private File copyToTempDir(String resource) throws IOException {
File dir = Files.createTempDirectory("d-m-p").toFile();
File ret = new File(dir, "Dockerfile");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Dockerfile with multi-stage builds with named stages
FROM fabric8/s2i-java as BUILD

RUN ls -la /

FROM BUILD as DEVELOPMENT

RUN ls -la /

FROM BUILD AS DEPOYLABLE

RUN ls -la /

0 comments on commit 1fa42c1

Please sign in to comment.