67

I am having troubles in passing environment variables to my custom image via the compose command option:

My compose file:

---

version: '2'
services:
myservice:
  image: mycustomimage_lms
  environment:
    CONF_HOME: /opt/apps-java/
    APP_ENV: dev
    UUID: me1
  command: -Dconfig.home=${CONF_HOME} -Dcomponent.name=LMS -Denv=${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=${UUID} -jar /opt/apps-java/my.jar
  ports:
    - "9060"
  volumes:
    - ./:/opt/apps-java/
    - ./:/var/logs/apps-logs/
    - ./:/tmp/data

My image is just a custom jre image which has an entrypoint set to a shell script that accepts jvm arguments. My run.sh that is called from enrtypoint

#!/bin/sh
export JAVA_HOME="/usr/java/latest/"
exec $JAVA_HOME/bin/java $@

I need to pass values to command at runtime since I can then use my image for a lot of other jars and just changing parameters to my image.

This is what i get:

 $> docker-compose up
 WARNING: The CONF_HOME variable is not set. Defaulting to a blank string.
 WARNING: The APP_ENV variable is not set. Defaulting to a blank string.
 WARNING: The UUID variable is not set. Defaulting to a blank string.

I have also gone through couple of answers such as :

Docker Compose - Command using Container Environment Variable

and

Docker-compose environment variables

but could not get it working. Any directions please?

3 Answers 3

79

The variables are being read by Compose when the file is parsed. But setting environment only provides values to the container, not to the file parsing.

If you're trying to pass those variables into the container, you need to escape them in the command using an extra $

-Dconfig.home=$${CONF_HOME} -Dcomponent.name=LMS -Denv=$${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=$${UUID

If you're just trying to use variables in the Compose file, you need to put those variables into an .env file.

See https://docs.docker.com/compose/compose-file/compose-file-v3/#variable-substitution for the full documentation

Sign up to request clarification or add additional context in comments.

9 Comments

This doesn't work for me. When I used $${VAR_NAME}, the value when echo that var is $VAR_NAME
It also did not work for me. A literal $VARIABLE is passed to the command, and it does not get expanded within the container.
sh -c "echo $$FOO" works for me, stackoverflow.com/a/46456139/5760975
All approaches stated here do not seem to work with Docker as of April 2023. Did anyone find a currently working solution?
I saw this issue from github, it works for me: github.com/docker/compose/issues/9757#issuecomment-1217460695
|
12

This is syntax specific and despite instructions in other answers, I didn't manage to run it properly until finding by trial and error following:

version: "3.9"
services:
  test-vars-service:
    image: alpine
    environment:
      VAR_ONE: "Hello"
      VAR_TWO: "World"
    command: sh -c 'echo "$$VAR_ONE $$VAR_TWO"'

This results in:

[+] Running 1/0
 âś” Container var-test-test-vars-service-1  Created                                                                                                                           0.0s 
Attaching to var-test-test-vars-service-1
var-test-test-vars-service-1  | Hello World
var-test-test-vars-service-1 exited with code 0

Comments

2

Escaping the $ so the variable is not substituted immediately but later in the container environment is the trick, as the accepted answer does in the the docker-compose.yml.

I just wanted to add that in case you pass a command to the docker-compose call, you need to escape the character in the shell, i.e. with \

docker-compose run --rm myservice "-Dconfig.home=\${CONF_HOME} -Dcomponent.name=LMS -Denv=\${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=\${UUID} -jar /opt/apps-java/my.jar"

1 Comment

does not works for compose file which is what the OP asked for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.