溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Docker容器之內網獨立IP訪問的示例分析

發布時間:2021-08-21 10:28:52 來源:億速云 閱讀:197 作者:小新 欄目:服務器

這篇文章給大家分享的是有關Docker容器之內網獨立IP訪問的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

實驗介紹

在學習Docker容器的時候,在虛擬機中創建并開啟了一個nginx的container,然后通過端口映射的方法,即將container的80端口映射到虛擬機的80端口,然后在宿主機上通過訪問虛擬機的IP從而訪問到Docker容器的Nginx服務。

然后開始思考,能否通過分配給container一個獨立的IP,然后在宿主機上訪問這個獨立IP從而訪問Nginx服務。通過各種探索,百度,最終實現,下面開始實驗,這里用的是虛擬機的NAT模式。

實驗圖解

Docker容器之內網獨立IP訪問的示例分析 

實驗實施

安裝docker服務

[root@promote ~]# yum install docker -y

關閉防火墻和SElinux

[root@promote ~]# systemctl stop firewalld.service
[root@promote ~]# setenforce 0

開啟docker服務

[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.
#將docker服務設置為開機啟動

啟動docker服務后,我們可以通過命令從服務端先搜索nginx鏡像。

Docker容器之內網獨立IP訪問的示例分析 

下載星級最高的nginx鏡像,這里順便說一下使用aliyun的docker源配置。

Docker容器之內網獨立IP訪問的示例分析 

下載nginx服務鏡像。

[root@promote etc]# docker pull docker.io/nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ... 
latest: Pulling from docker.io/library/nginx
be8881be8156: Pull complete 
32d9726baeef: Pull complete 
87e5e6f71297: Pull complete 
Digest: sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424
Status: Downloaded newer image for docker.io/nginx:latest

在虛擬機中創建一個自定義網絡

[root@promote etc]# docker network create --subnet=172.20.0.0/24 docker-br0
f900579310b9e692ab5a2593d9845be24166120a9de1c16e3143fa5a9c875f96
#創建一個172.20.0.0網段,網橋的名字命名為docker-br0,這兩個根據自己需要來

Docker容器之內網獨立IP訪問的示例分析

在自定義的網段中選取一個IP作為container的IP來啟動。

[root@promote etc]# docker images
REPOSITORY     TAG         IMAGE ID      CREATED       SIZE
docker.io/nginx   latest       c82521676580    2 weeks ago     109 MB
#查看我們下載下來的鏡像,下面會用到IMAGE ID
[root@promote etc]# docker run -itd --net docker-br0 --ip 172.20.0.10 --name nginx c82521676580 /bin/bash
a2da3616efd0c53745fd7b33823733598be749c83cd4a84f72025664837b3a77
#i表示保持容器打開,t表示給docker分配一個偽終端,d表示以daemon守護進程的方式開啟
#--net指定網橋名稱,--ip指定啟動ip ,--name指定服務名稱
[root@promote etc]# docker ps -a
CONTAINER ID    IMAGE        COMMAND       CREATED       STATUS       PORTS        NAMES
a2da3616efd0    c82521676580    "/bin/bash"     4 minutes ago    Up 4 minutes    80/tcp       nginx
#可以看到服務已經啟動

進入到容器中安裝一些工具

[root@promote etc]# docker exec -it nginx /bin/bash
#nginx就是上面指定的name

這個時候我們使用ifconfig

root@a2da3616efd0:/# ifconfig
bash: ifconfig: command not found
#可以看到并沒有這個命令,所以我們需要安裝net-tools工具

一開始我以為使用yum方式安裝,發現沒用, 后來查看系統命令發現,容器里面使用的是Debian Linux的apt-get命令,首先要更新源。

root@a2da3616efd0:/# apt-get update
#update 是更新 /etc/apt/sources.list 和 /etc/apt/sources.list.d列出的源地址,獲取最新軟件包信息。
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 [118 kB]
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... Done

root@a2da3616efd0:/proc# apt-get upgrade
#升級已經安裝的軟件包即update中的
Reading package lists... Done
Building dependency tree    
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

root@a2da3616efd0:/proc# apt-get install net-tools 
#安裝net-tools軟件包
Reading package lists... Done
Building dependency tree    
Reading state information... Done
The following NEW packages will be installed:
 net-tools
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Setting up net-tools (1.60+git20161116.90da8a0-1) ...

查看容器的ip地址

Docker容器之內網獨立IP訪問的示例分析 

開啟Nginx服務

root@a2da3616efd0:~# nginx
root@a2da3616efd0:~# netstat -ntap
Active 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:80       0.0.0.0:*        LISTEN   232/nginx: master p 
tcp    0   0 127.0.0.11:42541    0.0.0.0:*        LISTEN   - 
#nginx服務已經開啟,系統80端口也已經打開。

我們在虛擬機環境下,用172.20.0.10測試是否能訪問nginx默認首頁。

Docker容器之內網獨立IP訪問的示例分析

到物理機win10中訪問172.20.0.10,測試是否能訪問

Docker容器之內網獨立IP訪問的示例分析

結果就是并不能訪問nginx,我們嘗試使用win10來ping172.20.0.10。

Docker容器之內網獨立IP訪問的示例分析 

嘗試開啟路由轉發

Docker容器之內網獨立IP訪問的示例分析 

Docker容器之內網獨立IP訪問的示例分析 

分別查看container和win10的路由表

Docker容器之內網獨立IP訪問的示例分析 

Docker容器之內網獨立IP訪問的示例分析 

Docker容器之內網獨立IP訪問的示例分析 

Docker容器之內網獨立IP訪問的示例分析

這時候我們再來在win10上訪問172.20.0.10.發現可以訪問了。

Docker容器之內網獨立IP訪問的示例分析 

Docker容器之內網獨立IP訪問的示例分析

最后我們將虛擬機中的路由轉發關閉試試

Docker容器之內網獨立IP訪問的示例分析 

Docker容器之內網獨立IP訪問的示例分析 

最后總結

1、創建自定義網段
2、選一個自定義網段內的IP,開啟容器
3、開啟虛擬機的路由轉發
4、在物理機路由中添加自定義網段的路由條目,指定接口為虛擬機的IP地址

感謝各位的閱讀!關于“Docker容器之內網獨立IP訪問的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女