10

I know I can export PATH="$PATH:my_path" to add to PATH variable in current session.

I also know that I can add this line to ~/.bash_profile to make it persist across sessions for my user:

echo 'PATH="$PATH:my_path"' >> ~/.bash_profile

I also know that in linux ubuntu I can add that line to /etc/bash.bashrc and it's avaiable always for all users.

However, my linux inside the container is alpine and I don't know where to add that file. I couldn't find it in Google.

2 Answers2

6

Alpine uses Ash and from the ash(1) man page:

Therefore, a user should place commands to be executed only at login time in the .profile file, and commands that are executed for every shell inside the ENV file.

but, "every shell" isn't correct in the docs because the ENV variable is not read for non-interactive shells. For non-interactive shells, we can set the PATH variable in the Dockerfile:

ENV PATH=/root/.local/bin:$PATH

On the other hand for login shells (/bin/sh --login) setting PATH in the Dockerfile doesn't work because login scripts usually set the PATH variable which overrides whatever was set before. So to make it work for login shells we can add the following into /etc/profile:

RUN echo 'export "PATH=$PATH:my_path"' >> /etc/profile

To sum it up, I just add these two commands into the Dockerfile to update the PATH variable correctly:

ENV PATH=/root/.local/bin:$PATH
RUN echo 'export "PATH=$PATH:my_path"' >> /etc/profile

This works for Ash (/bin/sh), Bash (/bin/bash) and ZSH (/bin/zsh) on Alpine.

3

Alpine Linux uses by default ash as shell, it is quite similar to bash. The settings for everyone would be in the file /etc/profile

https://stackoverflow.com/a/35357011/2955337

if I had to find this myself I would run a find to see where PATH was set, I would grep through the etc directory.

grep "PATH=" /etc/*
sleepyhead
  • 176
  • 4