Home  >  Article  >  Operation and Maintenance  >  Let’s talk about several methods for docker to access the external network

Let’s talk about several methods for docker to access the external network

PHPz
PHPzOriginal
2023-04-04 09:16:5010382browse

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:

  1. Configure http proxy

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/

  1. Configure https proxy

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/

  1. Restart the Docker daemon

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:

  1. Create a new Docker network

Run the following command:

docker network create --subnet=172.18.0.0/16 mynetwork

  1. Run a new container and connect to the network

Run the following command:

docker run -it --name mycontainer --net mynetwork ubuntu:latest /bin/bash

  1. Configure network

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:

  1. Create a new bridge network

Run the following command:

docker network create -d bridge mybridge

  1. Configure the network

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn