How to update PATH environment variable in a Dockerfile?

Better Stack Team
Updated on April 7, 2023

To update the PATH environment variable in a Dockerfile, you can use the ENV instruction to set the new value of the PATH variable. Here's an example:

 
FROM ubuntu

# Set a new value for the PATH environment variable
ENV PATH="/usr/local/bin:${PATH}"

# Install some packages
RUN apt-get update && apt-get install -y \
    curl \
    git \
    python3

# Run a command that requires one of the packages installed above
CMD ["python3", "--version"]

In this example, the Dockerfile sets the PATH environment variable to include the /usr/local/bin directory, in addition to the existing value of the PATH variable. This ensures that any executables installed in /usr/local/bin will be available in the container's PATH.

Note that the order of the directories in the PATH variable matters, as the shell will search for executables in each directory in order until it finds a match. By adding /usr/local/bin to the beginning of the PATH variable, we ensure that executables installed in this directory take precedence over any other executables with the same name that may be installed elsewhere in the container.

Once the Dockerfile is built and a container is started from the image, the updated PATH environment variable will be available in the container's shell and any child processes.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack