diff --git a/commands/commands.go b/commands/commands.go index 9f85f051d5..a9c2a10f6d 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -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", diff --git a/commands/create.go b/commands/create.go index 0a6890bf0f..e69ac3c539 100644 --- a/commands/create.go +++ b/commands/create.go @@ -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"), diff --git a/docs/index.md b/docs/index.md index cd61b34b42..f4a9405790 100644 --- a/docs/index.md +++ b/docs/index.md @@ -20,7 +20,7 @@ them, then configures the `docker` client to talk to them. A "machine" is the combination of a Docker host and a configured client. Once you create one or more Docker hosts, Docker Machine supplies a number of commands for -managing them. Using these commands you can +managing them. Using these commands you can - start, inspect, stop, and restart a host - upgrade the Docker client and daemon @@ -28,7 +28,7 @@ managing them. Using these commands you can ## Understand Docker Machine basic concepts -Docker Machine allows you to provision Docker on virtual machines that reside either on your local system or on a cloud provider. Docker Machine creates a host on a VM and you use the Docker Engine client as needed to build images and create containers on the host. +Docker Machine allows you to provision Docker on virtual machines that reside either on your local system or on a cloud provider. Docker Machine creates a host on a VM and you use the Docker Engine client as needed to build images and create containers on the host. To create a virtual machine, you supply Docker Machine with the name of the driver you want use. The driver represents the virtual environment. For example, on a local Linux, Mac, or Windows system the driver is typically Oracle Virtual Box. For cloud providers, Docker Machine supports drivers such as AWS, Microsoft Azure, Digital Ocean and many more. The Docker Machine reference includes a complete [list of the supported drivers](/drivers). diff --git a/docs/reference/create.md b/docs/reference/create.md index 95e1824b5b..b6dd61c314 100644 --- a/docs/reference/create.md +++ b/docs/reference/create.md @@ -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, diff --git a/libmachine/engine/engine.go b/libmachine/engine/engine.go index d829e14022..39049106dc 100644 --- a/libmachine/engine/engine.go +++ b/libmachine/engine/engine.go @@ -4,6 +4,7 @@ type EngineOptions struct { ArbitraryFlags []string Dns []string GraphDir string + Env []string Ipv6 bool InsecureRegistry []string Labels []string diff --git a/libmachine/provision/boot2docker.go b/libmachine/provision/boot2docker.go index f7cdf9602f..1486181b7b 100644 --- a/libmachine/provision/boot2docker.go +++ b/libmachine/provision/boot2docker.go @@ -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 { diff --git a/libmachine/provision/debian.go b/libmachine/provision/debian.go index b6070ef027..aa0691b600 100644 --- a/libmachine/provision/debian.go +++ b/libmachine/provision/debian.go @@ -172,6 +172,7 @@ MountFlags=slave LimitNOFILE=1048576 LimitNPROC=1048576 LimitCORE=infinity +Environment={{range .EngineOptions.Env}}{{ printf "%q" . }} {{end}} [Install] WantedBy=multi-user.target diff --git a/libmachine/provision/generic.go b/libmachine/provision/generic.go index a2991e4b80..04ab390029 100644 --- a/libmachine/provision/generic.go +++ b/libmachine/provision/generic.go @@ -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 { diff --git a/libmachine/provision/redhat.go b/libmachine/provision/redhat.go index c583635af1..271e5d5ee1 100644 --- a/libmachine/provision/redhat.go +++ b/libmachine/provision/redhat.go @@ -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 { diff --git a/libmachine/provision/utils.go b/libmachine/provision/utils.go index 8727a6884f..1a51d665b6 100644 --- a/libmachine/provision/utils.go +++ b/libmachine/provision/utils.go @@ -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 } diff --git a/test/integration/core/arbitrary-engine-envs.bats b/test/integration/core/arbitrary-engine-envs.bats new file mode 100644 index 0000000000..4e8fe89a7e --- /dev/null +++ b/test/integration/core/arbitrary-engine-envs.bats @@ -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 ] +}