How can I get /etc/profile
to run automatically when starting 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? I've also had problems with using a ~/.profile
file. Any insight is appreciated!
EDIT:
Based on VonC's answer, I pulled and ran his example ruby
container. Here is what I got:
$ docker run --rm --name ruby -it codeclimate/alpine-ruby:b42
/ # more /etc/profile.d/rubygems.sh
export PATH=$PATH:/usr/lib/ruby/gems/2.0.0/bin
/ # env
no_proxy=*.local, 169.254/16
HOSTNAME=6c7e93ebc5a1
SHLVL=1
HOME=/root
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
/ # exit
Although the /etc/profile.d/rubygems.sh
file exists, it is not being run when I login and my PATH
environment variable is not being updated. Am I using the wrong docker run
command? Is something else missing? Has anyone gotten ~/.profile
or /etc/profile.d/
files to work with Alpine on Docker? Thanks!
You still can try in your Dockerfile a:
(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:With '
files/etc
" including anfiles/etc/profile.d/rubygems.sh
running just fine.In the OP project
Dockerfile
, there is aBut 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: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 shellsh -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 calledENV
before launching Ash.e.g. in your Dockerfile
When you build that you get:
Finally, when you run the newly generated container, you get:
Notice the Ash shell didn't run as a login shell.
So to answer your query, replace
with:
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
As mentioned by Jinesh before, the default shell in Alpine Linux is ash
Therefore simple solution is too add your aliases in .profile. In this case, I put all my aliases in ~/.ash_aliases
.ash_aliases file
And it works :)