Docker 网络

Docker 进阶

Mike Metzger

Data Engineering Consultant

Docker 网络基础

  • Docker 提供丰富的网络功能
  • 可创建网络以在容器、主机和外部系统间通信
  • 将讲解与网络交互的常用命令
Docker 进阶

Docker 网络类型

  • Docker 通过驱动支持多种网络类型
    • bridge:默认驱动,默认可出站,暴露端口后可入站
    • host:容器与主机完全互通
    • none:将容器与网络隔离
    • 还有更多类型,包含自定义驱动
  • 我们主要使用 bridge 驱动创建自定义网络
Docker 进阶

管理 Docker 网络

常用命令:

  • docker network
    • docker network <command>
    • docker network <command> --help
    • docker network ls 列出主机上的所有网络
    • docker network create 创建网络
    • docker network rm 删除网络
Docker 进阶

Docker 网络示例

  • 创建名为 mynetwork 的 Docker 网络
    repl@host:~$ docker network create mynetwork
    
5ff0febab98f73b74dd753eb44a30f7d7291052b3b1d58b0134589221cb8e33d
repl@host:~$ docker network ls

NETWORK ID NAME DRIVER SCOPE 2edc5ae4838c bridge bridge local a92988382711 host host local 5ff0febab98f mynetwork bridge local 5464ed866dad none null local
Docker 进阶

将容器接入网络

  • 如何将容器连接到网络?
  • docker run --network <networkname> ...
  • docker run --network mynetwork ubuntu bash
  • 也可事后连接容器
    • docker network connect <networkname> <container>
    • docker network connect mynetwork ubuntu-B
Docker 进阶

docker network inspect

  • 如何查看网络详情?
  • docker network inspect <networkname>
  • 提供配置信息和容器分配的 IP 地址
    repl@host:~$ docker network inspect mynetwork
    
Docker 进阶

docker network inspect 示例

        "Name": "mynetwork",
        ...
        "Driver": "bridge",
        ...
        Containers": {    "2be08aa942029191350d4bceb8816254af8713dd6f7dcbadcab8f068f7dbfdfa": {
                "Name": "unruffled_kare",
                "EndpointID": "29739356ae200e1e901d2eabef05efaca0fb37e1a4e1a4c3bf369f2892ca6c4b",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
Docker 进阶

让我们练习一下!

Docker 进阶

Preparing Video For Download...