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– last10secondsCommented Feb 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?– 1ijkCommented Feb 11, 2016 at 19:24
-
I'm looking for the answer also... Have you found anything yet?– Vlad FrolovCommented Feb 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"– last10secondsCommented Feb 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.– Law29Commented Apr 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
.
-
11How 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! Commented 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
. Commented Jun 26, 2016 at 23:39 -
1@Tonsic you may find interesting this solution for keeping environment variables with sudo. Commented 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.– OrlandoCommented Aug 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 aliases, environment variables, modify PATH, etc, by creating a script e.g. /etc/.shinit
and define an ENV
variable to point to that script and that script will run on every shell invocation. Here is an example:
FROM alpine
ENV ENV=/etc/.shinit
RUN echo 'export "PATH=$PATH:/root/.local/bin"' > ${ENV}
All this is explained in the ash(1) man page.
-
2This is actually a very nice alternative without the overhead of --login. Thank you!– EtiCommented Sep 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– SlavCommented Mar 25, 2020 at 16:34 -
This should be accepted as the correct answer. The currently accepted answer only works when
--login
is passed tosh
, but this works all the time. Commented Jul 10 at 22:36