I installed Docker CE on a fresh Ubuntu 18.04 VM. When running hello-world, the following error response came up:

docker: Error response from daemon: Get registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).

I thought that's weird because I was able to access various other services on the internet, but Docker was timing out when accessing registry-1.docker.io.

I could ping google.com and yahoo.com, so I knew the network was up. 

But I couldn't ping docker.io.

Oh.

I fixed this by using 8.8.8.8 and 8.8.4.4 as nameservers. 

Up until recently this could be done by editing /etc/resolv.conf, but now there's a new way to do this. It's called netplan and it uses a yaml formatted file to specify, among other things, the nameservers used by a network interface.

So all I did was opened up /etc/netplan/50-cloud-init.yaml and there were the following lines:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        enp0s3:
            dhcp4: true
    version: 2

Then I edited the file so it looked like what you can see below:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        enp0s3:
            dhcp4: true
            nameservers:
                    addresses: [8.8.8.8, 8.8.4.4]
    version: 2

And then all that was needed was to activate the new configuration by running the following command:

sudo netplan apply

That was easy. Everything then worked. The nameservers 8.8.8.8 and 8.8.4.4 were in use and running "docker run hello-world" worked as expected.