# Is it possible to pass custom environment variables to the docker container?

**URL:** https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795
**Category:** Self-hosting
**Created:** [September 2, 2020, 12:20pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795 "2020-09-02T12:20:03Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 2, 2020, 12:20pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/1 "2020-09-02T12:20:03Z")

</div>

Some client libraries like [GitHub - googleapis/google-auth-library-ruby: Google Auth Library for Ruby · GitHub](https://github.com/googleapis/google-auth-library-ruby#example-environment-variables) require the authentication credentials to be set as `environment variables`. Is it possible to pass them via the `yml` file.

I tried

1. Adding `export VAR=VALUE` to the custom commands section of the yml file.
2. Adding `VARIABLE: VALUE` to the `env` section of the yml.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 2, 2020, 1:32pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/2 "2020-09-02T13:32:58Z")

</div>

@merefield pointed out to me that its indeed possible.

in the custom commands section do

```plaintext
# adds the variable to .bashrc file
exec: echo 'export VAR=value' >> ~/.bashrc
...
# reloads the file 
exec: source ~/.bashrc

```

~~I can confirm that this approach indeed works. :slight_smile:~~

p.s.  
Somehow the `source ~/.bashrc` doesn’t update the environment. Maybe its being called too early.

Also tried,  
` - exec: /bin/bash -c "source ~/.bashrc"`

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [September 2, 2020, 4:28pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/3 "2020-09-02T16:28:03Z")

</div>

Can’t you use the `env:` section of the container definition for this? That should create an environment variable the Rails app can access.

So like

```plaintext
env:
  CUSTOM_ENV: "value"

```

Otherwise it looks like [pups already pipes in env variables on the host](https://github.com/discourse/pups#environment-variables), too.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 2, 2020, 4:41pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/4 "2020-09-02T16:41:10Z")

</div>

> [@justin](#):
>
> Can’t you use the `env:` section of the container definition for this?

That gives me an error related to docker. That would be very intuitive if it worked like that. :slight_smile: Or I’m crazy. I think there’s a rule to accept custom vars starting with `DISCOURSE_`.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 2, 2020, 5:00pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/5 "2020-09-02T17:00:37Z")

</div>

I’ll try the env thing once again when I get at my desk tomorrow.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 2, 2020, 6:02pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/6 "2020-09-02T18:02:03Z")

</div>

So, I was crazy. This works fine. The issue was due to a multiline value. I removed it and the build suceeded. But the question is, how to use multiline values?

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 3, 2020, 9:24pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/7 "2020-09-03T21:24:22Z")

</div>

Yuhoo, I was able to make it work. The issue was that I was using a RSA Private key as an environment variable. This is what needs to be done to make that work.

wrap your key in single quotes `''` and add an extra `\` with all the `\n` appearing in the key. i.e. `\n` would be come `\\n`.

---

<div class="post-metadata">

### Author: ![featheredtoast](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/featheredtoast/32/116994_2.png) [@featheredtoast](https://meta.discourse.org/u/featheredtoast)
#### Post date: [September 3, 2020, 10:44pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/8 "2020-09-03T22:44:47Z")

</div>

just FYI, there are [easier ways](https://yaml-multiline.info/) of doing that then updating all of the \n directly.

Something like this:

```plaintext
key: |
  ---BEGIN SECRET KEY---
  SHH do not read this is secret
  ---END SECRET KEY---

```

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 3, 2020, 10:49pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/9 "2020-09-03T22:49:47Z")

</div>

> [@featheredtoast](#):
>
> Something like this:

Do you mean pasting the middle part of the key as it is? That would be somewhat more cleaner.

---

<div class="post-metadata">

### Author: ![featheredtoast](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/featheredtoast/32/116994_2.png) [@featheredtoast](https://meta.discourse.org/u/featheredtoast)
#### Post date: [September 3, 2020, 10:51pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/10 "2020-09-03T22:51:20Z")

</div>

I mean that yml has multiline support starting with the `|` character (and optional variations)

You can then paste the key as-is with no edits (aside from prefixed spaces).

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 3, 2020, 10:53pm UTC](https://meta.discourse.org/t/is-it-possible-to-pass-custom-environment-variables-to-the-docker-container/162795/11 "2020-09-03T22:53:46Z")

</div>

> [@featheredtoast](#):
>
> You can then paste the key as-is with no edits (aside from prefixed spaces).

I tried a silly thing I added `|` and an actual new line instead of each `\n` which didn’t work.
