'How to load shell aliases in an alpine docker container with start
I have written a DOCKER file, which uses as an image an private adapted alpine image, which contains a nginx server. Note: alpine uses zsh, 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 it’s contents. But the file does not load 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 it’s 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
Solution 1:[1]
For me, this worked:
Create a shell script per alias. For example,
backup.shandcompile.shanddblogin.sh.Put all those scripts inside a directory. For example
scriptsAdd that directory to
PATHenvironment variable inside yourDockerfile. Example:ENV PATH="${PATH}:/temp/scripts"Mount this directory in your
docker-compose.ymlfile. Example:
volumes:
- /path/to/scripts/on/local/machine:/temp/scripts
Make sure the path you add to
PATHmatches the path you mount indocker-compose.yml. For example both should be/temp/scriptsNow inside your docker interactive shell, you can simply call your script files with their names, for example
compileordblogin.
Solution 2:[2]
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
Solution 3:[3]
Cory your file as .bashrc under your user's directory:
COPY docker/shared/bashrc /home/{YOUR_USER}/.bashrc
Solution 4:[4]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saeed Neamati |
| Solution 2 | Ralf |
| Solution 3 | Tania Petsouka |
| Solution 4 | Calamity Jane |
