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

Some client libraries like 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.

@merefield pointed out to me that its indeed possible.

in the custom commands section do

# 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"

6 Likes

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

env:
  CUSTOM_ENV: "value"

Otherwise it looks like pups already pipes in env variables on the host, too.

4 Likes

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_.

2 Likes

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

3 Likes

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?

3 Likes

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.

1 Like

just FYI, there are easier ways of doing that then updating all of the \n directly.

Something like this:

key: |
  ---BEGIN SECRET KEY---
  SHH do not read this is secret
  ---END SECRET KEY---
1 Like

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

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).

1 Like

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