36

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?

6
  • how about here /etc/lbu/lbu.conf Commented 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?
    – 1ijk
    Commented Feb 11, 2016 at 19:24
  • I'm looking for the answer also... Have you found anything yet? Commented 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" Commented 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.
    – Law29
    Commented Apr 23, 2018 at 15:05

2 Answers 2

45

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.

7
  • 11
    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! Commented Jun 25, 2016 at 1:28
  • 3
    @JeffKilbride use sh --login instead of just sh. Commented Jun 25, 2016 at 5:08
  • 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
  • 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
    Commented Aug 11, 2020 at 9:08
19

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.

3
  • 2
    This is actually a very nice alternative without the overhead of --login. Thank you!
    – Eti
    Commented Sep 16, 2019 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
    Commented Mar 25, 2020 at 16:34
  • This should be accepted as the correct answer. The currently accepted answer only works when --login is passed to sh, but this works all the time. Commented Jul 10 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.