docker为命令撰写环境变量 [英] docker compose environment variable for command

查看:179
本文介绍了docker为命令撰写环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过compose命令选项将环境变量传递到自定义映像时遇到麻烦:

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

我的撰写文件:

---

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

我的映像只是一个自定义jre映像,其入口点设置为接受jvm的shell脚本论点。从enrtypoint调用的我的run.sh

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 $@

我需要在运行时将值传递给命令,因为然后我可以将我的图像用于许多其他jar,只需将参数更改为我的图像即可。

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.

这就是我得到的:

 $> 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-使用命令容器环境变量

Docker-compose环境变量

,但无法使其正常工作。有什么方向吗?

but could not get it working. Any directions please?

推荐答案

在解析文件时,Compose正在读取变量。但是设置 environment 只能为容器提供值,而不能为文件解析提供值。

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

尝试在Compose文件中使用变量,您需要将这些变量放入 .env 文件。

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

请参见 https://docs.docker.com/compose/compose-file/#variable -替代以获取完整文档

这篇关于docker为命令撰写环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆