Skip to content

Commit

Permalink
fix for #130
Browse files Browse the repository at this point in the history
introduced cmd and entrypoint with exec and shell form support.
the change is not backward compatible to the previous version.

Signed-off-by: Oleg Sigida <oleg.sigida@gmail.com>
  • Loading branch information
osigida authored and rhuss committed Jun 14, 2015
1 parent 901064c commit e9e00fd
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 83 deletions.
41 changes: 37 additions & 4 deletions doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ of an image configuration. The available subelements are

* **assembly** specifies the assembly configuration as described in
[Build Assembly](#build-assembly)
* **command** is the command to execute by default (i.e. if no command
is provided when a container for this image is started).
* **cmd** A command to execute by default.
[Start-up Arguments]
* **entrypoint** An ENTRYPOINT allows you to configure a container that will run as an executable.
[Start-up Arguments]
* **env** hold environments as described in
[Setting Environment Variables](#setting-environment-variables).
* **from** specifies the base image which should be used for this
Expand Down Expand Up @@ -265,7 +267,16 @@ Here's an example:
<volumes>
<volume>/path/to/expose</volume>
</volumes>
<command>java /opt/demo/server.jar</command>

<entryPoint>
<!-- exec form for ENTRYPOINT -->
<params>
<param>java</param>
<param>-jar</param>
<param>/opt/demo/server.jar</param>
</params>
</entryPoint>

<assembly>
<mode>dir</mode>
<basedir>/opt/demo</basedir>
Expand Down Expand Up @@ -317,7 +328,29 @@ Here's an example:
`jboss:jboss:jboss` would be required.

In the event you do not need to include any artifacts with the image, you may
safely omit this element from the configuration.
safely omit this element from the configuration.

##### Start-up Arguments
Using `entryPoint` and `cmd` it is possible to specify [entry point](https://docs.docker.com/reference/builder/#entrypoint) or [cmd](https://docs.docker.com/reference/builder/#cmd) for a container

* **shell** shell form, translated to the docker file as is
* **params** list of arguments which will transformed into exec form
Either shell or params should be specified.

Example:

````xml
<entryPoint>
<!-- shell form for entry point -->
<shell>java -jar /opt/demo/server.jar</shell>
<!-- or exec form for entry -->
<params>
<param>java</param>
<param>-jar</param>
<param>/opt/demo/server.jar</param>
</params>
</entryPoint>
````

##### Docker Assembly

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
<version>1.5.5</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>

<!-- =============================================================================== -->

<dependency>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jolokia/docker/maven/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected void executeInternal(DockerAccess dockerAccess) throws DockerAccessExc
for (ImageConfiguration imageConfig : getImages()) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
if (buildConfig != null) {
buildConfig.validate();
String imageName = imageConfig.getName();
buildImage(imageName, imageConfig, dockerAccess);
tagImage(imageName, imageConfig, dockerAccess);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jolokia/docker/maven/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ ContainerCreateConfig createContainerConfig(DockerAccess docker, String imageNam
.entrypoint(runConfig.getEntrypoint())
.exposedPorts(mappedPorts.getContainerPorts())
.environment(runConfig.getEnvPropertyFile(), runConfig.getEnv(), project.getProperties())
.command(runConfig.getCommand())
.command(runConfig.getCmd())
.hostConfig(createContainerHostConfig(docker, runConfig, mappedPorts));
VolumeConfiguration volumeConfig = runConfig.getVolumeConfiguration();
if (volumeConfig != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public File createDockerTarArchive(String imageName, MojoParameters params, Buil
DockerFileBuilder builder = createDockerFileBuilder(buildConfig, assemblyConfig);
builder.write(buildDirs.getOutputDirectory());
}
return createTarball(buildDirs,extraDir,assemblyConfig.getMode());
return createTarball(buildDirs, extraDir, assemblyConfig.getMode());

} catch (IOException e) {
throw new MojoExecutionException(String.format("Cannot create Dockerfile in %s", buildDirs.getOutputDirectory()), e);
Expand Down Expand Up @@ -158,12 +158,15 @@ DockerFileBuilder createDockerFileBuilder(BuildImageConfiguration buildConfig, A
}

builder.baseImage(buildConfig.getFrom());
builder.command((String[]) null); // Use command from base image (gets overwritten below if explicitly set)

if (buildConfig.getCommand() != null) {
builder.command(EnvUtil.splitOnSpaceWithEscape(buildConfig.getCommand()));
if (buildConfig.getCmd() != null){
builder.cmd(buildConfig.getCmd());
}

if (buildConfig.getEntryPoint() != null){
builder.entryPoint(buildConfig.getEntryPoint());
}

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.io.IOException;
import java.util.*;

import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.plexus.util.FileUtils;
import org.jolokia.docker.maven.config.Arguments;

/**
* Create a dockerfile
Expand All @@ -15,6 +17,8 @@
*/
public class DockerFileBuilder {

private static final Joiner JOIN_ON_COMMA = Joiner.on("\",\"");

// Base image to use as from
private String baseImage;

Expand All @@ -24,9 +28,8 @@ public class DockerFileBuilder {
// Basedir to be export
private String basedir = "/maven";

// Default command and arguments
private String command = "true";
private String[] arguments = new String[0];
private Arguments entryPoint;
private Arguments cmd;

private Boolean exportBasedir = null;

Expand Down Expand Up @@ -75,21 +78,34 @@ public String content() throws IllegalArgumentException {
addPorts(b);
addVolumes(b);
addEntries(b);
addCommands(b);
addCmd(b);
addEntryPoint(b);

return b.toString();
}

private void addCommands(StringBuilder b) {
if (command != null) {
b.append("CMD [\"").append(command).append("\"");
for (String arg : arguments) {
b.append(",\"").append(arg).append("\"");
}
b.append("]").append("\n");
private void addEntryPoint(StringBuilder b){
if (entryPoint != null) {
buildArguments(b, "ENTRYPOINT", entryPoint);
}
}

private void addCmd(StringBuilder b){
if (cmd != null) {
buildArguments(b, "CMD", cmd);
}
}

private static void buildArguments(StringBuilder b, String name, Arguments arguments) {
b.append(name).append(" ");
if (arguments.getShell() != null) {
b.append(arguments.getShell());
} else {
b.append("[\"").append(JOIN_ON_COMMA.join(arguments.getParams())).append("\"]");
}
b.append("\n");
}

private void addEntries(StringBuilder b) {
List<String> destinations = new ArrayList<>();
for (AddEntry entry : addEntries) {
Expand Down Expand Up @@ -123,7 +139,7 @@ private void addPorts(StringBuilder b) {
if (ports.size() > 0) {
b.append("EXPOSE");
for (Integer port : ports) {
b.append(" " + port);
b.append(" ").append(port);
}
b.append("\n");
}
Expand Down Expand Up @@ -172,22 +188,23 @@ public DockerFileBuilder basedir(String dir) {
return this;
}

public DockerFileBuilder command(String ... args) {
if (args == null || args.length == 0) {
this.command = null;
this.arguments = new String[0];
} else {
this.command = args[0];
this.arguments = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[0];
}
public DockerFileBuilder cmd(Arguments cmd) {
cmd.validate();
this.cmd = cmd;
return this;
}

public DockerFileBuilder entryPoint(Arguments entryPoint) {
entryPoint.validate();
this.entryPoint = entryPoint;
return this;
}

public DockerFileBuilder user(String user) {
this.user = user;
return this;
}

public DockerFileBuilder add(String source, String destination) {
this.addEntries.add(new AddEntry(source, destination));
return this;
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/org/jolokia/docker/maven/config/Arguments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.jolokia.docker.maven.config;

import java.util.ArrayList;
import java.util.List;

public class Arguments {

/**
* @parameter
*/
private String shell;

/**
* @parameter
*/
private List<String> params;

public void setShell(String shell) {
this.shell = shell;
}

public String getShell() {
return shell;
}

public void setParams(List<String> params) {
this.params = params;
}

public List<String> getParams() {
return params;
}

public void validate() throws IllegalArgumentException {
if (shell == null && (params == null || params.isEmpty())){
throw new IllegalArgumentException("Argument conflict, either shell or params should be specified");
}
if (shell != null && params != null) {
throw new IllegalArgumentException("Argument conflict, either shell or params should be specified");
}
}

public static class Builder {
private String shell;
private List<String> params;

public static Builder get(){
return new Builder();
}

public Builder withShell(String shell){
this.shell = shell;
return this;
}

public Builder withParam(String param){
if (params == null) {
params = new ArrayList<>();
}
this.params.add(param);
return this;
}

public Arguments build(){
Arguments a = new Arguments();
a.setShell(shell);
a.setParams(params);
return a;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.jolokia.docker.maven.config;

import java.util.*;
import org.apache.maven.plugin.MojoExecutionException;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* @author roland
Expand Down Expand Up @@ -42,12 +46,17 @@ public class BuildImageConfiguration {
/**
* @parameter
*/
private Map<String,String> env;
private Map<String,String> env;

/**
* @parameter
*/
private String command;
private Arguments entryPoint;

/**
* @parameter
*/
private Arguments cmd;

/**
* @parameter
Expand Down Expand Up @@ -88,8 +97,12 @@ public Map<String, String> getEnv() {
return env;
}

public String getCommand() {
return command;
public Arguments getCmd() {
return cmd;
}

public Arguments getEntryPoint() {
return entryPoint;
}

public static class Builder {
Expand Down Expand Up @@ -135,13 +148,33 @@ public Builder env(Map<String, String> env) {
return this;
}

public Builder command(String command) {
config.command = command;
public Builder cmd(String cmd) {
if (config.cmd == null) {
config.cmd = new Arguments();
}
config.cmd.setShell(cmd);
return this;
}

public Builder entryPoint(String entryPoint) {
if (config.entryPoint == null) {
config.entryPoint = new Arguments();
}
config.entryPoint.setShell(entryPoint);
return this;
}

public BuildImageConfiguration build() {
return config;
}
}

public void validate() throws MojoExecutionException {
if (entryPoint != null) {
entryPoint.validate();
}
if (cmd != null) {
cmd.validate();
}
}
}
Loading

0 comments on commit e9e00fd

Please sign in to comment.