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

How can I get /etc/profile to run automatically when starting an Alpine Docker container interactively? I have added some aliases to an 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!

  • Alpine based Docker images fully compatible on Ubuntu host?
  • How to get 'man' working in an Alpine Linux Docker container?
  • Installing ssh-keyscan on Alpine linux?
  • Trying to connect to Ammonite sshd when app is running in docker container
  • Bash script fails to run node on docker image
  • Cannot install uwsgi on Alpine
  • 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/
    export PATH=$PATH:/usr/lib/ruby/gems/2.0.0/bin
    / # env
    no_proxy=*.local, 169.254/16
    / # exit

    Although the /etc/profile.d/ 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!

  • How to stream multiple logs from docker container using log driver
  • Is it possible to run Telegraf in Docker (and get accurate metrics)?
  • Docker host & no space left on device
  • Host port with DB to Docker container
  • Passing bash code through the environment (for docker-compose)
  • How can I reduce the size of Docker images
  • 3 Solutions collect form web for “How to get /etc/profile to run automatically in Alpine / Docker”

    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/ should run.
    See codeclimate/docker-alpine-ruby as an example:

    COPY files /

    With ‘files/etc” including an files/etc/profile.d/ running just fine.

    In the OP project Dockerfile, there is a

    COPY /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 -l
    87a58e26b744:/# echo $PATH

    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.5
    ENV 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.048kB
    Step 1/3 : FROM alpine:3.5
    3.5: Pulling from library/alpine
    627beaf3eaaf: Pull complete
    Digest: sha256:58e1a1bb75db1b5a24a462dd5e2915277ea06438c3f105138f97eb53149673c4
    Status: Downloaded newer image for alpine:3.5
     ---> 4a415e366388
    Step 2/3 : ENV ENV "/root/.ashrc"
     ---> Running in a9b6ff7303c2
     ---> 8d4af0b7839d
    Removing intermediate container a9b6ff7303c2
    Step 3/3 : RUN echo "echo 'Hello, world!'" > "$ENV"
     ---> Running in 57c2fd3353f3
     ---> 2cee6e034546
    Removing intermediate container 57c2fd3353f3
    Successfully built 2cee6e034546

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

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

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

    So to answer your query, replace

    ENV ENV="/root/.ashrc"


    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.


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

    localhost:~$ echo $SHELL

    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
    # Alias
    if [ -f ~/.ash_aliases ]; then
        . ~/.ash_aliases

    .ash_aliases file

    localhost:~$ cat .ash_aliases
    alias a=alias
    alias c=clear
    alias f=file
    alias g=grep
    alias l='ls -lh'

    And it works 🙂

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.