mirror of https://github.com/docker/docs.git
--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
|
@ -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.
|
three flags, ``-e``, ``--env`` and ``--env-file`` can be repeated.
|
||||||
|
|
||||||
Regardless of the order of these three flags, the ``--env-file`` are processed
|
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
|
.. 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
|
TEST_FOO=This is a test
|
||||||
|
|
||||||
The ``--env-file`` flag takes a filename as an argument and expects each line
|
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
|
to be in the VAR=VAL format, mimicking the argument passed to ``--env``.
|
||||||
value, then use `\n` escape characters inside of a double quoted VAL. Single
|
Comment lines need only be prefixed with ``#``
|
||||||
quotes are literal. An example of a file passed with ``--env-file``
|
|
||||||
|
An example of a file passed with ``--env-file``
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
$ cat ./env.list
|
$ cat ./env.list
|
||||||
TEST_FOO=BAR
|
TEST_FOO=BAR
|
||||||
|
|
||||||
|
# this is a comment
|
||||||
TEST_APP_DEST_HOST=10.10.0.127
|
TEST_APP_DEST_HOST=10.10.0.127
|
||||||
TEST_APP_DEST_PORT=8888
|
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'
|
# pass through this variable from the caller
|
||||||
$ sudo docker run --env-file ./env.list busybox env
|
TEST_PASSTHROUGH
|
||||||
|
$ sudo TEST_PASSTHROUGH=howdy docker run --env-file ./env.list busybox env
|
||||||
HOME=/
|
HOME=/
|
||||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||||
HOSTNAME=215d54a814bc
|
HOSTNAME=5198e0745561
|
||||||
TEST_FOO=BAR
|
TEST_FOO=BAR
|
||||||
TEST_APP_DEST_HOST=10.10.0.127
|
TEST_APP_DEST_HOST=10.10.0.127
|
||||||
TEST_APP_DEST_PORT=8888
|
TEST_APP_DEST_PORT=8888
|
||||||
TEST_SOME_MULTILINE_VAR=this is first line
|
TEST_PASSTHROUGH=howdy
|
||||||
this is second line
|
|
||||||
TEST_SOME_LITERAL_VAR='this\nwill\nall\nbe\none\nline'
|
|
||||||
container=lxc
|
|
||||||
|
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,14 +22,13 @@ func ParseEnvFile(filename string) ([]string, error) {
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
// line is not empty, and not starting with '#'
|
// line is not empty, and not starting with '#'
|
||||||
if len(line) > 0 && !strings.HasPrefix(line, "#") && strings.Contains(line, "=") {
|
if len(line) > 0 && !strings.HasPrefix(line, "#") {
|
||||||
data := strings.SplitN(line, "=", 2)
|
if strings.Contains(line, "=") {
|
||||||
key := data[0]
|
data := strings.SplitN(line, "=", 2)
|
||||||
val := data[1]
|
lines = append(lines, fmt.Sprintf("%s=%s", data[0], data[1]))
|
||||||
if str, err := strconv.Unquote(data[1]); err == nil {
|
} else {
|
||||||
val = str
|
lines = append(lines, fmt.Sprintf("%s=%s", line, os.Getenv(line)))
|
||||||
}
|
}
|
||||||
lines = append(lines, fmt.Sprintf("%s=%s", key, val))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return lines, nil
|
return lines, nil
|
Loading…
Reference in New Issue