mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
--env-file: simple line-delimited
match dock functionality, and not try to achieve shell-sourcing compatibility Docker-DCO-1.1-Signed-off-by: Vincent Batts <vbatts@redhat.com> (github: vbatts)
This commit is contained in:
parent
d9c257732e
commit
ff4ac7441b
2 changed files with 20 additions and 20 deletions
|
@ -1296,7 +1296,8 @@ 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.
|
||||
first, and then ``-e``/``--env`` flags. This way, the ``-e`` or ``--env`` will
|
||||
override variables as needed.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
|
@ -1306,29 +1307,30 @@ first, and then ``-e``/``--env`` flags. So that they can override VAR as needed.
|
|||
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``
|
||||
to be in the VAR=VAL format, mimicking the argument passed to ``--env``.
|
||||
Comment lines need only be prefixed with ``#``
|
||||
|
||||
An example of a file passed with ``--env-file``
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ cat ./env.list
|
||||
TEST_FOO=BAR
|
||||
|
||||
# this is a comment
|
||||
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
|
||||
|
||||
# pass through this variable from the caller
|
||||
TEST_PASSTHROUGH
|
||||
$ sudo TEST_PASSTHROUGH=howdy docker run --env-file ./env.list busybox env
|
||||
HOME=/
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
HOSTNAME=215d54a814bc
|
||||
HOSTNAME=5198e0745561
|
||||
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
|
||||
TEST_PASSTHROUGH=howdy
|
||||
|
||||
|
||||
.. code-block:: bash
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -23,14 +22,13 @@ func ParseEnvFile(filename string) ([]string, error) {
|
|||
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
|
||||
if len(line) > 0 && !strings.HasPrefix(line, "#") {
|
||||
if strings.Contains(line, "=") {
|
||||
data := strings.SplitN(line, "=", 2)
|
||||
lines = append(lines, fmt.Sprintf("%s=%s", data[0], data[1]))
|
||||
} else {
|
||||
lines = append(lines, fmt.Sprintf("%s=%s", line, os.Getenv(line)))
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("%s=%s", key, val))
|
||||
}
|
||||
}
|
||||
return lines, nil
|
Loading…
Add table
Reference in a new issue