From 33dde1f7288781a3a36951309f963e6040a1a0f5 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 6 Mar 2014 17:49:47 -0500 Subject: [PATCH] env-file: update functionality and docs Multiple flags allowed. Order prescribed. Examples provided. Multiline accounted for. Docker-DCO-1.1-Signed-off-by: Vincent Batts (github: vbatts) --- docs/sources/reference/commandline/cli.rst | 47 +++++++++++++++++++--- pkg/opts/envfile.go | 41 ++++++++----------- runconfig/parse.go | 3 +- 3 files changed, 59 insertions(+), 32 deletions(-) diff --git a/docs/sources/reference/commandline/cli.rst b/docs/sources/reference/commandline/cli.rst index 4e697766bc..989ea38798 100644 --- a/docs/sources/reference/commandline/cli.rst +++ b/docs/sources/reference/commandline/cli.rst @@ -1289,12 +1289,47 @@ explains in detail how to manipulate ports in Docker. $ sudo docker run -e MYVAR1 --env MYVAR2=foo --env-file ./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 --env-file flag takes a filename as an argument and expects each -line to be a VAR=VAL format. +This sets environmental variables in the container. For illustration all three +flags are shown here. Where ``-e``, ``--env`` 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). All +three flags, ``-e``, ``--env`` and ``--env-file`` can be repeated. + +Regardless of the order of these three flags, the ``--env-file`` are processed +first, and then ``-e``/``--env`` flags. So that they can override VAR as needed. + +.. code-block:: bash + + $ cat ./env.list + TEST_FOO=BAR + $ sudo docker run --env TEST_FOO="This is a test" --env-file ./env.list busybox env | grep TEST_FOO + TEST_FOO=This is a test + +The ``--env-file`` flag takes a filename as an argument and expects each line +to be in the VAR=VAL format. The VAL is Unquoted, so if you need a multi-line +value, then use `\n` escape characters inside of a double quoted VAL. Single +quotes are literal. An example of a file passed with ``--env-file`` + +.. code-block:: bash + + $ cat ./env.list + TEST_FOO=BAR + TEST_APP_DEST_HOST=10.10.0.127 + TEST_APP_DEST_PORT=8888 + TEST_SOME_MULTILINE_VAR="this is first line\nthis is second line" + TEST_SOME_LITERAL_VAR='this\nwill\nall\nbe\none\nline' + $ sudo docker run --env-file ./env.list busybox env + HOME=/ + PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + HOSTNAME=215d54a814bc + TEST_FOO=BAR + TEST_APP_DEST_HOST=10.10.0.127 + TEST_APP_DEST_PORT=8888 + TEST_SOME_MULTILINE_VAR=this is first line + this is second line + TEST_SOME_LITERAL_VAR='this\nwill\nall\nbe\none\nline' + container=lxc + .. code-block:: bash diff --git a/pkg/opts/envfile.go b/pkg/opts/envfile.go index 7c69f5d799..d9afdd952c 100644 --- a/pkg/opts/envfile.go +++ b/pkg/opts/envfile.go @@ -2,9 +2,10 @@ package opts import ( "bufio" - "bytes" - "io" + "fmt" "os" + "strconv" + "strings" ) /* @@ -17,30 +18,20 @@ func ParseEnvFile(filename string) ([]string, error) { } defer fh.Close() - 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{} + lines := []string{} + scanner := bufio.NewScanner(fh) + for scanner.Scan() { + line := scanner.Text() + // line is not empty, and not starting with '#' + if len(line) > 0 && !strings.HasPrefix(line, "#") && strings.Contains(line, "=") { + data := strings.SplitN(line, "=", 2) + key := data[0] + val := data[1] + if str, err := strconv.Unquote(data[1]); err == nil { + val = str + } + lines = append(lines, fmt.Sprintf("%s=%s", key, val)) } - - 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 } diff --git a/runconfig/parse.go b/runconfig/parse.go index e6e8b120d8..d3e32bc2de 100644 --- a/runconfig/parse.go +++ b/runconfig/parse.go @@ -203,7 +203,6 @@ 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()...) for _, ef := range flEnvFile.GetAll() { parsedVars, err := opts.ParseEnvFile(ef) if err != nil { @@ -211,6 +210,8 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf } envVariables = append(envVariables, parsedVars...) } + // parse the '-e' and '--env' after, to allow override + envVariables = append(envVariables, flEnv.GetAll()...) // boo, there's no debug output for docker run //utils.Debugf("Environment variables for the container: %#v", envVariables)