Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker compose environment variable for command

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?

like image 286
f-z-N Avatar asked Nov 06 '16 07:11

f-z-N


People also ask

Can Docker compose Read environment variables?

If you need to define various configuration values, environment variables are your best friend. Like many tools, Docker and, more specifically, Docker Compose can both define and read environment variables to help keep your configurations clean and modular.

How do I set an environment variable in docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.


2 Answers

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

like image 173
dnephin Avatar answered Oct 29 '22 17:10

dnephin


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" 
like image 31
C-nit Avatar answered Oct 29 '22 16:10

C-nit