How to .bashrc for root in Docker How to .bashrc for root in Docker bash bash

How to .bashrc for root in Docker


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 /iptablesRUN touch /root/.bashrc \ && cat iptables >> /root/.bashrc...

Run

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


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/bashrcroot@host:~# docker run -ti -v /tmp/bashrc:/root/.bashrc debian bash -i this is .bashrcroot@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.