Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 368
np20 Avatar asked Apr 03 '16 17:04

np20


People also ask

How do I specify a container name in Dockerfile?

The Dockerfile is for creating images not containers. You can now give names to your containers using the new --name flag for docker run . If --name is not provided Docker will automatically generate an alphanumeric string for the container name.

Can we run shell script in Dockerfile?

Like you may need to execute multiple commands at start point of container which is not easy to do that. But fortunetly , you can add shell script file in Entrypoint and it will get executed. Here is the example how you can run shell script from file on Entrypoint in dockerfile.


1 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 
like image 159
Erik Dannenberg Avatar answered Sep 21 '22 06:09

Erik Dannenberg