50·Docker Compose进阶

Compose 网络

Compose 网络

默认网络

# Compose 自动创建默认网络
# 服务名 = DNS 名
services:
  web:
    image: nginx
  app:
    image: my-app
    # app 中可以直接用 http://web 访问 nginx

自定义网络

services:
  web:
    image: nginx
    networks:
      - frontend

  app:
    image: my-app
    networks:
      - frontend
      - backend

  db:
    image: mysql
    networks:
      - backend

networks:
  frontend:
  backend:

网络隔离

web ←→ app ←→ db
web ✗→ db(不在同一网络)

小结

功能说明
默认网络自动创建,所有服务共享
自定义网络显式定义,可隔离
服务名 DNS服务间用服务名通信

练习编辑器

bash
Loading...