Skip to content

Commit

Permalink
Add env vars section
Browse files Browse the repository at this point in the history
  • Loading branch information
maricaantonacci committed Feb 6, 2022
1 parent 278529d commit b626eac
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions docs/container/env_vars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
An environment variable consists of a variable name and its value.

There are two ways to set environment variables for a docker container: with CLI arguments, using an _env_ file.

### CLI arguments

When we launch our Docker container, **we can pass environment variables as key-value pairs directly into the command line** using the parameter _–env_ (or its short form _-e_).

For instance, let's execute the following command:

```
$ docker run --rm --env VARIABLE1=foobar alpine env
```

The environment variables we set will be printed to the console:

```
VARIABLE1=foobar
```

As can be seen, the Docker container correctly interprets the variable `VARIABLE1`.

Also, **we can omit the value in the command line if the variable already exists in the local environment**.

For example, let's define a local environment variable:

```
export VARIABLE2=foobar2
```

Then, let's specify the environment variable without its value:

```
docker run --rm --env VARIABLE2 alpine env
```

And we can see Docker still picked up the value, this time from the surrounding environment:

```
VARIABLE2=foobar2
```

### Using --env-file

The above solution is adequate when the number of variables is low. However, as soon as we have more than a handful of variables, it can quickly become cumbersome and error-prone.

**An alternative solution is to use a text file to store our variables**, using the standard _key=value_ format.

Let's define a few variables in a file we'll call _my-env.txt_:


```
$ echo VARIABLE1=foobar1 > my-env.txt
$ echo VARIABLE2=foobar2 >> my-env.txt
$ echo VARIABLE3=foobar3 >> my-env.txt
```

Now, let's inject this file into our Docker container:

```
$ docker run --env-file my-env.txt alpine:3 env
```

Finally, let's take a look at the output:

```
VARIABLE1=foobar1
VARIABLE2=foobar2
VARIABLE3=foobar3
```
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ nav:
- Docker volumes and bind-mounts: 'container/volumes.md'
- Docker volume plugins - an example: 'container/volume_plugin.md'
- Summary: 'container/volume_summary.md'
- Environment variables:
- How to pass env variables to a container: 'container/env_vars.md'
- Docker web UI:
- Portainer: 'gui/portainer.md'

Expand Down

0 comments on commit b626eac

Please sign in to comment.