さまざまな Docker コンテナにアクセスする方法

リリース: 2020-04-02 15:53:52
オリジナル
2436 人が閲覧しました

さまざまな Docker コンテナにアクセスする方法

docker 内の異なるコンテナ間のアクセス方法:

仮想 IP アクセス

docker をインストールすると、docker はデフォルトで内部ブリッジ ネットワーク docker0 を作成します。作成された各コンテナには仮想ネットワーク カードが割り当てられ、コンテナは IP に基づいて相互にアクセスできます。

[root@33fcf82ab4dd /]# [root@CentOS ~]# ifconfig
......
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:35ff:feac:66d8  prefixlen 64  scopeid 0x20<link>
        ether 02:42:35:ac:66:d8  txqueuelen 0  (Ethernet)
        RX packets 4018  bytes 266467 (260.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4226  bytes 33935667 (32.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
......
ログイン後にコピー

centos イメージを実行し、IP アドレスを確認して次を取得します: 172.17.0.7

[root@CentOS ~]# docker run -it --name centos-1 docker.io/centos:latest
[root@6d214ff8d70a /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.7  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:7  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:07  txqueuelen 0  (Ethernet)
        RX packets 16  bytes 1296 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
ログイン後にコピー

同じコマンドを使用して別のコンテナを起動し、IP アドレスを確認して次を取得します: 172.17.0.8

[root@CentOS ~]# docker run -it --name centos-2 docker.io/centos:latest
[root@33fcf82ab4dd /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.8  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:8  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:08  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
ログイン後にコピー

コンテナ内の ping テストの結果は次のとおりです:

[root@33fcf82ab4dd /]# ping 172.17.0.7
PING 172.17.0.7 (172.17.0.7) 56(84) bytes of data.
bytes from 172.17.0.7: icmp_seq=1 ttl=64 time=0.205 ms
bytes from 172.17.0.7: icmp_seq=2 ttl=64 time=0.119 ms
bytes from 172.17.0.7: icmp_seq=3 ttl=64 time=0.118 ms
bytes from 172.17.0.7: icmp_seq=4 ttl=64 time=0.101 ms
ログイン後にコピー

ブリッジ ネットワークの作成

1. docker をインストールした後、次のコマンドを実行してブリッジ ネットワークを作成します。 docker network create testnet

新しく作成されたブリッジ テストネットをクエリします。

さまざまな Docker コンテナにアクセスする方法

#2. コンテナーを実行し、テストネット ネットワークに接続します。

使用法: docker run -it --name <コンテナ名> ---network --network-alias <ネットワーク エイリアス> <イメージ名>

[root@CentOS ~]# docker run -it --name centos-1 --network testnet --network-alias centos-1 docker.io/centos:latest
[root@CentOS ~]# docker run -it --name centos-2 --network testnet --network-alias centos-2 docker.io/centos:latest
ログイン後にコピー

3. あるコンテナから別のコンテナに ping を実行します。テスト結果は次のとおりです:

[root@fafe2622f2af /]# ping centos-1
PING centos-1 (172.20.0.2) 56(84) bytes of data.
bytes from centos-1.testnet (172.20.0.2): icmp_seq=1 ttl=64 time=0.158 ms
bytes from centos-1.testnet (172.20.0.2): icmp_seq=2 ttl=64 time=0.108 ms
bytes from centos-1.testnet (172.20.0.2): icmp_seq=3 ttl=64 time=0.112 ms
bytes from centos-1.testnet (172.20.0.2): icmp_seq=4 ttl=64 time=0.113 ms
ログイン後にコピー

その他の関連チュートリアルについては、PHP 中国語 Web サイトの dockertutorial 列に注目してください。

以上がさまざまな Docker コンテナにアクセスする方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!