问题描述
我正在将交叉编译环境设置为docker容器.在构建过程中,我为嵌入式设备安装了一个SDK:
I am setting up a cross-compile environment as a docker container. During the build process I install a SDK for an embedded device:
ADD sdk.tar /opt
WORKDIR /opt/
RUN sdkinstall.sh
将sdk安装在文件夹/opt/sdk/
which installs the sdk in a folder /opt/sdk/
现在我有一个文件/opt/sdk/envsetup
,其中包含一些环境变量:
Now I have a file /opt/sdk/envsetup
that contains some environment variables:
export SDKTARGETSYSROOT=/opt/sdk/sysroots/cortexa9hf-vfp-neon-gad-linux-gnueabi
export PATH=/opt/sdk/sysroots/x86_64-gadsdk-linux/usr/bin:/opt/sdk/sysroots/x86_64-gadsdk-linux/usr/bin/arm-gad-linux-gnueabi:$PATH
export PKG_CONFIG_SYSROOT_DIR=$SDKTARGETSYSROOT
.... and so on
通常,当我在容器中运行shell时,我会执行source /opt/sdk/envsetup
,然后从shell中编译内容.
Usually, when I would run a shell in the container I would do source /opt/sdk/envsetup
and then compile stuff from the shell.
但是,我想要实现的是所有这些环境变量都存在于docker容器内的系统范围内,并且当我执行诸如docker run -it xyz:latest make <somedir>
However, what I am trying to achieve is that all those environment variables are present system wide inside the docker container and are taken into account when I for example do something like docker run -it xyz:latest make <somedir>
我正在通过调用docker run -it xyz:latest echo $SDKTARGETSYSROOT
对此进行测试,并且希望该容器打印/opt/sdk/sysroots/cortexa9hf-vfp-neon-gad-linux-gnueabi`
I am testing this by calling docker run -it xyz:latest echo $SDKTARGETSYSROOT
and I was expecting the container to print /opt/sdk/sysroots/cortexa9hf-vfp-neon-gad-linux-gnueabi`
到目前为止我试图实现的目标:
What I tried to achieve this so far:
- 将var添加到/etc/environment:
RUN cat /opt/sdk/environment-setup-cortexa9hf-vfp-neon-gad-linux-gnueabi | cut -d" " -f 2 >> /etc/environment
- 将var添加到/etc/profile、/root/.profile和/root/.bashrc:
RUN echo -e "# Auto source some variables\nsource /opt/sdk/environment-setup-cortexa9hf-vfp-neon-gad-linux-gnueabi" >> [...]
- 通过
ENV SDKTARGETSYSROOT /opt/sdk/sysroots/cortexa9hf-vfp-neon-gad-linux-gnueabi
在dockerfile中手动添加一个变量,只是为了测试行为 -
.
或/bin/bash -c 'source'
文件/opt/sdk/envsetup
- Adding the vars to /etc/environment:
RUN cat /opt/sdk/environment-setup-cortexa9hf-vfp-neon-gad-linux-gnueabi | cut -d" " -f 2 >> /etc/environment
- Adding the vars to /etc/profile, /root/.profile and /root/.bashrc:
RUN echo -e "# Auto source some variables\nsource /opt/sdk/environment-setup-cortexa9hf-vfp-neon-gad-linux-gnueabi" >> [...]
- Adding one var manually in the dockerfile via
ENV SDKTARGETSYSROOT /opt/sdk/sysroots/cortexa9hf-vfp-neon-gad-linux-gnueabi
, just to test the behaviour .
or/bin/bash -c 'source'
the file/opt/sdk/envsetup
没有一个导致我的预期结果.如何实现这些变量的正确设置?
neither of those led to my expected result. How can I achieve those variables get setup properly?
由于包含变量的文件在docker build
的开头是未知的,并且是在容器内部的sdk安装期间创建的,因此,我不太喜欢通过ENV手动将其内容添加到dockerfile中的想法,即使这行得通,也没有.
As the file that contains the variables is unknown at the beginning of the docker build
and gets created during the sdk installation inside of the container, I do not really like the idea of adding its content manually into the dockerfile via ENV, even if that worked, which did not.
推荐答案
运行此命令时:
docker run -it xyz:latest echo $SDKTARGETSYSROOT
$SDKTARGETSYSROOT
由您的 local 外壳扩展.在任何情况下,您都没有真正在容器内测试环境变量.这将起作用:
The $SDKTARGETSYSROOT
is expanded by your local shell. In no case were you actually testing environment variables inside of your container. This would work:
docker run -it xyz:latest sh -c 'echo $SDKTARGETSYSROOT'
单引号禁止在本地shell中进行变量扩展,并且我们需要显式调用sh -c ...
,因为Docker默认情况下不使用shell执行命令.比较:
The single quotes inhibit variable expansion in your local shell, and we need to explicitly call sh -c ...
because Docker does not, by default, execute commands using a shell. Compare:
$ docker run -it alpine echo $HOME
/home/lars
使用:
$ docker run -it alpine echo '$HOME'
$HOME
使用:
$ docker run -it alpine sh -c 'echo $HOME'
/root
否则,您的几种方法似乎是完成您想要的事情的合理方法.
Otherwise, several of your approaches look like a reasonable way to do what you want.
这篇关于如何从docker容器中的文件在dockerfile中设置系统范围的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!