Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
ENV lets you set environment variables that you can use later in your Dockerfile, and they'll also be set within the environment of your running container.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Here's a new Dockerfile that
uses the ENV instruction.
0:00
ENV lets you set environment variables
that you can use later in your Dockerfile,
0:04
and they'll be set within the environment
of your running container.
0:09
They take the form key = value.
0:13
So appDir=/app sets an environment
variable named appDIR to a value of /app.
0:16
You can have multiple ENV lines, or
0:24
you can set several variables on
the same ENV line, separated by spaces.
0:26
The latter is generally recommended,
because they can be stored and
0:32
retrieved more efficiently.
0:35
If you need a value to contain spaces,
you can surround it in quotes.
0:37
Message="Welcome to our
app!" sets the message
0:40
variable to Welcome to our app!, with
no quotes appearing in the final value.
0:45
Once you've set an environment variable,
you can then access it later in
0:51
the Dockerfile, by typing a dollar sign,
followed by the variable's name.
0:54
So this WORKDIR $appDir line will
set the working directory to /app.
0:59
And RUN echo $message >
README.txt will write the text
1:05
Welcome to our app!, out to README.txt.
1:10
If you need to use a variable name in an
instruction where it won't be surrounded
1:14
by spaces, and are concerned that part of
the command might get treated as part of
1:18
the variable name, you can surround
the variable name with curly braces.
1:21
So this command line will run echo
when the container starts, and
1:26
print read/app/README.text for
a friendly greet.
1:30
We can confirm all this by
building our container with
1:35
docker build -t temp in
the current directory,
1:40
And then running it with docker run temp,
and
1:47
we'll can cat out the contents
of /app/README.txt.
1:51
The working directory
successfully got set to /app, and
2:02
our message was written to
README.txt within that directory.
2:06
Environment variables that we
set with the ENV instruction
2:11
are available on
the running container too.
2:14
Let's run the container with docker run.
2:17
Use -it to set up an indirect
of terminal session.
2:22
Run the temp image, and
run the /bin/sh command.
2:26
That will run the container and
launch a shell.
2:33
If we run the set command,
we'll see the settings for
2:36
all our environment variables.
2:38
And there near the bottom, we'll see
our appDir and message variables.
2:40
Accessing variables within a Docker file
gives us the same syntax as a shell.
2:46
So we can use the syntax we're
used to here on our terminal.
2:51
Echo $appDir will print the value
of the appDir variable.
2:54
And curly braces work too.
3:01
Echo ${message} will
3:03
print the value of message.
3:07
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up