Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: August 19, 2025
When working with Docker, we usually expect Docker containers to have internet access, especially if the host is connected to the Internet. However, it’s common for developers to encounter an issue where a Docker container has no internet. As a consequence, our efforts to download packages, ping external services, or even access APIs become significantly hindered.
In this tutorial, we’ll break down what to do if our Docker container has no internet. To that end, we’ll discuss how Docker handles networking, as well as common causes of the issue in question, and how to troubleshoot and resolve them.
When we run a container, Docker sets up an internal network that allows the container to access the Internet.
Docker uses a virtual bridge network, typically docker0, to connect containers to the host and the Internet. When a container starts, it’s attached to this virtual bridge and assigned a private IP address. The container sends its network traffic through the bridge, much like devices connect to a home router. Docker rewrites this traffic using a technique called Network Address Translation (NAT) and applies special rules via iptables to route the traffic out through the host’s network connection to reach the Internet.
The setup above usually works seamlessly but can break due to misconfigurations, firewall rules, DNS issues, or problems with network forwarding.
Now, let’s discuss a few reasons that may cause the a Docker container to have no internet:
Now that we’ve seen some common causes, let’s discuss how to fix the issue.
Let’s explore several methods to resolve the issue, depending on the root cause.
If a container can ping an IP address but not a domain, the issue is almost always DNS misconfiguration.
A container may manage to reach external APIs, but fail to resolve domain names to IP addresses such as dns.google. To demonstrate the issue, let’s start a container and explicitly give it an invalid DNS server:
$ docker run --rm --dns 192.0.2.1 alpine ping -c 3 dns.google
ping: bad address 'dns.google'
Let’s break down what happens:
To verify that Internet access itself isn’t broken, we can ping a public IP address directly:
$ docker run --rm --dns 192.0.2.1 alpine ping -c 3 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=254 time=24.889 ms
64 bytes from 8.8.8.8: seq=1 ttl=254 time=27.273 ms
64 bytes from 8.8.8.8: seq=2 ttl=254 time=108.262 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 24.889/53.474/108.262 ms
Successful pings prove the container can reach the Internet. This also means it’s the DNS that’s broken.
To fix the issue, let’s provide a working DNS server such as Google DNS (8.8.8.8):
$ docker run --rm --dns 8.8.8.8 alpine ping -c 3 dns.google
PING dns.google (8.8.4.4): 56 data bytes
64 bytes from 8.8.4.4: seq=0 ttl=254 time=21.428 ms
64 bytes from 8.8.4.4: seq=1 ttl=254 time=22.814 ms
64 bytes from 8.8.4.4: seq=2 ttl=254 time=45.347 ms
--- dns.google ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 21.428/29.863/45.347 ms
Instead of specifying a DNS server every time we run a container, we can configure it globally so that all containers can use it by default. Our approach depends on how we’ve installed Docker.
For Docker Desktop on Ubuntu or other Linux distributions, open Docker Desktop, navigate to Settings, and then to Docker Engine. Here, we can add the DNS configuration to the JSON:
{
"dns": ["8.8.8.8", "8.8.4.4"]
}
If other settings already exist, we can add the dns field alongside them. For Docker installed via a package manager (non-Desktop), we can create or edit the /etc/docker/daemon.json file to add the dns field.
After the addition, we can restart Docker for the changes to take effect.
Now, all containers launched on our system use the specified DNS servers by default, resolving DNS-related connectivity issues automatically.
Docker relies on IP forwarding to enable containers to send network traffic through the host machine and reach external networks, such as the Internet. If IP forwarding is disabled on the host, containers may appear to have no internet, even though Docker’s bridge and DNS settings seem correct.
Let’s ensure we’ve enabled IP forwarding on the host:
$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0
If the value above is 0, then IP forwarding is disabled, and we need to enable it:
$ sudo sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
To ensure the addition persists across reboots, we need to edit the /etc/sysctl.conf file:
# Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=1
In the file, we need to locate the line above and set the variable value to 1.
If DNS and IP forwarding are working but our container still has no internet, the issue may stem from a misconfigured default bridge network. While this isn’t a root-cause fix, we can often bypass the error by creating and using a custom bridge network.
Docker doesn’t allow us to remove or reconfigure the default bridge network. To work around this, we can easily create a custom bridge network. The custom bridge network also behaves similarly to the default one, but is under our control.
To create the network, let’s use custom-net to make a fresh and functioning network configuration:
$ docker network create --driver bridge custom-net
cbcf808fc5ff2d50b9f8c909d738498d7b7c4e73ed30d32499ee6491d4651145
Once the network is up, we can run our container using the new network:
$ docker run --rm --network custom-net alpine ping google.com
If the approach above works and we see successful ping responses, the default bridge was likely the issue.
Notably, we can use this approach when we’ve ruled out DNS, firewall, or IP forwarding issues, but the container still can’t access the Internet.
Additionally, we can clean up unused containers or networks to ensure they’re not the reason for misconfiguring the Docker network state. We can clean them up with the help of the docker system prune command:
$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- unused build cache
Are you sure you want to continue? [y/N]
This ensures that Docker can create a fresh and healthy network.
Finally, let’s discuss a few helpful best practices:
Now, we can prevent or mitigate future Internet issues in containers.
In this article, we explored several approaches to follow if a Docker container has no internet.
A Docker container with no internet connectivity can hinder development. The issue can be as simple as a DNS misconfiguration or as complicated as routing and firewall rules on the host. By understanding how Docker handles networking and carefully troubleshooting step by step, we can restore Internet connectivity to our containers.