Home > Article > Operation and Maintenance > Let’s talk about several methods for docker to access the external network
Docker is an open source containerization technology that helps developers package applications and dependencies into an independent, portable container, thereby enabling rapid deployment and operation of applications. In actual development, we often need to access external resources, so how can Docker access the external network? This article will introduce you to several methods to access the external network.
1. Set up Docker proxy
Setting up Docker proxy is a common method to access the external network, which can be achieved through the following steps:
Add --proxy=http://proxy-ip:proxy-port/ in the startup parameters of the Docker daemon, where proxy-ip and proxy-port need to be replaced with the actual proxy IP and port number. For example:
sudo dockerd --proxy=http://192.168.1.100:3128/
In the Docker daemon Add --proxy=https://proxy-ip:proxy-port/ to the startup parameters, where proxy-ip and proxy-port need to be replaced with the actual proxy IP and port number. For example:
sudo dockerd --proxy=https://192.168.1.100:3128/
Run the following command:
sudo systemctl daemon-reload
sudo systemctl restart docker
Now Docker can access the external network through the proxy.
2. Use Docker network
When accessing the external network, we can use Docker network to realize the network connection between the container and the host. The specific steps are as follows:
Run the following command:
docker network create --subnet=172.18.0.0/16 mynetwork
Run the following command:
docker run -it --name mycontainer --net mynetwork ubuntu:latest /bin/bash
Inside the Docker container, run the following command:
ip addr add 172.18.0.2/16 dev eth0
ip route add default via 172.18.0.1
The 172.18.0.1 here is the host IP address so that the container can access the external network through the host.
3. Use the bridge network
In addition to using the Docker network, we can also use the bridge network to realize the network connection between the container and the host. The specific steps are as follows:
Run the following command:
docker network create -d bridge mybridge
Run the following command:
docker run -it --name mycontainer --net mybridge ubuntu:latest /bin/bash
ip addr add 172.17.0.2/16 dev eth0
ip route add default via 172.17.0.1
The 172.17.0.1 here is the IP address of the bridge so that the container can access the external network through the bridge.
Summary
This article introduces three methods for Docker to access the external network, namely setting up Docker proxy, using Docker network and using bridge network. Through these methods, developers can flexibly connect the network between the container and the host to achieve access to external resources. In actual development, developers can choose different methods to implement Docker's access to the external network according to specific needs.
The above is the detailed content of Let’s talk about several methods for docker to access the external network. For more information, please follow other related articles on the PHP Chinese website!