docker compose environment variable for command

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"

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