Set an empty environment variable in using rancher-compose

How does one set an empty environment variable in using rancher-compose?

I’ve tried:

  • docker-compose.yml
environment:
    EMPTY1:
    EMPTY2: ""
    EMPTY3: ''
    EMPTY4: null
    EMPTY5:  ~

But it doesn’t create empty variables.

It’s possible to do so via UI. Empty variables are visible in API and container:

"launchConfig": {
  "environment": {
    "EMPTY1": '',
    "EMPTY2": '',
    "EMPTY3": '',
    "EMPTY4": '',
    "EMPTY5": '',
  }

env command output from container:

/ # env
HOSTNAME=ba962f043e17
SHLVL=2
HOME=/root
TERM=xterm-256color
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
EMPTY1=
EMPTY2=
EMPTY3=
EMPTY4=
EMPTY5=

Details:

  • Rancher: v1.1.1
  • Cattle: v0.165.7
  • User Interface: v1.1.12
  • Rancher Compose: v0.8.6

P.S. I’m asking this question here, since my comment on the related GitHub issue haven’t attracted anyone’s attention.

You’re syntax and spacing seem wrong. From here: https://docs.docker.com/compose/compose-file/#/environment.

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:

environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET

Well, I’m using an array notation and it works fine for other environment variables. It’s empty ones that I’m struggling with.

I’ve tried to use a dictionary, but it’s just assigns values literally:

  • docker-compose.yml
  environment:
    - EMPTY1
    - EMPTY2=""
    - EMPTY3=''
    - EMPTY4=null
    - EMPTY5=~

env command output from container:

/ # env
HOSTNAME=18a158c41d55
SHLVL=2
HOME=/root
TERM=xterm-256color
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
EMPTY2=""
EMPTY3=''
PWD=/
EMPTY4=null
EMPTY5=~

It doesn’t look like you’ve tried this:

- EMPTY1=

Yep, missed that. I’ve tried it now and it doesn’t work (and it shouldn’t according to docs). It will create env. variable only if it exists on a machine running rancher-compose.

OK, fair enough, that’s me out of ideas.

Why do you need an empty variable anyway?

I’m using richarvey/nginx-php-fpm/ container with custom startup script that does templating via sed. It pulls templates from env. variables and if they don’t exist, templates are not replaced.

Example:

  • docker-compose.yml
environment:
  CI_PROTO: http
  CI_DN: www.example.com
  CI_BASE: /app
  • config.php in container
define('ASSETS_URL', '$$_CI_PROTO_$$://$$_CI_DN_$$/$$_CI_BASE_$$');

So if I’d like to put my app in the root directory, putting an empty CI_BASE env. variable would be the easiest way. But since I can’t, I get this:

define('ASSETS_URL', 'http://www.example.com/$$_CI_BASE_$$');

Sure, this can be fixed easily one way or another (which I’ve already done), but I’d like to know if there is more straightforward way.

I see, thanks for providing the info, always useful to see what people are doing.

I mostly use Confd and that (or rather Go text templating that it uses) has an if operator where a value won’t be populated if the environment variable doesn’t exist. I’m not suggesting your switch to it, but perhaps it might inform a future choice.

Cheers

@beatcracker This doesn’t work with rancher-compose now. Can you file an issue in Github?

Thanks for the tip! ConfD is definitely looks promising.

1 Like

Sure, thanks for reply!

https://github.com/rancher/rancher/issues/5935