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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github