How can I set Bash aliases for docker containers in Dockerfile?

BashUnixDockerAliasDockerfile

Bash Problem Overview


I am new to Docker. I found that we can set environment variables using the ENV instruction in the Dockerfile. But how does one set Bash aliases for long commands in Dockerfile?

Bash Solutions


Solution 1 - Bash

Basically like you always do, by adding it to the user's .bashrc file:

FROM foo
RUN echo 'alias hi="echo hello"' >> ~/.bashrc

As usual this will only work for interactive shells:

docker build -t test .
docker run -it --rm --entrypoint /bin/bash test hi
/bin/bash: hi: No such file or directory
docker run -it --rm test bash
$ hi
hello

For non-interactive shells you should create a small script and put it in your path, i.e.:

RUN echo -e '#!/bin/bash\necho hello' > /usr/bin/hi && \
    chmod +x /usr/bin/hi

If your alias uses parameters (ie. hi Jim -> hello Jim), just add "$@":

RUN echo -e '#!/bin/bash\necho hello "$@"' > /usr/bin/hi && \
    chmod +x /usr/bin/hi

Solution 2 - Bash

To create an alias of an existing command, might also use ln -s:

ln -s $(which <existing_command>) /usr/bin/<my_command>

Solution 3 - Bash

If you want to use aliases just in Dockerfile, but not inside a container then the shortest way is the ENV declaration:

ENV update='apt-get update -qq'
ENV install='apt-get install -qq'

RUN $update && $install apt-utils \
    curl \
    gnupg \
    python3.6

And for use in a container the way like already described:

 RUN printf '#!/bin/bash \n $(which apt-get) install -qq $@' > /usr/bin/install
 RUN chmod +x /usr/bin/install

Most of the time I use aliases just in the building stage and do not go inside containers, so the first example is quicker, clearer and simpler for every day use.

Solution 4 - Bash

I just added this to my app.dockerfile file:

# Set up aliases
ADD ./bashrc_alias.sh /usr/sbin/bashrc_alias.sh
ADD ./initbash_profile.sh /usr/sbin/initbash_profile
RUN chmod +x /usr/sbin/initbash_profile
RUN /bin/bash -C "/usr/sbin/initbash_profile"

And inside the initbash_profile.sh file which just appends my custom aliases and no need to source the .bashrc file:

# Add the Bash aliases
cat /usr/sbin/bashrc_alias.sh >> ~/.bashrc

It worked a treat!

Another option is to just use the "docker exec -it <container-name> command" from outside the container and just use your own .bashrc or the .bash_profile file (what ever you prefer).

E.g.,

docker exec -it docker_app_1 bash

Solution 5 - Bash

You can use ENTRYPOINT, but it will not work for aliases, in your Dockerfile:

ADD dev/entrypoint.sh /opt/entrypoint.sh
ENTRYPOINT ["/opt/entrypoint.sh"]

Your entrypoint.sh file:

#!/bin/bash
set -e

function dev_run()
{

}

export -f dev_run

exec "$@"

Solution 6 - Bash

I think the easiest way would be to mount a file into your container containing your aliases, and then specify where Bash should find it:

docker run \
    -it \
    --rm \
    -v ~/.bash_aliases:/tmp/.bash_aliases \
    [image] \
    /bin/bash --init-file /tmp/.bash_aliases

Sample usage:

echo 'alias what="echo it works"' > my_aliases
docker run -it --rm -v ~/my_aliases:/tmp/my_aliases ubuntu:18.04 /bin/bash --init-file /tmp/my_aliases
alias

Output:

alias what='echo it works'
what

Output:

it works

Solution 7 - Bash

Used some of the previous solutions, but the aliases are not recognised still.

I'm trying to set aliases and use them both within later Dockerfile steps and in the container at runtime.

RUN echo "alias model-downloader='python3 ${MODEL_DL_PATH}/downloader.py'" >> ~/.bash_aliases && \
    echo "alias model-converter='python3 ${MODEL_DL_PATH}/converter.py'" >> ~/.bash_aliases && \
    source ~/.bash_aliases

# Download the model
RUN model-downloader --name $MODEL_NAME -o $MODEL_DIR --precisions $MODEL_PRECISION;

The solution for me was to use ENV variables that held folder paths and then add the exact executable. I could have use ARG too, but for more of my scenarios I needed the aliases in both the build stage and later in the runtime.

I used the ENV variables in conjunction with a Bash script that runs once dependencies have ponged and sets the Bash source, sets some more env variables, and allows for further commands to pipe through.

Solution 8 - Bash

Here is a Bash function to have your aliases in every container you use interactively.

ducker_it() {
    docker cp ~/bin/alias.sh "$1":/tmp
    docker exec -it "$1" /bin/bash -c "[[ ! -f /tmp/alias.sh.done ]] \
        && [[ -w /root/.bashrc ]] \
        && cat /tmp/alias.sh >> /root/.bashrc \
        && touch /tmp/alias.sh.done"
    docker exec -it "$1" /bin/bash
}

Required step before:

grep ^alias ~/.zshrc > ~/bin/alias.sh

Solution 9 - Bash

@ErikDannenberg's answer did the trick, but in my case, some adjustments were needed.

It didn't work with aliases cause apparently there's an issue with interactive shells.

I reached for his second solution, but it still didn't really work. I checked existing shell scripts in my project and noticed the head comment (first line = #!/usr/bin/env sh) differs a bit from #!/usr/bin/bash. After changing it accordingly it started working for my t and tc "aliases", but I had to use the addendum to his second solution for getting tf to work.

Here's the complete Dockerfile

FROM php:8.1.1-fpm-alpine AS build

RUN apk update && apk add git

RUN curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer

RUN apk add --no-cache $PHPIZE_DEPS \
    && pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && touch /usr/local/etc/php/conf.d/99-xdebug.ini \
    && echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/99-xdebug.ini \
    && echo -e '#!/usr/bin/env sh\nphp artisan test' > /usr/bin/t \
    && chmod +x /usr/bin/t \
    && echo -e '#!/usr/bin/env sh\nphp artisan test --coverage' > /usr/bin/tc \
    && chmod +x /usr/bin/tc \
    && echo -e '#!/usr/bin/env sh\nphp artisan test --filter "$@"' > /usr/bin/tf \
    && chmod +x /usr/bin/tf

WORKDIR /var/www

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnp20View Question on Stackoverflow
Solution 1 - BashErik DannenbergView Answer on Stackoverflow
Solution 2 - BashLaurent MagninView Answer on Stackoverflow
Solution 3 - BashSoniqueView Answer on Stackoverflow
Solution 4 - BashmikoopView Answer on Stackoverflow
Solution 5 - BashThomas DecauxView Answer on Stackoverflow
Solution 6 - BashGillespieView Answer on Stackoverflow
Solution 7 - BashalanionitaView Answer on Stackoverflow
Solution 8 - BashDjAlanView Answer on Stackoverflow
Solution 9 - Bashs3cView Answer on Stackoverflow