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

查看:29
本文介绍了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 脚本.我从入口点调用的 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/compose-file-v3/#variable-substitution 获取完整文档

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

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