diff --git a/doc/changelog.md b/doc/changelog.md index 4df96be6a..0ee7404c5 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -1,6 +1,8 @@ # ChangeLog * **0.11.5-M1** + - Add a `user` parameter to the assembly configuration so that the added files are created for this user + (#53, #175) - Fix problem when creating intermediate archive for collecting assembly files introduced with #139. The container can be now set with "mode" in the assembly configuration with the possible values `dir`, `tar`, `tgz` and `zip` (#171) @@ -8,6 +10,7 @@ - Fixed references to docker hub in documentation (#169) - Fixed registry authentication lookup (#146) + * **0.11.4** - Fixed documentation for available properties - Changed property `docker.assembly.exportBase` to `docker.assembly.exportBaseDir` (#164) diff --git a/doc/manual.md b/doc/manual.md index 90580ae12..d58b5fff6 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -8,6 +8,7 @@ - [`docker:push`](#dockerpush) - [`docker:remove`](#dockerremove) - [`docker:logs`](#dockerlogs) +* [Assembly Configuration](#build-assembly) * [External Configuration](#external-configuration) * [Registry Handling](#registry-handling) * [Authentication](#authentication) @@ -300,8 +301,18 @@ Here's an example: * **mode** specifies how the assembled files should be collected. By default the files a simply copied (`dir`), but can be set to be a Tar- (`tar`), compressed Tar- (`tgz`) or Zip- (`zip`) Archive. The archive formats have the advantage that file permission can be preserved better (since the copying is - independent from the underlying files systems), but might trigges internal bugs from the Maven assembler (as + independent from the underlying files systems), but might triggers internal bugs from the Maven assembler (as it has been in #171) +* **user** can be used to specify the user and group under which the files should be added. It has the general format + `user[:group[:run-user]]`. The user and group can be given either as numeric user- and group-id or as names. The group + id is optional. If a third part is given, then the build changes to user `root` before changing the ownerships, + changes the ownerships and then change to user `run-user` which is then used for the final command to execute. This feature + might be needed, if the base image already changed the user (e.g. to 'jboss') so that a `chown` from root to this user would fail. + For example, the image `jboss/wildfly` use a "jboss" user under which all commands are executed. Adding files in Docker + always happens under the UID root. These files can only be changed to "jboss" is the `chown` command is executed as root. + For the following commands to be run again as "jboss" (like the final `standalone.sh`), the plugin switches back to + user `jboss` (this is this "run-user") after changing the file ownership. For this example a specification of + `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. diff --git a/samples/data-jolokia-demo/pom.xml b/samples/data-jolokia-demo/pom.xml index dd4915a3b..2fdaff79f 100644 --- a/samples/data-jolokia-demo/pom.xml +++ b/samples/data-jolokia-demo/pom.xml @@ -356,6 +356,45 @@ + + + wildfly + + + + org.jolokia + docker-maven-plugin + + + + + jolokia/${project.artifactId}-wildfly:${project.version} + + jboss/wildfly:8.2.0.Final + + jboss:jboss:jboss + /opt/jboss/wildfly/standalone/deployments + assembly.xml + + + + + jolokia.port:8080 + + + + http://${docker.host.address}:${jolokia.port}/jolokia + + + + + + + + + + + tomcat diff --git a/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java b/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java index fedea06c6..dc806b6c7 100644 --- a/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java +++ b/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java @@ -364,7 +364,7 @@ protected String getConfiguredRegistry(ImageConfiguration imageConfig) { protected void checkImageWithAutoPull(DockerAccess docker, String name, String registry) throws DockerAccessException, MojoExecutionException { if (!docker.hasImage(name)) { if (autoPull) { - docker.pullImage(name, prepareAuthConfig(image,registry), registry); + docker.pullImage(name, prepareAuthConfig(name,registry), registry); ImageName imageName = new ImageName(name); if (registry != null && !imageName.hasRegistry()) { // If coming from a registry which was not contained in the original name, add a tag from the diff --git a/src/main/java/org/jolokia/docker/maven/access/ContainerCreateConfig.java b/src/main/java/org/jolokia/docker/maven/access/ContainerCreateConfig.java index 2de835be9..a3b250146 100644 --- a/src/main/java/org/jolokia/docker/maven/access/ContainerCreateConfig.java +++ b/src/main/java/org/jolokia/docker/maven/access/ContainerCreateConfig.java @@ -50,7 +50,7 @@ public ContainerCreateConfig entrypoint(String entrypoint) { private JSONArray splitOnWhiteSpace(String entrypoint) { JSONArray a = new JSONArray(); - for (String s : EnvUtil.splitWOnSpaceWithEscape(entrypoint)) { + for (String s : EnvUtil.splitOnSpaceWithEscape(entrypoint)) { a.put(s); } return a; diff --git a/src/main/java/org/jolokia/docker/maven/assembly/DockerAssemblyManager.java b/src/main/java/org/jolokia/docker/maven/assembly/DockerAssemblyManager.java index c387186f9..07c2d7bb8 100644 --- a/src/main/java/org/jolokia/docker/maven/assembly/DockerAssemblyManager.java +++ b/src/main/java/org/jolokia/docker/maven/assembly/DockerAssemblyManager.java @@ -151,7 +151,8 @@ DockerFileBuilder createDockerFileBuilder(BuildImageConfiguration buildConfig, A if (assemblyConfig != null) { builder.add("maven", "") .basedir(assemblyConfig.getBasedir()) - .exportBasedir(assemblyConfig.exportBasedir()); + .user(assemblyConfig.getUser()) + .exportBasedir(assemblyConfig.exportBasedir()); } else { builder.exportBasedir(false); } @@ -160,7 +161,7 @@ DockerFileBuilder createDockerFileBuilder(BuildImageConfiguration buildConfig, A builder.command((String[]) null); // Use command from base image (gets overwritten below if explicitly set) if (buildConfig.getCommand() != null) { - builder.command(EnvUtil.splitWOnSpaceWithEscape(buildConfig.getCommand())); + builder.command(EnvUtil.splitOnSpaceWithEscape(buildConfig.getCommand())); } return builder; diff --git a/src/main/java/org/jolokia/docker/maven/assembly/DockerFileBuilder.java b/src/main/java/org/jolokia/docker/maven/assembly/DockerFileBuilder.java index c613c3037..b841b3438 100644 --- a/src/main/java/org/jolokia/docker/maven/assembly/DockerFileBuilder.java +++ b/src/main/java/org/jolokia/docker/maven/assembly/DockerFileBuilder.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.*; +import org.apache.commons.lang3.StringUtils; import org.codehaus.plexus.util.FileUtils; /** @@ -28,7 +29,10 @@ public class DockerFileBuilder { private String[] arguments = new String[0]; private Boolean exportBasedir = null; - + + // User under which the files should be added + private String user; + // List of files to add. Source and destination follow except that destination // in interpreted as a relative path to the exportDir // See also http://docs.docker.io/reference/builder/#add @@ -67,15 +71,10 @@ public String content() throws IllegalArgumentException { b.append("FROM ").append(baseImage != null ? baseImage : DockerAssemblyManager.DEFAULT_DATA_BASE_IMAGE).append("\n"); b.append("MAINTAINER ").append(maintainer).append("\n"); - // Environment variable support addEnv(b); - // Ports addPorts(b); - // Volume export addVolumes(b); - // Entries addEntries(b); - // Default command mit args addCommands(b); return b.toString(); @@ -90,11 +89,26 @@ private void addCommands(StringBuilder b) { b.append("]").append("\n"); } } - + private void addEntries(StringBuilder b) { + List destinations = new ArrayList<>(); for (AddEntry entry : addEntries) { - b.append("COPY ").append(entry.source).append(" ") - .append(basedir).append("/").append(entry.destination).append("\n"); + String dest = (basedir.equals("/") ? "" : basedir) + "/" + entry.destination; + destinations.add(dest); + b.append("COPY ").append(entry.source).append(" ").append(dest).append("\n"); + } + if (user != null) { + String[] userParts = StringUtils.split(user,':'); + String userArg = userParts.length > 1 ? userParts[0] + ":" + userParts[1] : userParts[0]; + String cmd = "RUN [\"chown\", \"-R\", \"" + userArg + "\",\"" + + StringUtils.join(destinations, "\",\"") + "\"]\n"; + if (userParts.length > 2) { + b.append("USER root\n"); + b.append(cmd); + b.append("USER ").append(userParts[2]).append("\n"); + } else { + b.append(cmd); + } } } @@ -169,6 +183,11 @@ public DockerFileBuilder command(String ... args) { 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; diff --git a/src/main/java/org/jolokia/docker/maven/util/EnvUtil.java b/src/main/java/org/jolokia/docker/maven/util/EnvUtil.java index 0dee5a368..e2a9ebd36 100644 --- a/src/main/java/org/jolokia/docker/maven/util/EnvUtil.java +++ b/src/main/java/org/jolokia/docker/maven/util/EnvUtil.java @@ -83,7 +83,7 @@ public static List splitOnLastColon(List listToSplit) { return Collections.emptyList(); } - public static String[] splitWOnSpaceWithEscape(String toSplit) { + public static String[] splitOnSpaceWithEscape(String toSplit) { String[] split = toSplit.split("(?