added support for setting environment variables in docker engines

Signed-off-by: Ryan Grothouse <rgrothouse@gmail.com>
This commit is contained in:
Ryan Grothouse 2015-07-09 16:40:18 -04:00
parent 2c36635dd4
commit d553a2c757
11 changed files with 48 additions and 3 deletions

View File

@ -196,6 +196,11 @@ var sharedCreateFlags = []cli.Flag{
Name: "engine-storage-driver",
Usage: "Specify a storage driver to use with the engine",
},
cli.StringSliceFlag{
Name: "engine-env",
Usage: "Specify environment variables to set in the engine",
Value: &cli.StringSlice{},
},
cli.BoolFlag{
Name: "swarm",
Usage: "Configure Machine with Swarm",

View File

@ -70,6 +70,7 @@ func cmdCreate(c *cli.Context) {
},
EngineOptions: &engine.EngineOptions{
ArbitraryFlags: c.StringSlice("engine-opt"),
Env: c.StringSlice("engine-env"),
InsecureRegistry: c.StringSlice("engine-insecure-registry"),
Labels: c.StringSlice("engine-label"),
RegistryMirror: c.StringSlice("engine-registry-mirror"),

View File

@ -60,6 +60,7 @@ Options:
--engine-registry-mirror [--engine-registry-mirror option --engine-registry-mirror option] Specify registry mirrors to use
--engine-label [--engine-label option --engine-label option] Specify labels for the created engine
--engine-storage-driver "aufs" Specify a storage driver to use with the engine
--engine-env Specify environment variables to set in the engine
--swarm Configure Machine with Swarm
--swarm-master Configure Machine to be a Swarm master
--swarm-discovery Discovery service to use with Swarm
@ -146,6 +147,17 @@ $ docker-machine create -d virtualbox \
gdns
```
Additionally, Docker Machine supports a flag, `--engine-env`, which can be used to
specify arbitrary environment variables to be set within the engine with the syntax `--engine-env name=value`. For example, to specify that the engine should use `example.com` as the proxy server, you could run the following create command:
```
$ docker-machine create -d virtualbox \
--engine-env HTTP_PROXY=http://example.com:8080 \
--engine-env HTTPS_PROXY=https://example.com:8080 \
--engine-env NO_PROXY=example2.com \
proxbox
```
## Specifying Docker Swarm options for the created machine
In addition to being able to configure Docker Engine options as listed above,

View File

@ -4,6 +4,7 @@ type EngineOptions struct {
ArbitraryFlags []string
Dns []string
GraphDir string
Env []string
Ipv6 bool
InsecureRegistry []string
Labels []string

View File

@ -140,6 +140,9 @@ DOCKER_STORAGE={{.EngineOptions.StorageDriver}}
DOCKER_TLS=auto
SERVERKEY={{.AuthOptions.ServerKeyRemotePath}}
SERVERCERT={{.AuthOptions.ServerCertRemotePath}}
{{range .EngineOptions.Env}}export \"{{ printf "%q" . }}\"
{{end}}
`
t, err := template.New("engineConfig").Parse(engineConfigTmpl)
if err != nil {

View File

@ -172,6 +172,7 @@ MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
Environment={{range .EngineOptions.Env}}{{ printf "%q" . }} {{end}}
[Install]
WantedBy=multi-user.target

View File

@ -91,6 +91,8 @@ DOCKER_OPTS='
{{ end }}{{ range .EngineOptions.ArbitraryFlags }}--{{.}}
{{ end }}
'
{{range .EngineOptions.Env}}export \"{{ printf "%q" . }}\"
{{end}}
`
t, err := template.New("engineConfig").Parse(engineConfigTmpl)
if err != nil {

View File

@ -246,6 +246,7 @@ MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
Environment={{range .EngineOptions.Env}}{{ printf "%q" . }} {{end}}
`
t, err := template.New("engineConfig").Parse(engineConfigTmpl)
if err != nil {

View File

@ -164,7 +164,7 @@ func ConfigureAuth(p Provisioner) error {
return err
}
if _, err = p.SSHCommand(fmt.Sprintf("printf \"%s\" | sudo tee %s", dkrcfg.EngineOptions, dkrcfg.EngineOptionsPath)); err != nil {
if _, err = p.SSHCommand(fmt.Sprintf("printf %%s \"%s\" | sudo tee %s", dkrcfg.EngineOptions, dkrcfg.EngineOptionsPath)); err != nil {
return err
}

View File

@ -0,0 +1,19 @@
#!/usr/bin/env bats
load ${BASE_TEST_DIR}/helpers.bash
@test "$DRIVER: create with arbitrary engine envs" {
run machine create -d $DRIVER \
--engine-env=TEST=VALUE \
$NAME
[ $status -eq 0 ]
}
@test "$DRIVER: test docker process envs" {
# get pid of docker process, check process envs for set Environment Variable from above test
run machine ssh $NAME 'pgrep -f "docker -d" | xargs -I % sudo cat /proc/%/environ | grep -q "TEST=VALUE"'
[ $status -eq 0 ]
}