Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit bc87c5b

Browse files
Merge pull request #2750 from thaJeztah/19.03_backport_rewrite_build_env
[19.03 backport] builder: rephrase ENV section, remove examples for ENV key value without '=' Upstream-commit: 4620b42c3b1f0186a27b1ecebf523a37f1ea65a2 Component: cli
2 parents fcc83c0 + 8b51565 commit bc87c5b

9 files changed

Lines changed: 74 additions & 45 deletions

File tree

components/cli/cli/command/image/import_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ func TestNewImportCommandSuccess(t *testing.T) {
8181
},
8282
{
8383
name: "change",
84+
args: []string{"--change", "ENV DEBUG=true", "-"},
85+
imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) {
86+
assert.Check(t, is.Equal("ENV DEBUG=true", options.Changes[0]))
87+
return ioutil.NopCloser(strings.NewReader("")), nil
88+
},
89+
},
90+
{
91+
name: "change legacy syntax",
8492
args: []string{"--change", "ENV DEBUG true", "-"},
8593
imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) {
8694
assert.Check(t, is.Equal("ENV DEBUG true", options.Changes[0]))

components/cli/docs/reference/builder.md

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,10 @@ Example (parsed representation is displayed after the `#`):
500500

501501
```dockerfile
502502
FROM busybox
503-
ENV foo /bar
504-
WORKDIR ${foo} # WORKDIR /bar
505-
ADD . $foo # ADD . /bar
506-
COPY \$foo /quux # COPY $foo /quux
503+
ENV FOO=/bar
504+
WORKDIR ${FOO} # WORKDIR /bar
505+
ADD . $FOO # ADD . /bar
506+
COPY \$FOO /quux # COPY $FOO /quux
507507
```
508508

509509
Environment variables are supported by the following list of instructions in
@@ -994,53 +994,74 @@ port. For detailed information, see the
994994
## ENV
995995
996996
```dockerfile
997-
ENV <key> <value>
998997
ENV <key>=<value> ...
999998
```
1000999
10011000
The `ENV` instruction sets the environment variable `<key>` to the value
10021001
`<value>`. This value will be in the environment for all subsequent instructions
10031002
in the build stage and can be [replaced inline](#environment-replacement) in
1004-
many as well.
1005-
1006-
The `ENV` instruction has two forms. The first form, `ENV <key> <value>`,
1007-
will set a single variable to a value. The entire string after the first
1008-
space will be treated as the `<value>` - including whitespace characters. The
1009-
value will be interpreted for other environment variables, so quote characters
1010-
will be removed if they are not escaped.
1011-
1012-
The second form, `ENV <key>=<value> ...`, allows for multiple variables to
1013-
be set at one time. Notice that the second form uses the equals sign (=)
1014-
in the syntax, while the first form does not. Like command line parsing,
1003+
many as well. The value will be interpreted for other environment variables, so
1004+
quote characters will be removed if they are not escaped. Like command line parsing,
10151005
quotes and backslashes can be used to include spaces within values.
10161006
1017-
For example:
1007+
Example:
10181008
10191009
```dockerfile
1020-
ENV myName="John Doe" myDog=Rex\ The\ Dog \
1021-
myCat=fluffy
1010+
ENV MY_NAME="John Doe"
1011+
ENV MY_DOG=Rex\ The\ Dog
1012+
ENV MY_CAT=fluffy
10221013
```
10231014
1024-
and
1015+
The `ENV` instruction allows for multiple `<key>=<value> ...` variables to be set
1016+
at one time, and the example below will yield the same net results in the final
1017+
image:
10251018
10261019
```dockerfile
1027-
ENV myName John Doe
1028-
ENV myDog Rex The Dog
1029-
ENV myCat fluffy
1020+
ENV MY_NAME="John Doe" MY_DOG=Rex\ The\ Dog \
1021+
MY_CAT=fluffy
10301022
```
10311023
1032-
will yield the same net results in the final image.
1033-
10341024
The environment variables set using `ENV` will persist when a container is run
10351025
from the resulting image. You can view the values using `docker inspect`, and
10361026
change them using `docker run --env <key>=<value>`.
10371027
1038-
> **Note**
1028+
Environment variable persistence can cause unexpected side effects. For example,
1029+
setting `ENV DEBIAN_FRONTEND=noninteractive` changes the behavior of `apt-get`,
1030+
and may confuse users of your image.
1031+
1032+
If an environment variable is only needed during build, and not in the final
1033+
image, consider setting a value for a single command instead:
1034+
1035+
```dockerfile
1036+
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y ...
1037+
```
1038+
1039+
Or using [`ARG`](#arg), which is not persisted in the final image:
1040+
1041+
```dockerfile
1042+
ARG DEBIAN_FRONTEND=noninteractive
1043+
RUN apt-get update && apt-get install -y ...
1044+
```
1045+
1046+
> **Alternative syntax**
10391047
>
1040-
> Environment persistence can cause unexpected side effects. For example,
1041-
> setting `ENV DEBIAN_FRONTEND noninteractive` may confuse apt-get
1042-
> users on a Debian-based image. To set a value for a single command, use
1043-
> `RUN <key>=<value> <command>`.
1048+
> The `ENV` instruction also allows an alternative syntax `ENV <key> <value>`,
1049+
> omitting the `=`. For example:
1050+
>
1051+
> ```dockerfile
1052+
> ENV MY_VAR my-value
1053+
> ```
1054+
>
1055+
> This syntax does not allow for multiple environment-variables to be set in a
1056+
> single `ENV` instruction, and can be confusing. For example, the following
1057+
> sets a single environment variable (`ONE`) with value `"TWO= THREE=world"`:
1058+
>
1059+
> ```dockerfile
1060+
> ENV ONE TWO= THREE=world
1061+
> ```
1062+
>
1063+
> The alternative syntax is supported for backward compatibility, but discouraged
1064+
> for the reasons outlined above, and may be removed in a future release.
10441065
10451066
## ADD
10461067
@@ -1768,7 +1789,7 @@ The `WORKDIR` instruction can resolve environment variables previously set using
17681789
For example:
17691790

17701791
```dockerfile
1771-
ENV DIRPATH /path
1792+
ENV DIRPATH=/path
17721793
WORKDIR $DIRPATH/$DIRNAME
17731794
RUN pwd
17741795
```
@@ -1873,7 +1894,7 @@ this Dockerfile with an `ENV` and `ARG` instruction.
18731894
```dockerfile
18741895
FROM ubuntu
18751896
ARG CONT_IMG_VER
1876-
ENV CONT_IMG_VER v1.0.0
1897+
ENV CONT_IMG_VER=v1.0.0
18771898
RUN echo $CONT_IMG_VER
18781899
```
18791900

@@ -1894,7 +1915,7 @@ useful interactions between `ARG` and `ENV` instructions:
18941915
```dockerfile
18951916
FROM ubuntu
18961917
ARG CONT_IMG_VER
1897-
ENV CONT_IMG_VER ${CONT_IMG_VER:-v1.0.0}
1918+
ENV CONT_IMG_VER=${CONT_IMG_VER:-v1.0.0}
18981919
RUN echo $CONT_IMG_VER
18991920
```
19001921

@@ -2030,7 +2051,7 @@ Consider another example under the same command line:
20302051
```dockerfile
20312052
FROM ubuntu
20322053
ARG CONT_IMG_VER
2033-
ENV CONT_IMG_VER $CONT_IMG_VER
2054+
ENV CONT_IMG_VER=$CONT_IMG_VER
20342055
RUN echo $CONT_IMG_VER
20352056
```
20362057

@@ -2045,7 +2066,7 @@ this Dockerfile:
20452066
```dockerfile
20462067
FROM ubuntu
20472068
ARG CONT_IMG_VER
2048-
ENV CONT_IMG_VER hello
2069+
ENV CONT_IMG_VER=hello
20492070
RUN echo $CONT_IMG_VER
20502071
```
20512072

components/cli/docs/reference/commandline/build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ FROM busybox
742742
RUN echo hello > /hello
743743
RUN echo world >> /hello
744744
RUN touch remove_me /remove_me
745-
ENV HELLO world
745+
ENV HELLO=world
746746
RUN rm /remove_me
747747
```
748748

components/cli/docs/reference/commandline/commit.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ $ docker inspect -f "{{ .Config.Env }}" c3f279d17e0a
7373

7474
[HOME=/ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]
7575

76-
$ docker commit --change "ENV DEBUG true" c3f279d17e0a svendowideit/testimage:version3
76+
$ docker commit --change "ENV DEBUG=true" c3f279d17e0a svendowideit/testimage:version3
7777

7878
f5283438590d
7979

components/cli/docs/reference/commandline/import.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ $ sudo tar -c . | docker import - exampleimagedir
7272
### Import from a local directory with new configurations
7373

7474
```bash
75-
$ sudo tar -c . | docker import --change "ENV DEBUG true" - exampleimagedir
75+
$ sudo tar -c . | docker import --change "ENV DEBUG=true" - exampleimagedir
7676
```
7777

7878
Note the `sudo` in this example – you must preserve

components/cli/e2e/image/build_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ func TestBuildIidFileSquash(t *testing.T) {
116116
buildDir := fs.NewDir(t, "test-iidfile-squash-build",
117117
fs.WithFile("Dockerfile", fmt.Sprintf(`
118118
FROM %s
119-
ENV FOO FOO
120-
ENV BAR BAR
119+
ENV FOO=FOO
120+
ENV BAR=BAR
121121
RUN touch /fiip
122122
RUN touch /foop`, fixtures.AlpineImage)),
123123
)

components/cli/man/Dockerfile.5.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ A Dockerfile is similar to a Makefile.
201201
from the resulting image. Use `docker inspect` to inspect these values, and
202202
change them using `docker run --env <key>=<value>`.
203203

204-
Note that setting "`ENV DEBIAN_FRONTEND noninteractive`" may cause
204+
Note that setting "`ENV DEBIAN_FRONTEND=noninteractive`" may cause
205205
unintended consequences, because it will persist when the container is run
206206
interactively, as with the following command: `docker run -t -i image bash`
207207

@@ -388,7 +388,7 @@ A Dockerfile is similar to a Makefile.
388388
```
389389
1 FROM ubuntu
390390
2 ARG CONT_IMG_VER
391-
3 ENV CONT_IMG_VER v1.0.0
391+
3 ENV CONT_IMG_VER=v1.0.0
392392
4 RUN echo $CONT_IMG_VER
393393
```
394394
Then, assume this image is built with this command:
@@ -408,7 +408,7 @@ A Dockerfile is similar to a Makefile.
408408
```
409409
1 FROM ubuntu
410410
2 ARG CONT_IMG_VER
411-
3 ENV CONT_IMG_VER ${CONT_IMG_VER:-v1.0.0}
411+
3 ENV CONT_IMG_VER=${CONT_IMG_VER:-v1.0.0}
412412
4 RUN echo $CONT_IMG_VER
413413
```
414414

components/cli/man/src/container/commit.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ variable set to "true", you can create a new image based on that
2727
container by first getting the container's ID with `docker ps` and
2828
then running:
2929

30-
$ docker container commit -c="ENV DEBUG true" 98bd7fc99854 debug-image
30+
$ docker container commit -c="ENV DEBUG=true" 98bd7fc99854 debug-image

components/cli/man/src/image/import.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Import to docker via pipe and stdin:
3636
## Apply specified Dockerfile instructions while importing the image
3737
This example sets the docker image ENV variable DEBUG to true by default.
3838

39-
# tar -c . | docker image import -c="ENV DEBUG true" - exampleimagedir
39+
# tar -c . | docker image import -c="ENV DEBUG=true" - exampleimagedir
4040

4141
## When the daemon supports multiple operating systems
4242
If the daemon supports multiple operating systems, and the image being imported

0 commit comments

Comments
 (0)