2

For example I've setup name: FOO value: 'bar'.

I've validated that the key value works. Because what does work is:

jobs:
  build:
    docker: 
      - image: circleci/node:10.17.0
    steps:
      - run: |
          node something $FOO

However, the following does not work:

Now when I deploy and try to use it in my application it returns undefined:

console.log(process.env.FOO); // returns undefined

I tried setting it under the 'environment' key in the config.yml file:

jobs:
  build:
    docker: 
      - image: circleci/node:10.17.0
      environment:
        - FOO # note, don't use $FOO
    steps:
      - run: |
        node something $FOO
        ssh $MACHINE -- 'cd /home/ && docker build -t myApp . docker restart myApp'

But still no change.

Should I perhaps pass the variables to the build script in the ssh command?

Any ideas?


update based on Delena's tip

Kept ./circle-ci/config.yml as:

jobs:
  build:
    docker: 
      - image: circleci/node:10.17.0
      environment:
        FOO: $FOO

Then in the docker-compose file:

myApp:
  environment:
    - FOO

Will accept the answer when the build is green

Remi
  • 4,663
  • 11
  • 49
  • 84

1 Answers1

1

It looks like you're trying to access the environment variable from an app that runs in a Docker container, but you're not setting the environment variable in the container.

If that's the case, you can check out How to set an environment variable in a running docker container, but it looks like you'll have to stop the container and restart it again with the environment variable.

You could do something like:

ssh $MACHINE -- 'cd /home/ && docker build -t myApp && docker stop myApp && docker run -e "FOO=$FOO"'

Also check out the ENV (environment variables) section in the docker run docs.

D Malan
  • 10,272
  • 3
  • 25
  • 50
  • That makes sense. I am not running Docker using the `docker run`. But docker-compose. Can I also set env variables using docker-compose instead of the `-e` flag on docker run? (I'm new to Docker) – Remi Apr 19 '20 at 18:36
  • Oké. Found it, Have added it under an `environment` key in the docker-compose.yml file. (i.e. `myApp: environment: - FOO`) Should I then also refer to these variables in the Dockerfile itself? (i.e. `ENV FOO $FOO`) – Remi Apr 19 '20 at 18:42
  • I don't think you need to. Only adding it your docker-compose.yml file should be fine. Since you're using Docker Compose, you could then use `docker-compose build myApp` to rebuild the container and `docker-compose up --force-recreate` to stop and restart the app. Or maybe just `docker-compose up --build --force-recreate` should work. – D Malan Apr 19 '20 at 18:47
  • Thank you so much . Trying this out. Last thing, the 'environment' setting should then entirely be removed from `.circle-ci/config.yml`? Or is that making the reference for the docker-compose file? – Remi Apr 19 '20 at 18:51