The ENV instruction

As you may guess, the ENV instruction is used to define environment variables that will be set in the running containers created from the image being built. The variables are defined using typical key-value pairs. A Dockerfile can have one or more ENV instructions. Here is the ENV instruction syntax:

# ENV instruction syntax
# This is the form to create a single environment variable per instruction
# Everything after the space following the <key> becomes the value
ENV <key> <value>
# This is the form to use when you want to create more than one variable per instruction
ENV <key>=<value> ...

Each ENV instruction will create one or more environment variables (unless the key name is repeated). Let's take a look at some ENV instructions in a Dockerfile:

# ENV instruction Dockerfile for Docker Quick Start
FROM alpine
LABEL maintainer="Earl Waud <[email protected]>"
ENV appDescription This app is a sample of using ENV instructions
ENV appName=env-demo
ENV note1="The First Note First" note2=The Second Note Second
note3="The Third Note Third"
ENV changeMe="Old Value"
CMD ["sh"]

After building the image using this Dockerfile, you can inspect the image metadata and see the environment variables that have been created:

Environment variables can be set (or overridden) when a container is run using the --env parameter. Here, we see this feature in action:

It is important to know that using ENV instructions create a zero-byte-sized additional layer in the resulting image. If you are adding more than one environment variable to your image and can use the form of the instruction that supports setting multiple variables with one instruction, doing so will only create a single additional image layer, so that is the way to go.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset