使用 DockerFile 中的命令结果解析变量 [英] Parse a variable with the result of a command in DockerFile

查看:40
本文介绍了使用 DockerFile 中的命令结果解析变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用命令的结果在 dockerfile 中填充一个变量

I need to fill a variable in dockerfile with the result of a command

就像在 bash 中一样 var=$(date)

Like in bash var=$(date)

编辑 1

日期就是一个例子.在我的情况下,我使用 FROM phusion/baseimage:0.9.17 所以我希望在每个建筑物都使用最后一个版本,所以我使用这个curl -v --silent api.github.com/repos/phusion/baseimage-docker/tags 2>&1 |grep -oh 'rel-.*",' | head -1 | sed 's/",//' |sed 's/rel-//' ==> 0.9.17.但我不知道如何使用 dockerfile 在 var 中解析它以获得此结果

date is a example. in my case i use FROM phusion/baseimage:0.9.17 so i want at each building use the last version so i use this curl -v --silent api.github.com/repos/phusion/baseimage-docker/tags 2>&1 | grep -oh 'rel-.*",' | head -1 | sed 's/",//' | sed 's/rel-//' ==> 0.9.17. but i don't know how i parse it in var with dockerfile for this result

ENV verbaseimage=curl...
FROM phusion/baseimage:$verbaseimage

结果

在我的用例中

FROM phusion/baseimage:latest

但对于其他情况,问题仍未解决

But the question remains unresolved for other case

推荐答案

我遇到了同样的问题,并找到了通过在 dockerfile 中使用 RUN 命令将环境变量设置为函数结果的方法.

I had same issue and found way to set environment variable as result of function by using RUN command in dockerfile.

例如,我只需为 Rails 应用程序设置一次 SECRET_KEY_BASE,而无需像运行时那样更改:

For example i need to set SECRET_KEY_BASE for Rails app just once without changing as would when i run:

docker run  -e SECRET_KEY_BASE="$(openssl rand -hex 64)"

相反,我将其写入 Dockerfile 字符串,例如:

Instead it i write to Dockerfile string like:

RUN bash -l -c 'echo export SECRET_KEY_BASE="$(openssl rand -hex 64)" >> /etc/bash.bashrc'

即使在 bash 登录后,我的 env 变量也可以从 root 获得.或者可能是

and my env variable available from root, even after bash login. or may be

RUN /bin/bash -l -c 'echo export SECRET_KEY_BASE="$(openssl rand -hex 64)" > /etc/profile.d/docker_init.sh'

然后它在 CMD 和 ENTRYPOINT 命令中可用的变量

then it variable available in CMD and ENTRYPOINT commands

Docker 将其缓存为层,并且仅在您更改之前的某些字符串时才更改.

Docker cache it as layer and change only if you change some strings before it.

您也可以尝试不同的方式来设置环境变量.

You also can try different ways to set environment variable.

这篇关于使用 DockerFile 中的命令结果解析变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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