Skip to content

Commit

Permalink
Add workdir to <build> configuration
Browse files Browse the repository at this point in the history
This was provided by pull request #204
  • Loading branch information
rhuss committed Jun 29, 2015
1 parent 6247285 commit 418023d
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 11 deletions.
5 changes: 3 additions & 2 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

* **0.12.1**
- Add `docker:watch` (#187)
- Allow `extraHosts` ips to be resolved at runtime (#196)

- Allow `extraHosts` IPs to be resolved at runtime (#196)
- Add `workDir` as configuration option to `<build>` (#204)

* **0.12.0**
- Allow CMD and ENTRYPOINT with shell and exec arguments (#130, #149)
- Unix Socket support (#179)
Expand Down
2 changes: 2 additions & 0 deletions doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ of an image configuration. The available subelements are
[Start-up Arguments](#start-up-arguments) for details.
* **entrypoint** An entrypoint allows you to configure a container that will run as an executable.
See [Start-up Arguments](#start-up-arguments) for details.
* **workdir** the directory to change to when starting the container.
* **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 @@ -1118,6 +1119,7 @@ values in the `<build>` and `<run>` sections.
* **docker.dns.idx** List of dns servers to use
* **docker.dnsSearch.idx** List of dns search domains
* **docker.entrypoint** Container entry point
* **docker.workdir** Container working directory
* **docker.env.VARIABLE** Sets an environment
variable. E.g. `<docker.env.JAVA_OPTS>-Xmx512m</docker.env.JAVA_OPTS>`
sets the environment variable `JAVA_OPTS`. Multiple such entries can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ DockerFileBuilder createDockerFileBuilder(BuildImageConfiguration buildConfig, A
if (buildConfig.getMaintainer() != null) {
builder.maintainer(buildConfig.getMaintainer());
}
if (buildConfig.getWorkdir() != null) {
builder.workdir(buildConfig.getWorkdir());
}
if (assemblyConfig != null) {
builder.add("maven", "")
.basedir(assemblyConfig.getBasedir())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class DockerFileBuilder {
// Maintainer of this image
private String maintainer = "docker-maven-plugin@jolokia.org";

// Workdir
private String workdir = null;

// Basedir to be export
private String basedir = "/maven";

Expand All @@ -43,6 +46,8 @@ public class DockerFileBuilder {

// list of ports to expose and environments to use
private List<Integer> ports = new ArrayList<>();

// environment
private Map<String,String> envEntries = new HashMap<>();

// exposed volumes
Expand Down Expand Up @@ -73,17 +78,23 @@ 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");

addEnv(b);
addPorts(b);
addVolumes(b);
addEntries(b);
addWorkdir(b);
addCmd(b);
addEntryPoint(b);

return b.toString();
}

private void addWorkdir(StringBuilder b) {
if (workdir != null) {
b.append("WORKDIR ").append(workdir).append("\n");
}
}

private void addEntryPoint(StringBuilder b){
if (entryPoint != null) {
buildArguments(b, "ENTRYPOINT", entryPoint);
Expand Down Expand Up @@ -181,6 +192,11 @@ public DockerFileBuilder maintainer(String maintainer) {
return this;
}

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

public DockerFileBuilder basedir(String dir) {
if (dir != null) {
basedir = dir;
Expand Down Expand Up @@ -214,7 +230,11 @@ public DockerFileBuilder expose(List<String> ports) {
if (ports != null) {
for (String port : ports) {
if (port != null) {
this.ports.add(Integer.parseInt(port));
try {
this.ports.add(Integer.parseInt(port));
} catch (NumberFormatException exp) {
throw new IllegalArgumentException("Non numeric port " + port + " specified in port mapping",exp);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public class BuildImageConfiguration {
*/
private String command;

/**
* @parameter
*/
private String workdir;

/**
* @parameter
*/
Expand All @@ -81,6 +86,10 @@ public String getMaintainer() {
return maintainer;
}

public String getWorkdir() {
return workdir;
}

public AssemblyConfiguration getAssemblyConfiguration() {
return assembly;
}
Expand Down Expand Up @@ -132,6 +141,11 @@ public Builder maintainer(String maintainer) {
return this;
}

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

public Builder assembly(AssemblyConfiguration assembly) {
config.assembly = assembly;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public enum ConfigKey {
VOLUMES,
TAGS,
MAINTAINER,
WORKDIR,
VOLUMES_FROM,
WAIT_LOG("wait.log"),
WAIT_TIME("wait.time"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private BuildImageConfiguration extractBuildConfiguration(String prefix, Propert
.volumes(listWithPrefix(prefix, VOLUMES, properties))
.tags(listWithPrefix(prefix, TAGS, properties))
.maintainer(withPrefix(prefix, MAINTAINER, properties))
.workdir(withPrefix(prefix, WORKDIR, properties))
.build();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jolokia/docker/maven/util/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static String extractUrl(String dockerHost) {
if (unixSocket.exists() && unixSocket.canRead() && unixSocket.canWrite()) {
connect = "unix:///var/run/docker.sock";
} else {
throw new IllegalArgumentException("No url given and no DOCKER_HOST environment variable set");
throw new IllegalArgumentException("No url given, no DOCKER_HOST environment variable and no read/writable '/var/run/docker.sock'");
}
}
String protocol = connect.contains(":" + AbstractDockerMojo.DOCKER_HTTPS_PORT) ? "https:" : "http:";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.jolokia.docker.maven.assembly;

import java.io.IOException;
import java.util.*;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.IOUtils;
import org.jolokia.docker.maven.config.Arguments;
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.*;

Expand All @@ -27,6 +24,7 @@ public void testBuildDockerFile() throws Exception {
.basedir("/export")
.expose(Collections.singletonList("8080"))
.maintainer("maintainer@example.com")
.workdir("/tmp")
.volumes(Collections.singletonList("/vol1")).content();

String expected = loadFile("docker/Dockerfile.test");
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/docker/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ ENV foo bar
EXPOSE 8080
VOLUME ["/vol1"]
COPY /src /export/dest
WORKDIR /tmp
CMD ["c1","c2"]

0 comments on commit 418023d

Please sign in to comment.