How to pass project defined env variables to docker

I’ve been at this for hours now. Say I did
‘docker run -e TOKEN=$TOKEN ……’ inside my container it would token would be a literal $TOKEN and not what Ive defined it to be inside circleci project env variables. Anyone know a workaround for this?

Hi @fortsharif,

If possible, would you be able to share your config, or the org/project slug where you are running into this issue?

I am able to get the correct value to print out when using the following sample config:

version: 2.1

jobs:
  build:
    docker:
      - image: cimg/go:1.20.2
    steps:
      - setup_remote_docker:
          version: 20.10.14
      - run: |
          TOKEN="TEST123456"
          docker run -e TOKEN=$TOKEN cimg/go:1.20.2 /bin/bash -c 'echo $TOKEN'
workflows:
  workflow1:
    jobs:
      - build

This works thanks very much, what I had was

  build:
    docker:
      - image: fortsharif/fpl_db_ci:v2
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
    environment:
      FPL_TOKEN: $FPL_TOKEN
    steps:
      - setup_remote_docker:
          version: 20.10.14
      - run:
          name: "Start bot"
          command: |
            docker run --name fpl-bot \
            -e FPL_TOKEN=$FPL_TOKEN \
            fortsharif/fpl_db_ci:v2 /bin/bash -c 'python src/bot.py'

changed to the way you did it

  build:
    docker:
      - image: fortsharif/fpl_db_ci:v2
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
    steps:
      - setup_remote_docker:
          version: 20.10.14
      - run:
          name: "Start bot"
          command: |
            FPL_TOKEN=$FPL_TOKEN
            docker run --name fpl-bot \
            -e FPL_TOKEN=$FPL_TOKEN \
            fortsharif/fpl_db_ci:v2 /bin/bash -c 'python src/bot.py'

Do you have any idea why my original one didnt work?

I can add a guess as to why it did not work, variables have an order of precedence with project variables being lower than those defined with the “environment:” statement.

My guess is that

    environment:
      FPL_TOKEN: $FPL_TOKEN

First creates a new variable called FPL_TOKEN and then searches all the defined variables called FPL_TOKEN to get the value to be assigned. The issue being that the search will use the order of precedence and so find the newly created variable (which is null) first and so assign null.

If true this should really be treated as an error condition.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.