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 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 
3

4 Answers

You can use Environment Replacement in your Dockerfile as follows:

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

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

2

[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}

7

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.

3

You Might Also Like