dockeralpine-linux

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


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

Solution

  • 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