Docker从命令或文件中设置环境变量 [英] Docker set environment variable from command or file

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

问题描述

我需要设置一个环境变量CLASSPATH.在该变量中,我需要设置命令的结果:

I need to set an environment variable CLASSPATH. In that variable I need to set the result of a command:

hadoop classpath --glob

这将返回大量的Java库,并且都需要将它们设置为该CLASSPATH变量.最大的问题是,我只能在docker构建完成后才能运行此命令,这意味着我必须在ENTRYPOINT中执行此命令.但是我只是无法正常工作.我尝试了不同的方法:

This will return a ton of java libraries and they all need to get set into that CLASSPATH variable. The big problem is that I can only run this command after the docker build is complete, meaning I have to do it in the ENTRYPOINT. But I just cant get it working. I tried differen approaches:

ENTRYPOINT ["sh", "-c", "export CLASSPATH=$(hadoop classpath --glob) ...."
ENTRYPOINT ["sh", "-c", "set CLASSPATH=$(hadoop classpath --glob) ...."
ENTRYPOINT ["sh", "-c", "CLASSPATH=$(hadoop classpath --glob) ...."
ENTRYPOINT ["sh", "-c", "/bin/bash && export CLASSPATH=$(hadoop classpath --glob) ...."

但是没有任何作用.该命令本身正在运行,我使用以下命令对其进行了测试:

But nothing of it is working. The command itself is working, I tested it using:

ENTRYPOINT ["sh", "-c", "echo $(hadoop classpath --glob) >> /tmp/classpath.tmp ...."

此文件在启动后包含正确的内容.因此,设置和保留环境变量只是一个问题.我应该如何设置环境变量?通常您使用类似

This file contains the correct content after startup. So there is just a problem with setting and persisting the environment variable. How am I supposed to set the environment variable? Usually you use something like

ENV CLASSPATH="some classpath"

但是在这里我不能使用ENV语句,因为它不会处理命令$(hadoop classpath --glob)

But here I cant use the ENV statement since it wont process the command $(hadoop classpath --glob)

推荐答案

该问题的唯一可行解决方案是根本不使用Dockerfile中的ENTRYPOINT.

The only working solution for that problem was to not use an ENTRYPOINT in the Dockerfile at all.

  1. 创建entrypoint.sh
  2. 将所需的所有内容(例如导出)添加到该sh文件中
  3. 将dockerfile中的entrypoint.sh复制到/
  4. 在docker run或dockercompose中执行entrypoint.sh
  1. Create entrypoint.sh
  2. Add all the stuff you need (exports for example) into that sh file
  3. Copy entrypoint.sh in dockerfile to /
  4. Execut entrypoint.sh in docker run or dockercompose

注意:您需要通过. /entrypoint.sh执行它.否则它将无法正常工作.

Attention: you need to execute it by . /entrypoint.sh. Otherwise it wont work.

这篇关于Docker从命令或文件中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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