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

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

问题描述



像在bash var = $(date)中一样,我需要在dockerfile中填入一个变量

编辑1



date就是一个例子。
在我的情况下,我使用 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。
但是我不知道我如何在var中用dockerfile解析这个结果

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

RESULT



在我的用例中

  FROM phusion / baseimage:latest 

但是其他情况下问题仍然未解决

解决方案

我有同样的问题,并且通过在docker文件中使用RUN命令,找到了设置环境变量作为结果的方法。



例如我需要为Rails应用程序设置SECRET_KEY_BASE一次,而不会随着我的运行而改变:

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

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

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

和我的env变量从root可用,即使在bash登录之后。
或可能

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

那么它在CMD和ENTRYPOINT命令中可用的变量



Docker将其缓存为图层,只有在您之前更改一些字符串时才会更改。



您还可以尝试< a href =https://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables>设置环境变量的不同方式


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

Like in bash var=$(date)

EDIT 1

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

RESULT

In my use case

FROM phusion/baseimage:latest

But the question remains unresolved for other case

解决方案

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

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)"

Instead it i write to Dockerfile string like:

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

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'

then it variable available in CMD and ENTRYPOINT commands

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天全站免登陆