35

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?

1ijk
  • 1,417
  • 2
  • 19
  • 31

2 Answers2

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
  • 9
    How 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
    @JeffKilbride use `sh --login` instead of just `sh`. – Vlad Frolov Jun 25 '16 at 05:08
  • 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
  • 1
    For 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" 
Anoyi
  • 41
  • 4
SkyRar
  • 1,107
  • 1
  • 20
  • 37
  • 2
    This is actually a very nice alternative without the overhead of --login. Thank you! – Eti Sep 16 '19 at 15:28
  • 3
    A 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