Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use network in docker

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Docker network how to use, in response to this problem, this article details the corresponding analysis and solution, hoping to help more want to solve this problem of small partners to find a simpler and easier way.

1 Mapping host ports

Mapping the host port, that is, mapping the port provided by the docker container to a port of the host, and consumers can directly access the host.

docker run --name docker-nginx -d -p 8080:80 -v /root/service/nginx/nginx.conf:/etc/nginx/nginx.conf -v /root/service/nginx/conf.d:/etc/nginx/conf.d -v /root/service/nginx/html:/usr/share/nginx/html nginx

When using the above command to run the container, port 80 of the container docker-nginx is mapped to port 8080 of the host, where-p 8080:80 is the mapping relationship of the specified port. Consumers can access port 8080 of the host directly. In addition, port 8080 of the host firewall must be opened. (The firewall setup method has been introduced in the previous article)

However, in most of Docker Hub's commonly used image instance descriptions, this method is basically not used to run a container, because in the public network, opening an extra port means that the host has an extra vulnerability in the network world. The service on my mainframe has been attacked because the firewall has not closed the port. Therefore, it is necessary to use docker network to form a network between containers.

2 docker network Build a network between containers

In docker, docker network is mainly used for communication between containers, that is, to form a local area network between containers. In fact, using-link can achieve a simple network between containers, but when there are many containers and the communication relationship is more complex, using network is more organized.

Docker network is to form a local area network between containers, and then each container can join this network, and then the access between containers is like the access between hosts in the local area network.

Create a networker docker network create docker-network // docker-network is the name of the local area network, customize docker network ls //View existing networks Connect containers to networks//Run new containers and join them to the docker-network network/ --network indicates the network to which this container is connected//-network-alias indicates the name of this container in this network, You can also use--ip to specify the container's ipdocker run --name docker-nginx -d --network docker-network --alias nginx-network nginx//already running containers join the network using the following command// docker network connect [OPTIONS] NETWORK CONTAINERdocker network connect --alias nginx-network docker-nginxdocker network connect --alias wp-network docker-network docker-wordpress access between containers

With the above command, we have created a network called docker-network, and two containers have joined this network, named nginx-network and wp-network in the network. Now you need to use nginx as a reverse proxy to access wordpress (wp-network), you can directly modify the nginx configuration file, set the proxy host address to wp-network,(wp-network is the name of the container in the network) as follows:

location / { proxy_pass http://wp-network:80/;}//Note: Port 80 here is the port exposed by wordpress mirror (i.e. the port exposed in DockerFile),//is not mapped to the host port. (In fact, there is no need to map the port) About the docker network how to use the answer to the question to share here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology