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
5
Answers
This seems to work:
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.
Cory your file as .bashrc under your user’s directory:
COPY docker/shared/bashrc /home/{YOUR_USER}/.bashrc
You can add aliases during the docker build like so:
For me, this worked:
Create a shell script per alias. For example,
backup.sh
andcompile.sh
anddblogin.sh
.Put all those scripts inside a directory. For example
scripts
Add that directory to
PATH
environment variable inside yourDockerfile
. Example:ENV PATH="${PATH}:/temp/scripts"
Mount this directory in your
docker-compose.yml
file. Example:Make sure the path you add to
PATH
matches the path you mount indocker-compose.yml
. For example both should be/temp/scripts
Now inside your docker interactive shell, you can simply call your script files with their names, for example
compile
ordblogin
.By default docker start a non-login shell.
To read .profile file you need a login shell
docker exec -it <container> ash -l
.To read /etc/profile (or another startup file) everytime, you’ve to set ENV variable.
Example Dockerfile