How to load shell aliases in an alpine docker container with start

Viewed 3795

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

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

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.

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

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


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

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

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.

Related