docker-compose3中网络通信

不同docker-compose中容器之间网络通信

A docker-compose文件中创建网络,并启动

version: "3"
services:
  nginx001:
    container_name: nginx001  #容器名称
    domainname: nginx001  #设置容器的域名。当容器需要与其他容器或主机进行通信时,可以使用域名来进行寻址。
    hostname: nginx001 #参数用来设置容器的主机名。主机名是容器在网络中的唯一标识符,可以用来识别和定位容器。
    image: nginx:1.24 # 镜像
    restart: always # 重启策略
    ports:
      - "8000:80"
    networks:  # 使用网络
      mynet:   # 使用自定义网络名称

networks:  #定义网络
  mynet:   # compose中网络名称
    name: mynet  # 实际docker中网络名称 docker network ls 查看的名称
    driver: bridge  # 网络名称

B docker-compose文件使用A中创建网络,并启动

version: "3"
services:
  nginx002:
    container_name: nginx002
    domainname: nginx002
    hostname: nginx002
    image: nginx:1.24
    restart: always
    ports:
      - "8001:80"
    networks:
      - mynet  #服务使用指定创建好的网络

networks: # 定义网络
  mynet:  # 网络名称,表示使用A中docker-compos中已创建好的docke网络名称
    external: true  # true表示使用A中docker-compos中已创建好的docke网络,没有则启动报错.

注意

  • 2个compose的yaml文件,不要放在一起,不然提示警告.
    • WARN[0000] Found orphan containers ([nginx001]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.

参考

posted on   Colin88  阅读(135)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示