Skip to content

Commit

Permalink
#441 : Add documentation
Browse files Browse the repository at this point in the history
Add some documentation and update changelog.
  • Loading branch information
rhuss committed May 17, 2016
2 parents 7c52adc + aec1585 commit e87fd3c
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 13 deletions.
3 changes: 2 additions & 1 deletion doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
- More robust response stream parsing (#436)
- Add `docker.dockerFileDir` and `docker.dockerFile` to the properties configuration provider. (#438)
- Fix splitting of bind volumes for Windows pathes (#443)

- Add new build config option `user` for switching the user at the end of the Dockerfile. `docker.user` can be used
for the properties configuration provider (#441)
* **0.15.1** (2016-05-03)
- Fix push / pull progress bar ([#91](https://github.com/fabric8io/docker-maven-plugin/issues/91))
- Allow empty environment variable ([#434](https://github.com/fabric8io/docker-maven-plugin/issues/434))
Expand Down
1 change: 1 addition & 0 deletions doc/manual/docker-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ of an image configuration. In addition to `<dockerFileDir>` and
* **skip** if set to true disables building of the image. This config option is best used together with a maven property
* **tags** contains a list of additional `tag` elements with which an
image is to be tagged after the build.
* **user** is the user to which the Dockerfile should switch to the end (corresponds to the `USER` Dockerfile directive).
* **volumes** contains a list of `volume` elements to create a container
volume.
* **workdir** the directory to change to when starting the container.
Expand Down
3 changes: 2 additions & 1 deletion doc/manual/external-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ one image layer is created.
* **docker.restartPolicy.name** Container restart policy
* **docker.restartPolicy.retry** Max restrart retries if `on-failure` used
* **docker.tags.idx** defines a list of tags to apply to a built image
* **docker.user** Container user
* **docker.user** User to switch to at the end of a Dockerfile. Not to confuse with `docker.username` which is used for
authentication when interacting with a Docker registry.
* **docker.volumes.idx** defines a list of volumes to expose when building an image
* **docker.volumesFrom.idx** defines a list of image aliases from which
the volumes should be mounted of the container. The list semantics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ DockerFileBuilder createDockerFileBuilder(BuildImageConfiguration buildConfig, A
.labels(buildConfig.getLabels())
.expose(buildConfig.getPorts())
.run(buildConfig.getRunCmds())
.volumes(buildConfig.getVolumes());
.volumes(buildConfig.getVolumes())
.user(buildConfig.getUser());
if (buildConfig.getMaintainer() != null) {
builder.maintainer(buildConfig.getMaintainer());
}
Expand All @@ -272,7 +273,7 @@ DockerFileBuilder createDockerFileBuilder(BuildImageConfiguration buildConfig, A
if (assemblyConfig != null) {
builder.add(ASSEMBLY_NAME, "")
.basedir(assemblyConfig.getBasedir())
.user(assemblyConfig.getUser())
.assemblyUser(assemblyConfig.getUser())
.exportBasedir(assemblyConfig.exportBasedir());
} else {
builder.exportBasedir(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class DockerFileBuilder {
private Boolean exportBasedir = null;

// User under which the files should be added
private String assemblyUser;

// User to run as
private String user;

// List of files to add. Source and destination follow except that destination
Expand Down Expand Up @@ -103,9 +106,17 @@ public String content() throws IllegalArgumentException {
addCmd(b);
addEntryPoint(b);

addUser(b);

return b.toString();
}

private void addUser(StringBuilder b) {
if (user != null) {
DockerFileKeyword.USER.addTo(b, user);
}
}

private void addWorkdir(StringBuilder b) {
if (workdir != null) {
DockerFileKeyword.WORKDIR.addTo(b, workdir);
Expand Down Expand Up @@ -135,11 +146,11 @@ private static void buildArguments(StringBuilder b, DockerFileKeyword key, Argum
}

private void addEntries(StringBuilder b) {
if (user != null) {
if (assemblyUser != null) {
String tmpDir = createTempDir();
copyAddEntries(b,tmpDir);

String[] userParts = StringUtils.split(user, ":");
String[] userParts = StringUtils.split(assemblyUser, ":");
String userArg = userParts.length > 1 ? userParts[0] + ":" + userParts[1] : userParts[0];
String chmod = "chown -R " + userArg + " " + tmpDir + " && cp -rp " + tmpDir + "/* / && rm -rf " + tmpDir;
if (userParts.length > 2) {
Expand Down Expand Up @@ -275,6 +286,11 @@ public DockerFileBuilder entryPoint(Arguments entryPoint) {
return this;
}

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

public DockerFileBuilder user(String user) {
this.user = user;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public class BuildImageConfiguration {
*/
private Arguments cmd;

/** @parameter */
private String user;

/**
* @parameter
*/
Expand Down Expand Up @@ -224,6 +227,10 @@ public List<String> getRunCmds() {
return runCmds;
}

public String getUser() {
return user;
}

public Map<String, String> getArgs() {
return args;
}
Expand Down Expand Up @@ -341,7 +348,12 @@ public Builder entryPoint(String entryPoint) {
}
return this;
}


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

public Builder skip(String skip) {
if (skip != null) {
config.skip = Boolean.valueOf(skip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import static io.fabric8.maven.docker.assembly.DockerFileKeyword.WORKDIR;

/**
* Enum holding possible configuration keys
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ private BuildImageConfiguration extractBuildConfiguration(String prefix, Propert
.tags(listWithPrefix(prefix, TAGS, properties))
.maintainer(withPrefix(prefix, MAINTAINER, properties))
.workdir(withPrefix(prefix, WORKDIR, properties))
.skip(withPrefix(prefix, ConfigKey.SKIP_BUILD, properties))
.dockerFile(withPrefix(prefix, ConfigKey.DOCKER_FILE, properties))
.dockerFileDir(withPrefix(prefix, ConfigKey.DOCKER_FILE_DIR, properties))
.skip(withPrefix(prefix, SKIP_BUILD, properties))
.dockerFile(withPrefix(prefix, DOCKER_FILE, properties))
.dockerFileDir(withPrefix(prefix, DOCKER_FILE_DIR, properties))
.user(withPrefix(prefix, USER, properties))
.build();
}

Expand Down Expand Up @@ -112,7 +113,7 @@ private RunImageConfiguration extractRunConfiguration(String prefix, Properties
.log(extractLogConfig(prefix,properties))
.wait(extractWaitConfig(prefix, properties))
.volumes(extractVolumeConfig(prefix, properties))
.skip(withPrefix(prefix, ConfigKey.SKIP_RUN, properties))
.skip(withPrefix(prefix, SKIP_RUN, properties))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,25 @@ public void illegalNonAbsoluteBaseDir() {
}

@Test
public void testUserWithChown() {
String dockerFile = new DockerFileBuilder().user("jboss:jboss:jboss")
public void testAssemblyUserWithChown() {
String dockerFile = new DockerFileBuilder().assemblyUser("jboss:jboss:jboss")
.add("a","a/nested").add("b","b/deeper/nested").content();
String EXPECTED_REGEXP = "chown\\s+-R\\s+jboss:jboss\\s+([^\\s]+)"
+ "\\s+&&\\s+cp\\s+-rp\\s+\\1/\\*\\s+/\\s+&&\\s+rm\\s+-rf\\s+\\1";
Pattern pattern = Pattern.compile(EXPECTED_REGEXP);
assertTrue(pattern.matcher(dockerFile).find());
}

@Test
public void testUser() {
String dockerFile = new DockerFileBuilder().assemblyUser("jboss:jboss:jboss").user("bob")
.add("a","a/nested").add("b","b/deeper/nested").content();
String EXPECTED_REGEXP = "USER bob$";
Pattern pattern = Pattern.compile(EXPECTED_REGEXP);
assertTrue(pattern.matcher(dockerFile).find());
}


@Test
public void testExportBaseDir() {
assertTrue(new DockerFileBuilder().basedir("/export").content().contains("/export"));
Expand Down

0 comments on commit e87fd3c

Please sign in to comment.