¿Cómo establecer alias bash para contenedores docker en Dockerfile?
Soy nuevo en docker. Encontré que podemos establecer variables de entorno usando la instrucción ENV en el Dockerfile. Pero, ¿cómo se establecen los alias de bash para los comandos largos en Dockerfile?
6 answers
Básicamente como siempre lo haces, agregándolo al .bashrc
del usuario:
FROM foo
RUN echo 'alias hi="echo hello"' >> ~/.bashrc
Como de costumbre, esto solo funcionará para shells interactivos:
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
Para shells no interactivos debes crear un pequeño script y ponerlo en tu camino, es decir:
RUN echo -e '#!/bin/bash\necho hello' > /usr/bin/hi && \
chmod +x /usr/bin/hi
Si su alias usa parámetros (ie. hi Jim
-> hello Jim
), basta con añadir "$@"
:
RUN echo -e '#!/bin/bash\necho hello "$@"' > /usr/bin/hi && \
chmod +x /usr/bin/hi
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-25 15:24:01
Para crear un alias de un comando existente, también puede usar ln -s
:
ln -s $(which <existing_command>) /usr/bin/<my_command>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-11 18:47:10
Acabo de agregar esto a mi aplicación.dockerfile
# setup 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"
Y dentro de la initbash_profile.sh
que acaba de añadir mis alias personalizados y no hay necesidad de fuente de la .archivo bashrc.
# add the bash aliases
cat /usr/sbin/bashrc_alias.sh >> ~/.bashrc
Funcionó un placer!
Otra opción es usar el "comando docker exec-it" desde fuera del contenedor y usar el suyo propio .bashrc o .bash_profile (lo que prefieras)
Eg. docker exec -it docker_app_1 bash
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-19 02:09:42
Puede usar entrypoint, pero no funcionará para alias, en su Dockerfile:
ADD dev/entrypoint.sh /opt/entrypoint.sh
ENTRYPOINT ["/opt/entrypoint.sh"]
Su entrypoint.sh
#!/bin/bash
set -e
function dev_run()
{
}
export -f dev_run
exec "$@"
(Copiar/pegar rápido, lo siento)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-29 10:36:49
- edita este archivo ~/.bash_aliases
vi ~/.bash_aliases
- fuente de este archivo~/.bash_aliases
source ~/.bash_aliases
- hecho.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-04 13:32:15
Si desea usar alias solo en Dockerfile, pero no dentro del contenedor, entonces el camino más corto es ENV
declaración:
ENV update='apt-get update -qq'
ENV install='apt-get install -qq'
RUN $update && $install apt-utils \
curl \
gnupg \
python3.6
Y para su uso en contenedor de la manera ya descrita:
RUN printf '#!/bin/bash \n $(which apt-get) install -qq $@' > /usr/bin/install
RUN chmod +x /usr/bin/install
La mayoría de las veces uso alias solo en la etapa de construcción y no entro en contenedores, por lo que el primer ejemplo es más rápido, más claro y más simple para el uso diario.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-17 22:20:35