Docker exec linux terminal create alias

≯℡__Kan透↙ 提交于 2020-02-05 08:31:10

问题


I have a running and detached container. I want to create a command alias there before attaching to that container.

When I am attached to the container and I type:

alias bar='foo'

an alias is created, and might be checked by:

alias

command.

but if I want to do the same by docker exec command ie this way

docker exec -it <container-name> /bin/bash -c "alias bar='foo'"

it does not work, probably because when I'm attached to the container and type into its terminal

/bin/bash -c "alias bar='foo'"

it does not work as well.

Do you know how to modify alias bar='foo' so it works together with docker exec command applied to a detached container?


回答1:


The alias built-in creates an alias in the current shell. Aliases, like environment variables, are not persisted, only loaded. You need to update your .bashrc or whatever inside the container to have the desired alias so that it can be loaded on each start of bash.




回答2:


Alias in your bashrc file does not directly accept parameters. Although in your case you will have to create a function and alias that.

You can add following on your ~/.bashrc

dexec() {
    docker exec -it "$1" /bin/bash
    echo "$1"
}

and don't forget to do

source ~/.bashrc

ideally, you can do something like this

dexec <container_id>



回答3:


add into your Dockerfile something like
RUN echo alias bar='foo' >> ~/.bashrc
Actually, if you running your container under a user other than root you need to put this command into the correct .bashrc



来源:https://stackoverflow.com/questions/54297615/docker-exec-linux-terminal-create-alias

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!