skip to Main Content

I have a docker container running an alpine image. I want to run a curl command that utilizes a cookie that is stored in a environment variable, TEST_VAR.
I can’t seem to figure out how to run this curl command with the environment variable, I keep getting the message "The TEST_VAR variable is not set. Defaulting to a blank string." If I look at the variables that the alpine container is able to load, I can see the TEST_VAR and its value, but it seems that the curl command is unable to use/read it.. How do I fix this?

Docker compose file:

services:
  alpine_test:
    image: alpine:latest
    container_name: alpine_test
    env_file:
      - default.env
    command: /bin/sh -c "apk add --no-cache curl && 
                        curl -b 'value=$TEST_VAR' https://website.com"
    restart: "on-failure"

With the variable in default.env:

TEST_VAR=thisisatestvariable

2

Answers


  1. In a Dockerfile, to use environment variables from a .env file, you should use the syntax ${VARIABLE_NAME}
    Try this:

    command: /bin/sh -c "apk add --no-cache curl && 
                            curl -b "value=${TEST_VAR}" https://website.com"
    
    Login or Signup to reply.
  2. Docker Compose will process all variables in the compose file using the hosts own environment.

    $$ is the necessary escape to pass a $ through to the container:

    services:
      test:
        image: alpine
        environment:
          FOO: "container"
        command: [ "sh", "-c", "echo $FOO; echo $$FOO" ]
    

    FOO=host docker compose run test

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search