1

I haven't posted on SO in a long while, I hope I am not breaking any rules.

I have an environment variable in my ~/.bash_profile exported as below and I made sure to run source ~/.bash_profile:

export HOSTNAME=some-name

And this is how my docker-compose.yml looks like:

version: '3'

services:
    someservice:
        build:
            context: .
            dockerfile: ./Dockerfile
        image: ...
        container_name: ...
        restart: unless-stopped
        hostname: "someservice.${HOSTNAME:?Error: env var HOSTNAME is not defined or is empty}"
        ...

If I run docker-compose up -d, I will get the following error:

ERROR: Invalid interpolation format for "hostname" option in service "some-service":
 "someservice.${HOSTNAME:?Error: env var HOSTNAME is not defined or is empty}"

If I replace HOSTNAME above with some-name, I will get the following error:

Starting someservice ... error

ERROR: for someservice  Cannot start service: OCI runtime create failed: 
container_linux.go:346: starting container process caused "process_linux.go:449: 
container init caused \"sethostname: invalid argument\"": unknown

ERROR: Encountered errors while bringing up the project.

Any idea why I am getting the error and how to troubleshoot and/or fix it? Thank you so much!

Edit1: Fixed typo in my export

Edit2: This might help with troubleshooting: Removing the ERROR part from the yaml file fixed the issue. So, in my docker-compose.yml I just have:

...
   hostname: "someservice.${HOSTNAME} 
...
Yalda
  • 680
  • 1
  • 18
  • 39
  • 2
    Does this answer your question? [How do I pass environment variables to Docker containers?](https://stackoverflow.com/questions/30494050/how-do-i-pass-environment-variables-to-docker-containers) – Philippe B. Jan 16 '20 at 16:23
  • Setting `hostname:` for a container has almost no perceivable effect and you can usually delete this setting. – David Maze Jan 16 '20 at 20:28

2 Answers2

2

To expose an environment value we use = not :, change your bash profile to export HOSTNAME=some-name

According to the documentation, other extended shell-style features, such as ${VARIABLE/foo/bar}, are not supported. See https://docs.docker.com/compose/compose-file/. You might need to only use "${HOSTNAME:?Error: env var HOSTNAME is not defined or is empty}" without someservice.

xwlee
  • 1,083
  • 1
  • 11
  • 29
  • Thank you very much for your comment. Unfortunately, I mistyped my question. I actually have `export HOSTNAME=some-name`. – Yalda Jan 16 '20 at 18:44
0

Does it work if you set the version of your compose file to '2.1'? This is a diagnostic check to confirm the documentation says what I think it does, so if you have to comment/change other lines to avoid using 3.x features that's ok for now.

The compose file documentation indicates that :? expansion works for compose files version 2.1, and I'm wondering if that was one of the things that didn't get ported over to version 3 compose files (there are a few things like that)