本文翻译自:In a Dockerfile, How to update PATH environment variable?

I have a dockerfile that download and builds GTK from source, but the following line is not updating my image's environment variable: 我有一个从源代码下载并构建GTK的dockerfile,但是以下行未更新映像的环境变量:

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

I read that that I should be using ENV to set environment values, but the following instruction doesn't seem to work either: 我读到我应该使用ENV来设置环境值,但是以下指令似乎也不起作用:

ENV PATH /opt/gtk/bin:$PATH

This is my entire Dockerfile: 这是我的整个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

#1楼

参考:https://stackoom.com/question/1PGhK/在Dockerfile中-如何更新PATH环境变量


#2楼

Although the answer that Gunter posted was correct, it is not different than what I already had posted. 尽管Gunter发布的答案是正确的,但与我已经发布的答案没有什么不同。 The problem was not the ENV directive, but the subsequent instruction RUN export $PATH 问题不在于ENV指令,而是随后的指令RUN export $PATH

There's no need to export the environment variables, once you have declared them via ENV in your Dockerfile. 一旦在Dockerfile中通过ENV声明了环境变量,就无需导出环境变量。

As soon as the RUN export ... lines were removed, my image was built successfully 删除RUN export ...行后,便成功构建了我的映像


#3楼

您可以在Dockerfile使用环境替换 ,如下所示:

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

#4楼

This is discouraged (if you want to create/distribute a clean Docker image), since the PATH variable is set by /etc/profile script, the value can be overridden. 不建议这样做(如果要创建/分发干净的Docker映像),因为PATH变量是由/etc/profile脚本设置的,因此可以覆盖该值。

head /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

At the end of the Dockerfile, you could add: 在Dockerfile的末尾,您可以添加:

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

So PATH is set for all users. 因此,为所有用户设置了PATH。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐