How to load shell aliases in an Alpine docker container with start
Asked Answered
I

8

6

I have written a Dockerfile which uses as an image an private adapted Alpine image, which contains a nginx server. Note: Alpine uses sh, not Bash.

I love to have some shell aliases available, when working in the container and it drives me nuts when they are missing. So I copy a small prepared file to /root/.profile, which works. I can view the file and its contents. But the file does not load automatically; only if I manually do . ~/.profile in the container then I have the aliases available.

What do I have to do, that my profile is automatically loaded after I started the container and connect into its shell?

FROM myprivatealpineimage/base-image-php:7.4.13

ARG TIMEZONE

COPY ./docker/shared/bashrc /root/.profile

COPY ./docker/shared/ /tmp/scripts/
RUN chmod +x -R /tmp/scripts/ \
    && /tmp/scripts/set_timezone.sh ${TIMEZONE}\
    && apk update\
    && apk add --no-cache git

RUN install-ext pecl/apcu pecl/imagick pecl/zip pecl/redis
RUN apk add --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php

WORKDIR /var/www
Indian answered 23/2, 2021 at 12:34 Comment(7)
Found out it is no zsh, but sh, which is used here.Indian
Were you able to solve this at last?Sewing
See my answer below. Can you confirm if this works for you?Indian
I ended up adding to PATH in Dockerfile and call the scripts in that path by name.Sewing
Can you please post your solution?Indian
Posted, thank you.Sewing
This question is similar to: How to get /etc/profile to run automatically in Alpine / Docker. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.Elbring
F
5

By default Docker starts a non-login shell. To read .profile file you need to add an -l option to start a login shell, like docker exec -it <container> ash -l

To read /etc/profile (or another startup file) every time, you have to set the ENV variable.

Example Dockerfile:

ARG PHPVERSION=7.4
FROM php:$PHPVERSION-fpm-alpine

ARG PHPVERSION=7.4
ENV PHPVERSION_ENV=$PHPVERSION

# copy composer from official image
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

#set $ENV
ENV ENV=/etc/profile
#copy aliases definition
COPY /assets/alias.sh /etc/profile.d/alias.sh
Felicita answered 11/2, 2022 at 14:32 Comment(2)
What is -l flag?Acinaciform
@AH.Pooladvand, -l flag activates a login shell mode.Clarissa
S
3

For me, this worked:

  1. Create a shell script per alias. For example, backup.sh and compile.sh and dblogin.sh.

  2. Put all those scripts inside a directory. For example scripts

  3. Add that directory to PATH environment variable inside your Dockerfile. Example:

    ENV PATH="${PATH}:/temp/scripts"

  4. Mount this directory in your docker-compose.yml file. Example:

        volumes: 
            - /path/to/scripts/on/local/machine:/temp/scripts
  1. Make sure the path you add to PATH matches the path you mount in docker-compose.yml. For example both should be /temp/scripts

  2. Now inside your docker interactive shell, you can simply call your script files with their names, for example compile or dblogin.

Sewing answered 7/7, 2021 at 7:40 Comment(0)
D
1

You can add aliases during the docker build like so:

# Making our command line life a little easier...
RUN echo 'alias ll="ls -l"' >> ~/.bashrc
RUN echo 'alias la="ls -la"' >> ~/.bashrc
Divide answered 23/2, 2021 at 13:8 Comment(3)
How does that differ from copying the whole file during the build?Indian
IMHO it is more "what you see is what you get". And .bashrc is executed for all shells, not just login shells like .profile. So the difference is like this you end up with a .bashrc file in your home, not with a .profile file. Using ~ is also independent of the run user. You can do the same when copying the file, of course.Divide
This doesn't help at all if they are not using Bash.Elbring
T
1

Most of the suggestions are possible and working. If you look at a standard Docker image, for example the Nginx image (not Alpine!), you can set a default environment for all users.

  • /etc/profile: The default environment for all users
  • /etc/profile.d: local customizations to add to the OS default

To add your own environment create (or COPY in the Dockerfile) a file with your custom environment variables and COPY it to /etc/profile.d/

Dockerfile:

COPY ./customenv.sh /etc/profile.d

When starting or executing Docker use this command

docker exec -it <docker container> /bin/bash -l
docker exec -it <docker container> /bin/ash -l

Alpine use /bin/ash, but all examples above apply the same.

Torse answered 11/7 at 8:20 Comment(2)
This is confused on multiple accounts. Like the question already tells you, Alpine does not run Bash at all, so that part of the answer is basically irrelevant. Also, the discussion of /etc/profile vs /etc/profile.d is vaguely misleading. "Custom profiles"? No, you can customize the default, but that's not what I would understand "custom" to mean in this context (it sounds more like you could create a separate profile for a particular user, maybe?)Elbring
True, i adjusted the text a bit to remove confusing texts. I was commenting on the comments more then the original question as most of the answer, although they work, dont take in account the, already available, default setup in alpine (using /etc/enviroment /etc/profile and /etc/profile.d.Torse
S
1

Add following to the Dockerfile:

RUN echo 'alias ll="ls -alh"' >> /etc/bash/bashrc

I've verified this approach works for me.

Spaniel answered 10/9 at 8:4 Comment(1)
Do you work with Alpine? AFAIK Alpine doesn't use the bash.Indian
D
0

Cory your file as .bashrc under your user's directory:

COPY docker/shared/bashrc /home/{YOUR_USER}/.bashrc

Directoire answered 23/2, 2021 at 13:4 Comment(7)
That is what I am doing. In the docker container I am root. Alpine uses zsh, not bash therefore I copy not to .bashrc but to .profileIndian
are you sure about zsh? I do have an alpine container and .bashrc works fine. bash-5.0$ cat /etc/alpine-release 3.12.3Directoire
@CalamityJane, if you use zsh then bash configuration files like .bashrc and .profile won't help. Either you need to switch to use bash, or easier, define the alias in .zshrc.Divide
BTW, the official plain vanilla Alpine image uses ash as its default shell.Divide
Quite sure it is not bash, because I discussed it with colleagues. Not so sure if it is ash or zsh though. I will check that out.Indian
This link says that in fact .profile is the correct file for bourne shell profiles: osr507doc.xinuos.com/en/OSUserG/_The_Bourne_shell_profile.htmlIndian
OP states is using Alpine, Alpine uses ash shell, so this won't work.Dozen
S
0

For the people using alpine and root user when sshing, simply add your alias like this in your dockerfile:

RUN echo "alias ll='ls -l'" >> /root/.profile

Simply adding it to the /root/.profile will do the trick. No need to source it aftewards, or have any interactive shell when running docker.

Sorilda answered 27/12, 2023 at 20:43 Comment(0)
I
-1

This seems to work:

FROM myprivatealpineimage/base-image-php:8.0.3

ARG TIMEZONE
ARG WITH_XDEBUG=false

COPY ./docker/shared/bashrc /root/.profile
COPY ./docker/shared/bashrc /root/.ashrc

COPY ./docker/shared/ /tmp/scripts/
RUN chmod +x -R /tmp/scripts/ \
    && /tmp/scripts/set_timezone.sh ${TIMEZONE} \
    && apk add --no-cache git  

WORKDIR /var/www

I just build my container anew and when I logged in, I had my aliases. If somebody can confirm, that this setup works I will mark it as correct answer. No clue though why it works and the other didn't.

Indian answered 7/7, 2021 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.