support for `docker run` environment variables file

Docker-DCO-1.1-Signed-off-by: Vincent Batts <vbatts@redhat.com> (github: vbatts)
This commit is contained in:
Vincent Batts 2014-02-16 19:24:22 -05:00 коммит произвёл Vincent Batts
Родитель 1805ef1ccc
Коммит cd51ac92bd
3 изменённых файлов: 69 добавлений и 1 удалений

Просмотреть файл

@ -1152,6 +1152,7 @@ image is removed.
--cidfile="": Write the container ID to the file
-d, --detach=false: Detached mode: Run container in the background, print new container id
-e, --env=[]: Set environment variables
--envfile="": Read in a line delimited file of ENV variables
-h, --hostname="": Container host name
-i, --interactive=false: Keep stdin open even if not attached
--privileged=false: Give extended privileges to this container
@ -1284,6 +1285,17 @@ This exposes port ``80`` of the container for use within a link without
publishing the port to the host system's interfaces. :ref:`port_redirection`
explains in detail how to manipulate ports in Docker.
.. code-block:: bash
$ sudo docker run -e MYVAR1 --env MYVAR2=foo --envfile ./env.list ubuntu bash
This sets environmental variables to the container. For illustration all three
flags are shown here. Where -e and --env can be repeated, take an environment
variable and value, or if no "=" is provided, then that variable's current
value is passed through (i.e. $MYVAR1 from the host is set to $MYVAR1 in the
container). The --envfile flag takes a filename as an argument and expects each
line to be a VAR=VAL format.
.. code-block:: bash
$ sudo docker run --name console -t -i ubuntu bash

44
pkg/opts/envfile.go Normal file
Просмотреть файл

@ -0,0 +1,44 @@
package opts
import (
"bufio"
"bytes"
"io"
"os"
)
/*
Read in a line delimited file with environment variables enumerated
*/
func ParseEnvFile(filename string) ([]string, error) {
fh, err := os.Open(filename)
if err != nil {
return []string{}, err
}
var (
lines []string = []string{}
line, chunk []byte
)
reader := bufio.NewReader(fh)
line, isPrefix, err := reader.ReadLine()
for err == nil {
if isPrefix {
chunk = append(chunk, line...)
} else if !isPrefix && len(chunk) > 0 {
line = chunk
chunk = []byte{}
} else {
chunk = []byte{}
}
if !isPrefix && len(line) > 0 && bytes.Contains(line, []byte("=")) {
lines = append(lines, string(line))
}
line, isPrefix, err = reader.ReadLine()
}
if err != nil && err != io.EOF {
return []string{}, err
}
return lines, nil
}

Просмотреть файл

@ -68,6 +68,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
flWorkingDir = cmd.String([]string{"w", "-workdir"}, "", "Working directory inside the container")
flCpuShares = cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
flLabelOptions = cmd.String([]string{"Z", "-label"}, "", "Options to pass to underlying labeling system")
flEnvFile = cmd.String([]string{"#envfile", "-envfile"}, "", "Read in a line delimited file of ENV variables")
// For documentation purpose
_ = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxify all received signal to the process (even in non-tty mode)")
@ -199,6 +200,17 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
}
}
// collect all the environment variables for the container
envVariables := []string{}
envVariables = append(envVariables, flEnv.GetAll()...)
parsedVars, err := opts.ParseEnvFile(*flEnvFile)
if err != nil {
return nil, nil, cmd, err
}
envVariables = append(envVariables, parsedVars...)
// boo, there's no debug output for docker run
//utils.Debugf("Environment variables for the container: %#v", envVariables)
config := &Config{
Hostname: hostname,
Domainname: domainname,
@ -213,7 +225,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
AttachStdin: flAttach.Get("stdin"),
AttachStdout: flAttach.Get("stdout"),
AttachStderr: flAttach.Get("stderr"),
Env: flEnv.GetAll(),
Env: envVariables,
Cmd: runCmd,
Dns: flDns.GetAll(),
DnsSearch: flDnsSearch.GetAll(),