My general question is: How can I restrict my containers from being able to access the internet (ping apple.com
wouldn’t work) but still be able to access other containers on my private network via their hostname (ping container_a
would still work).
My setup follows as such: I have an Orange Pi Zero running three docker containers, lets say container_a, container_b, and container_c. I have a user defined bridge network set up and all containers are connected to it.
I have removed internet access from my OPiZ by emptying the /etc/resolv.conf file.
I then noticed that my containers can still resolve, e.g. ping apple.com
does work in my container.
Each of my containers /etc/resolv.conf file looks like this by default:
nameserver 127.0.0.11
options edns0 trust-ad ndots:0
As an effort to restrict internet access I commented out the lines in the /etc/resolv.conf file on container startup.
After doing this I noticed that my container_b couldn’t communicate with container_a via hostname anymore. e.g. ping container_a
no longer worked.
I had the thought that I could add
container_a <container ip>
to container_b’s /etc/hosts file and that would allow it to access container_a via hostname and obviously, this way container_b doesn’t have to reach out to the network to see if any container has the hostname “container_a” because it will be in the /etc/hosts file.
I ran this command to get the IP of container_a: sudo docker container inspect <container name> | jq -r '.[0].NetworkSettings.Networks.<network name>.IPAddress'
(substitute the values in the <> with real values)
Anyways, this worked and I was able to ping container_a
but I wonder if there is a “better” or “correct” way to do this.
What I would like to have happen is I can still reach outward to my private network so I don’t have to add an entry to each containers /etc/hosts file and the docker network can help me connect to the other containers via hostname but I want to prevent my containers from being able to access the internet.
How can I accomplish this?