Tuesday, February 1, 2022

[SOLVED] How to permanently change $PATH in docker container

Issue

I am building a docker image and want to permanently change its env vars and path. My simplified dockerfile is like this:

FROM python:3.6.8-slim-stretch

USER root

RUN pip3 install pyspark

RUN touch /etc/profile.d/set-up-env.sh && \
    echo export SPARK_HOME='/usr/local/lib/python3.6/site-packages/pyspark' >> /etc/profile.d/set-up-env.sh && \
    echo export PATH='${SPARK_HOME}/bin:${PATH}' >> /etc/profile.d/set-up-env.sh && \
    echo export PYSPARK_PYTHON='python3.6' >> /etc/profile.d/set-up-env.sh && \
    chmod +x /etc/profile.d/set-up-env.sh

The image can be built successfully with docker build -t data-job-base . But when I run it docker run --rm -it data-job-base bash, in this running container SPARK_HOME is empty and PATH has no change. I cat /etc/profile.d/set-up-env.sh and can see that it is properly written:

export SPARK_HOME=/usr/local/lib/python3.6/site-packages/pyspark
export PATH=${SPARK_HOME}/bin:${PATH}
export PYSPARK_PYTHON=python3.6

I don't understand, why this set-up-env.sh doesn't get run when I start the shell?

Note that modifying /etc/environment has no effect either.


Solution

OK, I have solved this by adding my shell commands in bash.bashrc instead of profile, profile.d, or environment.



Answered By - Z.Wei
Answer Checked By - Senaida (WPSolving Volunteer)