3

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
Calamity Jane
  • 2,189
  • 5
  • 36
  • 68

5 Answers5

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.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
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
Ralf
  • 6,735
  • 3
  • 16
  • 32
  • How does that differ from copying the whole file during the build? – Calamity Jane Feb 23 '21 at 13:54
  • 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. – Ralf Feb 23 '21 at 14:10
2

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


apug
  • 36
  • 3
0

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

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

Tania Petsouka
  • 1,388
  • 1
  • 9
  • 20
  • 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 .profile – Calamity Jane Feb 23 '21 at 13:53
  • are you sure about zsh? I do have an alpine container and .bashrc works fine. bash-5.0$ cat /etc/alpine-release 3.12.3 – Tania Petsouka Feb 23 '21 at 14:17
  • @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`. – Ralf Feb 23 '21 at 14:18
  • 3
    BTW, the official plain vanilla Alpine image uses `ash` as its default shell. – Ralf Feb 23 '21 at 14:25
  • 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. – Calamity Jane Feb 23 '21 at 15:13
  • This link says that in fact .profile is the correct file for bourne shell profiles: http://osr507doc.xinuos.com/en/OSUserG/_The_Bourne_shell_profile.html – Calamity Jane Feb 24 '21 at 09:18
  • OP states is using Alpine, Alpine uses ash shell, so this won't work. – DevAntoine Jul 27 '21 at 07:35
-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.

Calamity Jane
  • 2,189
  • 5
  • 36
  • 68