ChatGPT解决这个技术问题 Extra ChatGPT

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:

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 PATH /opt/gtk/bin:$PATH

This is my entire 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
The LD_LIBRARY_PATH and PATH should be set using ENV not export. You are also LD_LIBRARY_PATH shouldn't be pointing at PATH!. Deleting files in the Dockerfile doesn't make your image smaller, check centurylinklabs.com/optimizing-docker-images/?hvid=4wO7Yt.
is the current dockerfile a valid one ?
@HuiWang it may not. It was written, it was written 1.5 years ago and a lot has changed since then. Just make sure you incorporate the changes described in the selected answer.

H
Homme Zwaagstra

You can use Environment Replacement in your Dockerfile as follows:

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

Is = equals sign necessary?
@IgorGanapolsky Not in this instance as it is specifying a single variable. However, it doesn't hurt and is mandatory when specifying multiple variables. See the ENV documentation for more details.
That works! Please take care about the = it needs to be without spaces. If you add spaces next to = like this ENV PATH = "/opt/gtk/bin:${PATH}" WILL CRASH YOUR $PATH
Won't this update the image with the HOST's $PATH appended?
ENV PATH="/opt/gtk/bin:${PATH}" may not be the same as ENV PATH="/opt/gtk/bin:$PATH" The former, with curly brackets, might provide you with the host's PATH. The documentation doesn't suggest this would be the case, but I have observed that it is. This is simple to check just do RUN echo $PATH and compare it to RUN echo ${PATH}
I
ILikeTacos

Although the answer that Gunter posted was correct, it is not different than what I already had posted. The problem was not the ENV directive, but the subsequent instruction RUN export $PATH

There's no need to export the environment variables, once you have declared them via ENV in your Dockerfile.

As soon as the RUN export ... lines were removed, my image was built successfully


RUN A=B, RUN export A, and RUN export A=B, are valid shell commands, but affect the environment only of commands that follow in that same RUN directive (but none are given). Similarly, if you had RUN export PATH=/foo; prog1; prog2; (in the same RUN), the PATH modification would affect prog1 and prog2. So, RUN export $PATH is a noop (because no program uses that modified environment) and it should make no difference if that directive is there or not. By "Gunter", do you mean this answer?
The fix is really to change the PATH value with an ENV directive, not RUN. Then those changes would carry over when the docker builder invokes the following RUN.
d
dankirkd

[I mentioned this in response to the selected answer, but it was suggested to make it more prominent as an answer of its own]

It should be noted that

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

may not be the same as

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

The former, with curly brackets, might provide you with the host's PATH. The documentation doesn't suggest this would be the case, but I have observed that it is. This is simple to check just do RUN echo $PATH and compare it to RUN echo ${PATH}


Just tried to verify this assertion, both commands gave the same output. Using docker 20.10.2.
I just tried it with 20.10.2 on a Mac and the two were not the same. ${PATH} included paths on my laptop (such as /Library/... and /Users/...), while $PATH reflected only paths within the context of my container.
The latest docs.docker.com/engine/reference/builder/… says "Environment variables are notated in the Dockerfile either with $variable_name or ${variable_name}. They are treated equivalently"
Hmmm... "equivalently" doesn't necessarily mean "exactly the same". I suggest anyone interested check whether this is true for $PATH and ${PATH}. 20.10.6 still has the differing behavior.
that's wild. I'm also seeing different output.
T
Thomas Decaux

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.

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:

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

So PATH is set for all users.


As per this Ubuntu documentation, /etc/environment is a list of assignment expressions, not a script, and does not support variable expansion, so it is unlikely that the RUN syntax would work.
Yes, it will be expanded and export PATH=<some path> will be written to /etc/environment, which is still incorrect because that file is not a script but a list of <var name>=<value>. export will likely make it fail unless your system supports some black magic outside the spec.
Most paths to running Docker containers don't read /etc/profile or /etc/environment, so changing these scripts is usually ineffective.