154

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?

0

9 Answers 9

270

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
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for this can you provide an example of non interactive shell? I was looking for ENTRYPOINT and CMD to solve this
Bash alias only works in interactive shells. Just create a small shell script and put in your path, same effect as an alias. Updated the answer with a simple example.
Note: from stackoverflow.com/a/8467449 you should rather use printf instead of echo to ensure more consistent behavior. So printf '#!/bin/bash\necho hello' > /usr/bin/hi chmod +x /usr/bin/hi
You should use "$@" instead of $* for multiple arguments escaping. See unix.stackexchange.com/questions/248464/…
For me -e didn't work in case of non-interactive shells. Removing the same worked like a charm.
|
27

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

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

1 Comment

I wouldn't call this an alias .. it is a soft link / symlink. The convenience of aliases is that you can pass parameters to the command like alias ll='ls -lh' which is not possible with symlinks.
9

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

Comments

8

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.

Comments

5

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

Comments

2

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

Comments

1

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 "$@"

1 Comment

Pay attention that some Docker image could already override default entrypoint (such as phusion base image).
0

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.

3 Comments

Every RUN instruction is executed in a new shell, so in your example the alias file is no longer loaded when you try to use the alias.
100% @ErikDannenberg thank you for the comment and also for reminding me that I need to post an update to this - I've uncovered a solution and it's very much like you suggested.
What is "ponged"? Not "(Britain, Australia, New Zealand, slang) To stink, to smell bad.", I presume. Or "(slang, theater, derogatory) To deliver a line of a play in an arch, suggestive or unnatural way, so as to draw undue attention to it." or "(slang, theater, intransitive) To invent a line of dialogue when one has forgotten the actual line. "?
0

@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.