In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you the content of a sample analysis of independent IP access within the Docker container. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction to the experiment
When learning the Docker container, a container of nginx is created and opened in the virtual machine, and then port mapping is used to map port 80 of container to port 80 of the virtual machine, and then access the Nginx service of the Docker container on the host by accessing the IP of the virtual machine.
Then start thinking about whether you can access the Nginx service by assigning a separate IP to the container and then accessing the separate IP on the host machine. Through a variety of exploration, Baidu, and finally realized, the following experiment begins, here is the use of virtual machine NAT mode.
Experimental diagram
Experimental implementation
Install the docker service
[root@promote ~] # yum install docker-y
Turn off the firewall and SElinux
[root@promote ~] # systemctl stop firewalld.service [root@promote ~] # setenforce 0
Enable the docker service
[root@promote ~] # systemctl start docker.service [root@promote ~] # systemctl enable docker.service Created symlink from / etc/systemd/system/multi-user.target.wants/docker.service to / usr/lib/systemd/system/docker.service.# sets the docker service to boot
After starting the docker service, we can search for the nginx image from the server with the command.
Download the highest star nginx image, which, by the way, uses aliyun's docker source configuration.
Download the nginx service image.
[root@promote etc] # docker pull docker.io/nginxUsing default tag: latestTrying to pull repository docker.io/library/nginx... Latest: Pulling from docker.io/library/nginxbe8881be8156: Pull complete 32d9726baeef: Pull complete 87e5e6f71297: Pull complete Digest: sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424Status: Downloaded newer image for docker.io/nginx:latest
Create a custom network in the virtual machine
[root@promote etc] # docker network create-- subnet=172.20.0.0/24 docker-br0f900579310b9e692ab5a2593d9845be24166120a9de1c16e3143fa5a9c875f96# creates a 172.20.0.0 network segment, and the bridge is named docker-br0. These two are based on their own needs.
Select an IP in the custom IP address range as the IP of the container to launch.
[root@promote etc] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/nginx latest c825216765802 weeks ago 109 MB# to view the downloaded image, we will use IMAGE ID [root@promote etc] # docker run-itd-- net docker-br0-- ip 172.20.0.10-- name nginx c82521676580 / bin/basha2da3616efd0c53745fd7b33823733598be749c83cd4a84f72025664837b3a77#i to keep the container open, and t to assign a pseudo terminal to docker D means to open as a daemon daemon #-- net specifies the bridge name,-- ip specifies the startup ip,-- name specifies the service name [root@promote etc] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa2da3616efd0 c82521676580 "/ bin/bash" 4 minutes ago Up 4 minutes 80/tcp nginx# can see that the service has been started
Go into the container and install some tools
[root@promote etc] # docker exec-it nginx / bin/bash#nginx is the name specified above
We use ifconfig at this time.
Root@a2da3616efd0:/# ifconfigbash: ifconfig: command not found# can see that there is no such command, so we need to install the net-tools tool
At first I thought it was useless to install it in yum mode, but later I checked the system command and found that Debian Linux apt-get command was used in the container, so the source should be updated first.
Root@a2da3616efd0:/# apt-get update#update updates the source addresses listed in / etc/apt/sources.list and / etc/apt/sources.list.d to get the latest package information. Get:1 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB] Get:3 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [386 kB] Ign:2 http://cdn-fastly.deb.debian.org/debian stretch InRelease Get:4 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease [91.0 kB] Get:5 http://cdn-fastly.deb. Debian.org/debian stretch Release [118kB] Get:6 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages [5148 B] Get:7 http://cdn-fastly.deb.debian.org/debian stretch Release.gpg [2434 B] Get:8 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages [7099 kB] Fetched 7796 kB in 1min 50s (70.8 kB/s) Reading package lists... Doneroot@a2da3616efd0:/proc# apt-get upgrade# upgrades the installed software package, namely Reading package lists... in update DoneBuilding dependency tree Reading state information... DoneCalculating upgrade... Done0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.root@a2da3616efd0:/proc# apt-get install net-tools # install net-tools package Reading package lists... DoneBuilding dependency tree Reading state information... DoneThe following NEW packages will be installed: net-tools0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.Setting up net-tools (1.60+git20161116.90da8a0-1)...
Check the ip address of the container
Enable the Nginx service
Root@a2da3616efd0:~# nginxroot@a2da3616efd0:~# netstat-ntapActive Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0 LISTEN 232/nginx: master p tcp 0 0 127.0.0.11 servers and established 42541 0.0.0.0 root@a2da3616efd0:~# nginxroot@a2da3616efd0:~# netstat-# nginx service has been enabled System port 80 has also been opened.
We tested whether the default home page of nginx can be accessed with 172.20.0.10 in a virtual machine environment.
Visit 172.20.0.10 in the physical machine win10 to test whether it can be accessed.
The result is that there is no access to nginx, so we try to use win10 to ping172.20.0.10.
Attempt to turn on route forwarding
View the routing tables of container and win10 respectively
At this time, let's visit 172.20.0.10 on win10. Found that it was accessible.
Finally, let's try to turn off route forwarding in the virtual machine.
Finally, a summary.
1. Create a custom network segment
2. Select an IP in a custom IP address range and open the container.
3. Enable routing and forwarding of virtual machines
4. Add the routing entry of the custom network segment to the physical machine routing, and specify the interface as the IP address of the virtual machine.
Thank you for reading! This is the end of the article on "sample Analysis of Independent IP access in Docker Container". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.