docker compose environment variable for command

橙三吉。 提交于 2020-06-08 15:52:28

问题


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?


回答1:


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/#variable-substitution for the full documentation



来源:https://stackoverflow.com/questions/40447029/docker-compose-environment-variable-for-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!