How to get /etc/profile to run automatically in Alpine / Docker How to get /etc/profile to run automatically in Alpine / Docker docker docker

How to get /etc/profile to run automatically in Alpine / Docker


The default shell in Alpine Linux is ash.

Ash will only read the /etc/profile and ~/.profile files if it is started as a login shell sh -l.

To force Ash to source the /etc/profile or any other script you want upon its invocation as a non login shell, you need to setup an environment variable called ENV before launching Ash.

e.g. in your Dockerfile

FROM alpine:3.5ENV ENV="/root/.ashrc"RUN echo "echo 'Hello, world!'" > "$ENV"

When you build that you get:

deployer@ubuntu-1604-amd64:~/blah$ docker build --tag test .Sending build context to Docker daemon  2.048kBStep 1/3 : FROM alpine:3.53.5: Pulling from library/alpine627beaf3eaaf: Pull completeDigest: sha256:58e1a1bb75db1b5a24a462dd5e2915277ea06438c3f105138f97eb53149673c4Status: Downloaded newer image for alpine:3.5 ---> 4a415e366388Step 2/3 : ENV ENV "/root/.ashrc" ---> Running in a9b6ff7303c2 ---> 8d4af0b7839dRemoving intermediate container a9b6ff7303c2Step 3/3 : RUN echo "echo 'Hello, world!'" > "$ENV" ---> Running in 57c2fd3353f3 ---> 2cee6e034546Removing intermediate container 57c2fd3353f3Successfully built 2cee6e034546

Finally, when you run the newly generated container, you get:

deployer@ubuntu-1604-amd64:~/blah$ docker run -ti test /bin/shHello, world!/ # exit

Notice the Ash shell didn't run as a login shell.

So to answer your query, replace

ENV ENV="/root/.ashrc"

with:

ENV ENV="/etc/profile"

and Alpine Linux's Ash shell will automatically source the /etc/profile script each time the shell is launched.

Gotcha: /etc/profile is normally meant to only be sourced once! So, I would advise that you don't source it and instead source a /root/.somercfile instead.

Source: https://stackoverflow.com/a/40538356


You still can try in your Dockerfile a:

RUN echo '\        . /etc/profile ; \    ' >> /root/.profile

(assuming the current user is root. If not, replace /root with the full home path)

That being said, those /etc/profile.d/xx.sh should run.
See codeclimate/docker-alpine-ruby as an example:

COPY files /

With 'files/etc" including an files/etc/profile.d/rubygems.sh running just fine.


In the OP project Dockerfile, there is a

COPY aliases.sh /etc/profile.d/

But the default shell is not a login shell (sh -l), which means profile files (or those in /etc/profile.d) are not sourced.

Adding sh -l would work:

docker@default:~$ docker run --rm --name ruby -it codeclimate/alpine-ruby:b42 sh -l87a58e26b744:/# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/ruby/gems/2.0.0/bin


As mentioned by Jinesh before, the default shell in Alpine Linux is ash

localhost:~$ echo $SHELL/bin/ashlocalhost:~$ 

Therefore simple solution is too add your aliases in .profile. In this case, I put all my aliases in ~/.ash_aliases

localhost:~$ cat .profile # ~/.profile# Aliasif [ -f ~/.ash_aliases ]; then    . ~/.ash_aliasesfilocalhost:~$ 

.ash_aliases file

localhost:~$ cat .ash_aliasesalias a=aliasalias c=clearalias f=filealias g=grepalias l='ls -lh'localhost:~$

And it works :)