DEV Community

Janice
Janice

Posted on

How to make Docker work in China

While I'm on my holiday in China, I surprisingly found out that Docker was unable to download images from Docker Hub at all, not even behind a VPN. It took me some time to figure out this work-around and I hope it can help you save your time.

Step 1 - Launch a proxy server

Launch an EC2 instance on AWS as a proxy server, make sure the server locates at somewhere that's not restricted from accessing Docker Hub.

For personal uses, t2.micro is enough. Remember to open an SSH port because we'll need to run commands in our instance later.

Step 2 - Install Squid

sudo yum update -y
sudo yum install squid -y
sudo systemctl start squid
sudo systemctl enable squid
Enter fullscreen mode Exit fullscreen mode

Squid is listening on 3128 by default. Open the port 3128 on your EC2 instance so that traffic can be accepted.

Edit your Squid configurations:

sudo vi /etc/squid/squid.conf
Enter fullscreen mode Exit fullscreen mode

Find the line

http_access deny all
Enter fullscreen mode Exit fullscreen mode

and change it to

http_access allow all
Enter fullscreen mode Exit fullscreen mode

but really for security enhancement, you should replace all to IP addresses you want to allow.

Then

sudo systemctl restart squid
Enter fullscreen mode Exit fullscreen mode

Testing if your proxy server works:

curl -x http://new-instance-public-ip:3128 https://whatismyipaddress.com/
Enter fullscreen mode Exit fullscreen mode

Step 3 - Configure in Docker

I'm using a Mac and I have Docker Desktop installed. The steps can vary based on different operating systems and how you installed Docker so make sure you find your own way.

Let Docker know your proxy server by going to Settings - Resources - Proxies and fill in the below three fields:

# Http server
http://new-instance-public-ip:3128
# Https server
http://new-instance-public-ip:3128
# bypass proxy settings for these
registry-1.docker.com,*.docker.com,10.0.0.0/8
Enter fullscreen mode Exit fullscreen mode

Final Step - Testing

Now try downloading images in your Docker to test. Docker should have worked by now!
Remember to stop your EC2 instance when it's no longer used to avoid any unwanted charges. Happy docking!

Top comments (0)