6

For Docker Containers we use Alpine Linux, like Node:Alpine, to make our images as small as possible. But somethimes you want to login on that Container. It would be convenient to have your aliases in that Container.

When we use standard Linux, we map a .bashrc to /root/.bashrc and have all our aliasses available.

But I cannot find the .bashrc equivalent for Alpine Linix.

I've tried:

  • .bashrc
  • .bash_profile
  • .profile
  • .ash_profile
  • .ashrc

All not working.

Additional information

Alpine Linux is started in a Docker Container.

Dockerfile

FROM node:alpine

ENV NODE_ENV production

WORKDIR /usr/src/app

COPY --from=builder /usr/src/app/build .

Login to the container

I use the following command from the Host (my OSX macbook)

docker exec -it <containerName> /bin/sh

docker-compose.yml

  jobrunner:
    build:
      context: ./build
      dockerfile: Dockerfile
    image: jobrunner:1.0
    volumes:
      - ./deployment/jobs/root/.bashrc:/root/.bashrc
      - ./deployment/jobs/root/.bashrc:/root/.profile
      - ./logs:/usr/src/app/logs
      - ./data:/usr/src/app/data
    environment:
      NODE_ENV: production
    restart: unless-stopped
    container_name: JobHelloWorld
    command: ["node", "./src/job_starter.js"]

Starting the container with:

docker-compose up -d

In the Container

When I enter the container with

docker exec -it <containername> /bin/sh

and then the output of the commands requested in the comments:

/usr/src/app # echo $SHELL

/usr/src/app # ps -p $$ ps: unrecognized option: p BusyBox v1.32.1 () multi-call binary.

Usage: ps [-o COL1,COL2=HEADER] [-T]

Show list of processes

-o COL1,COL2=HEADER Select columns for display
-T          Show threads

/usr/src/app #

ps -ef

I guess the output of 'ps -ef' is also intersting:

/usr/src/app # ps -ef
PID   USER     TIME  COMMAND
    1 root      0:00 sleep 360d
    8 root      0:00 sh
   16 root      0:00 ps -ef

it shows the sh shell (correct?). But the startup of /bin/sh does not work.

ps aux | grep $$

/usr/src/app # ps aux | grep $$
   19 root      0:00 /bin/sh
   26 root      0:00 grep 19

/bin/sh is a link

/usr/src/app # ls -la /bin/sh
lrwxrwxrwx    1 root     root            12 Apr 14 10:25 /bin/sh -> /bin/busybox

Small test changing .profile

/usr/src/app # cat ~/.profile
alias l='ls $LS_OPTIONS -la'

export foo="bar"

/usr/src/app # /usr/src/app # echo $foo

/usr/src/app # /usr/src/app # . ~/.profile Job-/usr/src/app# echo $foo bar Job-/usr/src/app#

As you see, the .profile does work, it just is not executed upon log in onto Alpine Linux in a Docker environment, or Alpine does not execute .profile upon login.

Does anybody know what the startup script is for an interactive session on Alpine Linux?

Or: Does anybody know why Docker does not execute .profile upon login from 'docker exec'.

BertC
  • 211
  • 1
  • 2
  • 8
  • What shell do you run on alpine? You can see your default login shell with echo $SHELL and you can see what you're currently running with ps -p $$. Please [edit] your question and add the output of both commands. – terdon Aug 17 '21 at 08:15
  • Question updated with output of the commands you mentioned. – BertC Aug 17 '21 at 08:22
  • /bin/sh should be a symlink most likely. Where does it point to? – Panki Aug 17 '21 at 08:46
  • /bin/sh points to /bin/busybox Which is correct since busybox is the default shell for Alpine. (Why it does not present itself under the $SHELL env var I don't know). Default startup script for busybox is .profile, but that did not work. – BertC Aug 17 '21 at 08:49
  • Thanks. Sorry about the ps -p, it looks like you are using busybox ps but yes, it does indeed look like you are using a simple sh. Most likely busybox sh as well. Just to be sure, you can check with ps aux | grep $$. You say that ~/.profile didn't work? That suggests you aren't running a login shell. Which is odd. When/how do you log into the container? If you add export foo="bar" to /etc/profile, then log into the container and run echo $foo, do you get bar? – terdon Aug 17 '21 at 08:51
  • 3
    Hang on. do you issue the docker exec -it <containername> /bin/sh manually? Can you change that to docker exec -it <containername> /bin/sh -l? That will launch a login shell which should let you use ~/.profile. – terdon Aug 17 '21 at 08:54
  • Yes!! That was it. You need to use "-l" in the docker exec command. Never saw that before and I don't need that when log in on Ubuntuy containers (then the .bashrc gets executed). Thanks @terdon for your help – BertC Aug 17 '21 at 09:20

3 Answers3

5

Big thanks for @terdon for his time and help.

When log in on Alpine Linux in a Docker Container, you need to use the following command to get the .profile executed:

docker exec -it <containerName> /bin/sh -l

Notice the -l ('l' for Login? :-) ) at the end. That makes the Alpine shell start a login shell which means that it reads /etc/profile and ~/.profile, so I can use those files to define my aliases.

You don't need it for Ubuntu Containers though.

terdon
  • 242,166
BertC
  • 211
  • 1
  • 2
  • 8
4

You can set the environment variable $ENV to the path of a script you want to run on login. This avoids the need to pass -l as an argument to /bin/sh.

Example:

ENV ENV=/root/.ashrc

This sources /root/.ashrc on startup.

Nick ODell
  • 2,608
1

To log in a user in the Alpine Linux Docker container, using their configured login shell, use login -f username.

For example,

docker exec -it mycontainer login -f root

If the user has a password set, and you want to use it, remove -f from the command.

This will cause the user's configured login shell to be used as the login shell, running as a login shell, which means it will read whatever shell initialisation files the shell would ordinarily read when running as a login shell (/etc/zsh/zprofile and ~/.zprofile in the case of zsh, for example).

Kusalananda
  • 333,661