在Dockerfile中,如何更新PATH环境变量?

时间:2014-11-23 19:58:33

标签: docker dockerhub

我有一个从源代码下载和构建GTK的dockerfile,但以下行没有更新我的图像的环境变量:

RUN PATH="/opt/gtk/bin:$PATH"
RUN export PATH

我读到我应该使用ENV来设置环境值,但以下指令似乎也不起作用:

ENV PATH /opt/gtk/bin:$PATH

这是我的整个Dockerfile:

FROM ubuntu
RUN apt-get update
RUN apt-get install -y golang gcc make wget git libxml2-utils libwebkit2gtk-3.0-dev libcairo2 libcairo2-dev libcairo-gobject2 shared-mime-info libgdk-pixbuf2.0-* libglib2-* libatk1.0-* libpango1.0-* xserver-xorg xvfb

# Downloading GTKcd
RUN wget http://ftp.gnome.org/pub/gnome/sources/gtk+/3.12/gtk+-3.12.2.tar.xz
RUN tar xf gtk+-3.12.2.tar.xz
RUN cd gtk+-3.12.2

# Setting environment variables before running configure
RUN CPPFLAGS="-I/opt/gtk/include"
RUN LDFLAGS="-L/opt/gtk/lib"
RUN PKG_CONFIG_PATH="/opt/gtk/lib/pkgconfig"
RUN export CPPFLAGS LDFLAGS PKG_CONFIG_PATH
RUN ./configure --prefix=/opt/gtk
RUN make
RUN make install

# running ldconfig after make install so that the newly installed libraries are found.
RUN ldconfig

# Setting the LD_LIBRARY_PATH environment variable so the systems dynamic linker can find the newly installed libraries.
RUN LD_LIBRARY_PATH="/opt/gtk/lib"

# Updating PATH environment program so that utility binaries installed by the various libraries will be found.
RUN PATH="/opt/gtk/bin:$PATH"
RUN export LD_LIBRARY_PATH PATH

# Collecting garbage
RUN rm -rf gtk+-3.12.2.tar.xz

# creating go code root
RUN mkdir gocode
RUN mkdir gocode/src
RUN mkdir gocode/bin
RUN mkdir gocode/pkg

# Setting the GOROOT and GOPATH enviornment variables, any commands created are automatically added to PATH
RUN GOROOT=/usr/lib/go
RUN GOPATH=/root/gocode
RUN PATH=$GOPATH/bin:$PATH
RUN export GOROOT GOPATH PATH

4 个答案:

答案 0 :(得分:383)

您可以在Dockerfile中使用Environment Replacement,如下所示:

ENV PATH="/opt/gtk/bin:${PATH}"

答案 1 :(得分:41)

虽然Gunter发布的答案是正确的,但与我发布的内容没有什么不同。问题不是ENV指令,而是后续指令RUN export $PATH

在Dockerfile中通过ENV声明环境变量之后,无需导出环境变量。

删除RUN export ...行后,我的图片构建成功

答案 2 :(得分:2)

不建议这样做(如果要创建/分发干净的Docker映像),因为PATH变量是由/etc/profile脚本设置的,因此可以覆盖该值。

head /etc/profile

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH

在Dockerfile的末尾,您可以添加:

RUN echo "export PATH=$PATH" > /etc/environment

因此为所有用户设置了PATH。

答案 3 :(得分:0)

[我是根据所选答案回答此问题的,但建议将其作为自己的答案来突出显示]

应注意的是

ENV PATH="/opt/gtk/bin:${PATH}" 

可能与

不同
ENV PATH="/opt/gtk/bin:$PATH" 

前者带有花括号,可能会为您提供主机的路径。文档并没有建议是这种情况,但是我观察到确实如此。只需检查RUN echo $PATH并将其与RUN echo ${PATH}

进行比较,这很简单