Docker Tips — Accessing Local Network

Docker containerizes the networking stack from the host computer by default. However, there are times when you require the image/container to be able to access local network resources.

One way to allow docker to access local network resources (e.g. a locally hosted apt repository) is with the --network=host parameter. This can be used with both docker build and docker run.

$ docker build --network=host
$ docker run --network=host

From the Docker documentation:

The host network adds a container on the hosts network stack. You’ll find the network configuration inside the container is identical to the host.

There might also be some security concerns with this, so it should only be used when necessary.

From older Docker Documentation (though I can’t find a direct link):

— network=host
Tells Docker to skip placing the container inside of a separate network stack. In essence, this choice tells Docker to not containerize the container’s networking! While container processes will still be confined to their own filesystem and process list and resource limits, a quick ip addr command will show you that, network-wise, they live “outside” in the main Docker host and have full access to its network interfaces. Note that this does not let the container reconfigure the host network stack — that would require — privileged=true — but it does let container processes open low-numbered ports like any other root process. It also allows the container to access local network services like D-bus. This can lead to processes in the container being able to do unexpected things like restart your computer. You should use this option with caution.