Skip to content

Commit

Permalink
Support environment variables (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
mglazer authored and markelliot committed May 18, 2016
1 parent 1932d99 commit 42ba9a4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ command to execute for the `dockerRun` tasks:
image 'busybox'
volumes 'hostvolume': '/containervolume'
daemonize true
env 'MYVAR1': 'MYVALUE1', 'MYVAR2': 'MYVALUE2'
command 'sleep', '100'
}

Expand All @@ -191,6 +192,8 @@ command to execute for the `dockerRun` tasks:
- `volumes` optional map of volumes to mount in the container. The key is path
to the host volume, relative to the project folder, the value is the exposed
container volume path.
- `env` optional map of environment variables to supply to the running container.
These must be exposed in the Dockerfile with `ENV` instructions.
- `daemonize` defaults to true to daemonize the container after starting. However
if your container runs a command and exits, you can set this to false.
- `clean` will add `--rm` to the `docker run` command to ensure that containers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableSet

import static com.google.common.base.Preconditions.checkNotNull

class DockerRunExtension {

private String name
private String image
private List<String> command = ImmutableList.of()
private Set<String> ports = ImmutableSet.of()
private Map<String,String> env = ImmutableMap.of()
private Map<String,String> volumes = ImmutableMap.of()
private boolean daemonize = true
private boolean clean = false
Expand Down Expand Up @@ -79,6 +81,18 @@ class DockerRunExtension {
this.command = ImmutableList.copyOf(command)
}

private void setEnvSingle(String key, String value) {
this.env.put(checkNotNull(key, "key"), checkNotNull(value, "value"))
}

public void env(Map<String,String> env) {
this.env = ImmutableMap.copyOf(env)
}

public Map<String, String> getEnv() {
return env
}

public void ports(String... ports) {
ImmutableSet.Builder builder = ImmutableSet.builder()
for (String port : ports) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
*/
package com.palantir.gradle.docker

import com.google.common.collect.Lists
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.Exec
import org.gradle.logging.StyledTextOutput
import org.gradle.logging.StyledTextOutputFactory
import static org.gradle.logging.StyledTextOutput.Style

import java.io.File
import java.util.Map.Entry

import com.google.common.collect.Lists

import static org.gradle.logging.StyledTextOutput.Style

class DockerRunPlugin implements Plugin<Project> {
@Override
Expand Down Expand Up @@ -104,6 +102,7 @@ class DockerRunPlugin implements Plugin<Project> {
args.add('-v')
args.add("${localFile.absolutePath}:${volume.value}")
}
args.addAll(ext.env.collect{ k, v -> ['-e', "${k}=${v}"] }.flatten())
args.addAll(['--name', ext.name, ext.image])
if (!ext.command.isEmpty()) {
args.addAll(ext.command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,49 @@ class DockerRunPluginTests extends AbstractPluginTest {
buildResult.output =~ /(?m):dockerRunStatus\nDocker container 'foo' is STOPPED./
}

def 'can run with environment variables'() {
given:
temporaryFolder.newFile('Dockerfile') << '''
FROM alpine:3.2
RUN mkdir /test
VOLUME /test
ENV MYVAR1 QUUW
ENV MYVAR2 QUUX
ENV MYVAR3 QUUY
ENV MYVAR4 QUUZ
CMD echo "\$MYVAR1 = \$MYVAR2 = \$MYVAR3 = \$MYVAR4"
'''.stripIndent()
buildFile << '''
plugins {
id 'com.palantir.docker'
id 'com.palantir.docker-run'
}
docker {
name 'foo-image:latest'
}
dockerRun {
name 'foo-envvars'
image 'foo-image:latest'
env 'MYVAR1': 'FOO', 'MYVAR2': 'BAR', 'MYVAR4': 'ZIP'
daemonize false
}
'''.stripIndent()

when:
BuildResult buildResult = with('docker', 'dockerRemoveContainer', 'dockerRun', 'dockerRunStatus').build()

then:
buildResult.task(':dockerRemoveContainer').outcome == TaskOutcome.SUCCESS

buildResult.task(':dockerRun').outcome == TaskOutcome.SUCCESS
buildResult.output =~ /(?m)FOO = BAR = QUUY = ZIP/
buildResult.task(':dockerRunStatus').outcome == TaskOutcome.SUCCESS
buildResult.output =~ /(?m):dockerRunStatus\nDocker container 'foo-envvars' is STOPPED./
}


def isLinux() {
return System.getProperty("os.name") =~ /(?i).*linux.*/
Expand Down

0 comments on commit 42ba9a4

Please sign in to comment.