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?
Asked
Active
Viewed 7.3k times
35

1ijk
- 1,417
- 2
- 19
- 31
-
how about here /etc/lbu/lbu.conf – last10seconds Feb 10 '16 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? – 1ijk Feb 11 '16 at 19:24
-
I'm looking for the answer also... Have you found anything yet? – Vlad Frolov Feb 12 '16 at 07:22
-
Unfortunately I have not found much. The only other information I found is from https://www.washington.edu/alpine/tech-notes/config-notes.html under the header "Configuration Inheritance" – last10seconds Feb 12 '16 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. – Law29 Apr 23 '18 at 15:05
-
Having the same issue here – Hizzy Mar 03 '20 at 21:03
2 Answers
46
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
.

SkyRar
- 1,107
- 1
- 20
- 37

Vlad Frolov
- 7,445
- 5
- 33
- 52
-
yes, `/etc/profile` and/or `/etc/profile.d/` were what I ended up using. makes sense – 1ijk Feb 12 '16 at 20:32
-
9How do you get `/etc/profile` to run automatically when you start an alpine docker container interactively? I have added some aliases to an `aliases.sh` file and placed it in `/etc/profile.d`, but when I start the container using `docker 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! – Jeff Kilbride Jun 25 '16 at 01:28
-
3
-
2@VladFrolov Thanks! I asked this question in another thread and got an answer there, too. This also works: `sh -l`. – Jeff Kilbride Jun 26 '16 at 23:39
-
other very nice thing to have in hand is `sudo -i
`. I had to export a http proxy, everytime I had to run something that installed things from internet, I had to `sudo su` then `export http.....` then `apk blabla`.. now `sudo -i apk add whatever` – Tonsic Aug 09 '18 at 23:27 -
1@Tonsic you may find interesting [this solution](https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo) for keeping environment variables with sudo. – Vlad Frolov Aug 10 '18 at 04: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. – Orlando Aug 11 '20 at 09:08
17
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! – Eti Sep 16 '19 at 15:28
-
3A quick note: values set by `ENV` will be inherited by all other docker images that use your image as `FROM`. It may have unexpected consequences when your `/etc/profile` is available in other child images – Slav Mar 25 '20 at 16:34