👨‍💻

docker network 備忘録

2023/12/13に公開

概要

Docker Networkで時々躓くので備忘録を残す

ケース別備忘録

Network Driverについて

下記参照すればまるわかりです
Https://Qiita.Com/Tsutomunakamura/Items/Ed046ee21caca4a2ffd9

エラー「networks with user configured subnets」

下記のコマンドでnetworkを作成した後に、docker-compose.ymlでコンテナに静的IPアドレスを付けたくなった場合

docker network create -d bridge sample_network

①networkを削除

docker network rm sample_network

②subnetを指定して再作成

docker network create --subnet=192.168.92.0/24 sample_network

network設定をdocker-compose.ymlで定義

①シングル構成

docker-compose.yml
version: "3"

services:
  container:
    networks:
      networkA:
        ipv4_address: 192.168.82.1
  #その他の設定

networks:
  networkA:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: 192.168.82.0/24

②マルチ構成 - externalで接続

[コンテナA]docker-compose.yml
version: "3"

services:
  containerA:
    container_name: containerA
    networks:
      networkA:
        ipv4_address: 192.168.82.1
  #その他の設定

networks:
  networkA:
    driver: bridge
    name: networkA
    ipam:
      driver: default
      config:
      - subnet: 192.168.82.0/24
[コンテナB]docker-compose.yml
version: "3"

services:
  containerB:
    container_name: containerB
    networks:
      networkA:
        ipv4_address: 192.168.82.2
  #その他の設定

networks:
  networkA:
    external: true
疎通確認
共通
docker compose exec containerA apt-get update
docker compose exec containerA apt-get install iputils-ping
containerA
docker compose exec containerA ping containerB
---
PING containerB (192.168.82.200) 56(84) bytes of data.
64 bytes from containerB.networkA (192.168.82.200): icmp_seq=1 ttl=64 time=0.226 ms
64 bytes from containerB.networkA (192.168.82.200): icmp_seq=2 ttl=64 time=0.254 ms
64 bytes from containerB.networkA (192.168.82.200): icmp_seq=3 ttl=64 time=0.184 ms
---
containerB
docker compose exec containerB ping containerA
---
PING containerA (192.168.82.100) 56(84) bytes of data.
64 bytes from containerA.networkA (192.168.82.100): icmp_seq=1 ttl=64 time=0.737 ms
64 bytes from containerA.networkA (192.168.82.100): icmp_seq=2 ttl=64 time=0.176 ms
64 bytes from containerA.networkA (192.168.82.100): icmp_seq=3 ttl=64 time=0.109 ms
---

疎通確認&名前解決に問題なさそうです。

ネットワーク関連コマンド

ネットワーク一覧

docker network ls

ネットワークの情報表示

docker network inspect ${network_name}

Discussion