Compose: Environment variables are not set when using docker-compose through ssh

Created on 29 Dec 2016  ·  4Comments  ·  Source: docker/compose

Hello,

Given docker-compose 1.9.0 and the following docker-compose.yml:

version: '2'
services:
kafka:
image: wurstmeister/kafka
environment:
KAFKA_ADVERTISED_HOST_NAME: ${HOSTNAME}

When I execute the following command:

ssh [email protected] "docker-compose up -d

Then the hostname environment variable is not recognized. I get the following message:

The HOSTNAME variable is not set. Defaulting to a blank string.

If I execute the same command on the host "bar" with the user "foo" I don't get the error. Same if I first connect with ssh, and then execute the command.
This is a major issue for us. This workflow happens in our CI.

The only workaround we found is to write the following script:

#!/bin/bash
export HOSTNAME=${HOSTNAME}
docker-compose up -d

And execute it through ssh.. Which is not handsome..

kinquestion

Most helpful comment

I found this which seems relevant. bash does not export $HOSTNAME by default, which explains why it's not being seen by docker-compose.

I was able to reproduce locally in my Ubuntu 16.04 environment so I can confirm this has nothing to do with SSH, either.

All 4 comments

Well, doesn't that happen because when executing the command through ssh directly, you're not actually opening a shell? Meaning that some environment variables like $HOSTNAME will not be set?

Looks like your issue is with UNIX and/or SSH - I don't see anything that can be done about this at the Compose level.

That's what I was thinking. But the following command workaround works.. Which means that the variable HOSTNAME is correctly set during the ssh session. So I don't know why compose do not recognize it... That's weird.

I found this which seems relevant. bash does not export $HOSTNAME by default, which explains why it's not being seen by docker-compose.

I was able to reproduce locally in my Ubuntu 16.04 environment so I can confirm this has nothing to do with SSH, either.

@shin- @m-a-d-e How to fix this on Ubuntu, please clarify ?

Was this page helpful?
0 / 5 - 0 ratings