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?
9 Answers
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

- 30,738
- 21
- 105
- 131

- 5,716
- 2
- 16
- 21
-
Thanks for this can you provide an example of non interactive shell? I was looking for ENTRYPOINT and CMD to solve this – c24b Jul 13 '16 at 13:38
-
5Bash 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. – Erik Dannenberg Jul 13 '16 at 15:25
-
2Note: from https://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 – barraq Aug 30 '17 at 16:47
-
3You should use "$@" instead of $* for multiple arguments escaping. See https://unix.stackexchange.com/questions/248464/escape-shell-arg-from-one-script-to-another – gnou Feb 25 '18 at 14:04
-
How to put that script inside PATH? – Saeed Neamati Jul 05 '21 at 17:35
-
Execute `echo $PATH` in your container, copy your script into one of the listed directories. – Erik Dannenberg Jul 06 '21 at 19:42
-
2For me `-e` didn't work in case of non-interactive shells. Removing the same worked like a charm. – Prasannjeet Singh Jan 23 '22 at 04:03
To create an alias of an existing command, might also use ln -s
:
ln -s $(which <existing_command>) /usr/bin/<my_command>

- 30,738
- 21
- 105
- 131

- 369
- 3
- 4
-
12I 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. – qwertz Jun 27 '19 at 20:12
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.

- 30,738
- 21
- 105
- 131

- 6,670
- 6
- 41
- 60
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

- 30,738
- 21
- 105
- 131

- 1,981
- 1
- 18
- 18
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

- 30,738
- 21
- 105
- 131

- 5,780
- 3
- 32
- 54
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 "$@"

- 30,738
- 21
- 105
- 131

- 21,738
- 2
- 113
- 124
-
Pay attention that some Docker image could already override default entrypoint (such as phusion base image). – Thomas Decaux Jul 29 '16 at 10:25
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

- 71
- 3
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.

- 30,738
- 21
- 105
- 131

- 1,272
- 1
- 16
- 34
-
1Every `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. – Erik Dannenberg Jul 07 '20 at 07:37
-
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. – alanionita Jul 07 '20 at 14:50
-
2What is "ponged"? [Not](https://en.wiktionary.org/wiki/pong#Verb) *"(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. "*? – Peter Mortensen Nov 09 '21 at 03:24
@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

- 1,481
- 19
- 28