How to .bashrc for root in Docker

烂漫一生 提交于 2020-01-31 21:33:48

问题


I want to give my root user in a (centos:6) Docker container a .bashrc. However, when I run my container, I find that the .bashrc has not been sourced. Can this be done?

My build command:

...
RUN touch .bashrc
RUN echo "iptables -t nat -A OUTPUT -d hostA -p tcp --dport 3306 -j DNAT --to hostB" >> .bashrc
...

My run command:

docker run -it --cap-add=NET_ADMIN myImage /bin/bash

回答1:


Turns out I was adding the file incorrectly. It should be /root/.bashrc rather than just .bashrc. With the file added in the correct place, no run command or CMD is required.

Build

...
ADD iptables /iptables
RUN touch /root/.bashrc \
 && cat iptables >> /root/.bashrc
...

Run

docker run -it --cap-add=NET_ADMIN myImage /bin/bash



回答2:


The bash manpage states that .bashrc is read when the shell is interactive. Thus, if you want a bash that reads .bashrc, you need to launch bash with -i.

See that:

root@host:~# echo 'echo this is .bashrc' > /tmp/bashrc
root@host:~# docker run -ti -v /tmp/bashrc:/root/.bashrc debian bash -i 
this is .bashrc
root@01da3a7e9594:/#

But, executing bash -i like this in the container, overrides the entrypoint or cmd, so you might be better with wrapping the iptables command and the entrypoint you are originally using in a shell script that becomes your entrypoint / cmd.



来源:https://stackoverflow.com/questions/37284667/how-to-bashrc-for-root-in-docker

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