Skip to content

Commit

Permalink
Merge pull request fabric8io#173 from dawidmalina/squash-commands
Browse files Browse the repository at this point in the history
Squash all RUN commands into one layer
  • Loading branch information
mattnworb committed Jan 11, 2016
2 parents f0f580e + f5ad00b commit 86d4bb7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/com/spotify/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public class BuildMojo extends AbstractDockerMojo {

private List<String> runList;

/** Flag to squash all run commands into one layer. Defaults to false. */
@Parameter(property = "squashRunCommands", defaultValue = "false")
private boolean squashRunCommands;

/** All resources will be copied to this directory before building the image. */
@Parameter(property = "project.build.directory")
protected String buildDirectory;
Expand Down Expand Up @@ -568,8 +572,12 @@ private void createDockerFile(final String directory, final List<String> filesTo
}

if (runList != null && !runList.isEmpty()) {
for (final String run : runList) {
commands.add("RUN " + run);
if (squashRunCommands) {
commands.add("RUN " + Joiner.on(" &&\\\n\t").join(runList));
} else {
for (final String run : runList) {
commands.add("RUN " + run);
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/spotify/docker/BuildMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ public class BuildMojoTest extends AbstractMojoTestCase {
"VOLUME /example2"
);

private static final List<String> GENERATED_DOCKERFILE_WITH_SQUASH_COMMANDS = Arrays.asList(
"FROM busybox",
"MAINTAINER user",
"ENV FOO BAR",
"WORKDIR /opt/app",
"ADD resources/parent/child/child.xml resources/parent/child/",
"ADD resources/parent/parent.xml resources/parent/",
"ADD copy2.json .",
"RUN ln -s /a /b &&\\",
"\twget 127.0.0.1:8080",
"EXPOSE 8080 8081",
"USER app",
"ENTRYPOINT date",
"CMD [\"-u\"]"
);

private static final List<String> PROFILE_GENERATED_DOCKERFILE = Arrays.asList(
"FROM busybox",
"ENV APP_NAME FOOBAR",
Expand Down Expand Up @@ -268,6 +284,22 @@ public void testBuildWithGeneratedDockerfile() throws Exception {
Files.readAllLines(Paths.get("target/docker/Dockerfile"), UTF_8));
}

public void testBuildWithGeneratedDockerfileWithSquashCommands() throws Exception {
final File pom = getTestFile("src/test/resources/pom-build-generated-dockerfile-with-squash-commands.xml");
assertNotNull("Null pom.xml", pom);
assertTrue("pom.xml does not exist", pom.exists());

final BuildMojo mojo = setupMojo(pom);
final DockerClient docker = mock(DockerClient.class);
mojo.execute(docker);

verify(docker).build(eq(Paths.get("target/docker")), eq("busybox"),
any(AnsiProgressHandler.class));
assertFilesCopied();
assertEquals("wrong dockerfile contents", GENERATED_DOCKERFILE_WITH_SQUASH_COMMANDS,
Files.readAllLines(Paths.get("target/docker/Dockerfile"), UTF_8));
}

public void testBuildGeneratedDockerFile_CopiesEntireDirectory() throws Exception {
final File pom = getTestFile("src/test/resources/pom-build-copy-entire-directory.xml");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>Docker Maven Plugin Test Pom</name>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<configuration>
<!-- a DockerFile should be generated since dockerDirectory is not specified -->
<baseImage>busybox</baseImage>
<maintainer>user</maintainer>
<dockerHost>http://host:2375</dockerHost>
<imageName>busybox</imageName>
<entryPoint>date</entryPoint>
<env>
<FOO>BAR</FOO>
</env>
<exposes>
<expose>8081</expose>
<expose>8080</expose>
</exposes>
<cmd>-u</cmd>
<!-- same copying tests as pom-build-docker-directory.xml, but make sure it works with auto-generated -->
<!-- docker file. pom-build-docker-directory.xml specified its own docker directory-->
<resources>
<resource>
<!-- test we handle all elements correctly -->
<targetPath>resources</targetPath>
<directory>src/test/resources/copy1</directory>
<include>**/*.xml</include>
<exclude>**/*exclude*</exclude>
</resource>
<resource>
<!-- test we handle missing elements correctly -->
<directory>src/test/resources/copy2</directory>
</resource>
</resources>
<runs>
<run>ln -s /a /b</run>
<run>wget 127.0.0.1:8080</run>
</runs>
<squashRunCommands>true</squashRunCommands>
<workdir>/opt/app</workdir>
<user>app</user>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 86d4bb7

Please sign in to comment.