I know, with Ubuntu, you can set default values for environment variables in /etc/environment
; I do not see that file in Alpine linux. Is there a different location for setting system-wide defaults?
-
how about here /etc/lbu/lbu.conf– last10secondsFeb 10, 2016 at 21:06
-
@Rick not seeing the effect. do you know of any good documentation for this in Alpine? or do you have more details, tips, tricks, with setting envars in Alpine?– 1ijkFeb 11, 2016 at 19:24
-
I'm looking for the answer also... Have you found anything yet?– Vlad FrolovFeb 12, 2016 at 7:22
-
Unfortunately I have not found much. The only other information I found is from washington.edu/alpine/tech-notes/config-notes.html under the header "Configuration Inheritance"– last10secondsFeb 12, 2016 at 15:41
-
1@Rick that is not the right Alpine; your URL refers to the Alpine Mail User Agent, not to the minimal container-adapted Linux-based OS.– Law29Apr 23, 2018 at 15:05
2 Answers
It seems that /etc/profile
is the best place I could find. At least, some environment variables are set there:
export CHARSET=UTF-8
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PAGER=less
export PS1='\h:\w\$ '
umask 022
for script in /etc/profile.d/*.sh ; do
if [ -r $script ] ; then
. $script
fi
done
According to the contents of /etc/profile
, you can create a file with .sh
extension in /etc/profile.d/
and you have to pass --login
every time to load the env variables e.g docker exec -it container sh --login
.
-
9How do you get
/etc/profile
to run automatically when you start an alpine docker container interactively? I have added some aliases to analiases.sh
file and placed it in/etc/profile.d
, but when I start the container usingdocker run -it [my_container] sh
, my aliases aren't active. I have to manually type. /etc/profile
from the command line each time. Is there some other configuration necessary to get/etc/profile
to run at login? Any insight is appreciated! Jun 25, 2016 at 1:28 -
3
-
2@VladFrolov Thanks! I asked this question in another thread and got an answer there, too. This also works:
sh -l
. Jun 26, 2016 at 23:39 -
1@Tonsic you may find interesting this solution for keeping environment variables with sudo. Aug 10, 2018 at 4:06
-
1For anyone wondering. This also works for non-root user cronjobs. E.g. using a Docker container running crond as root. And having a crontab for user foo that runs a cronjob that needs environment variables from the /etc/profile.– OrlandoAug 11, 2020 at 9:08
If you are talking about Alpine Docker image, then you can define those env variables inside Dockerfile like below. Here you don't need to pass --login
every time. These variables will be automatically available system wide globally.
FROM alpine
ENV GITHUB_TOKEN=XXXXXXXXXXXXXXXXXXXXXXX \
COMPOSER_HOME=/home/deploy/.composer
Also you can define your alias, env etc inside /etc/profile and define a ENV
inside Dockerfile like below to source the profile automatically.
FROM alpine
ENV GITHUB_TOKEN=XXXXXXXXXXXXXXXXXXXXXXX \
COMPOSER_HOME=/home/deploy/.composer
ENV ENV="/etc/profile"
-
2This is actually a very nice alternative without the overhead of --login. Thank you!– EtiSep 16, 2019 at 15:28
-
3A quick note: values set by
ENV
will be inherited by all other docker images that use your image asFROM
. It may have unexpected consequences when your/etc/profile
is available in other child images– SlavMar 25, 2020 at 16:34