Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access internet in docker build?

First of all, I start docker with the command:

docker --iptables=false -d

When I want to start my docker container, I start it like this

docker run --net=host myImageName

Use the host of net mode can make me access internet from the docker container.

But when I write dockerFile, I can't make it start in host mode.

So how can I access internet in the docker build.

In addition, I must run docker in the mode --iptables=false, because I don't have nat table in iptables. And I don't want it.(Reason link)

So in this mode, how can I access internet in the dockFile build.

like image 941
yunfan Avatar asked Sep 26 '22 02:09

yunfan


1 Answers

  1. Set DEFAULT_FORWARD_POLICY="ACCEPT" @ /etc/default/ufw
  2. Add the following lines JUST BEFORE “*filter” @ /etc/ufw/before.rules

*nat

:POSTROUTING ACCEPT [0:0]

-A POSTROUTING ! -o docker0 -s 172.17.0.0/16 -j MASQUERADE

COMMIT

I, as many people, had to add --iptables=trueto make sure my UFW rules were respected by Docker. But all the articles seem leave out out an important thing that is... the time you reboot your machine, a series of configurations done when you enabled that setting on-the-fly and rebooted the Docker Engine will be lost. The problem? Your containers will no longer connect to the internetz.

Eventually I found out an article with a "full solution". You can find it here and it basically tells you what I wrote at the beginning of the answer.

Hope it helps

like image 170
andreftavares Avatar answered Sep 30 '22 07:09

andreftavares