1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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 <vbatts@redhat.com> (github: vbatts)
This commit is contained in:
Vincent Batts 2014-03-06 17:49:47 -05:00
parent acf5289ddd
commit 33dde1f728
3 changed files with 59 additions and 32 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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)