RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1154020
Accepted
icYFTL
icYFTL
Asked:2020-07-17 20:14:35 +0000 UTC2020-07-17 20:14:35 +0000 UTC 2020-07-17 20:14:35 +0000 UTC

码头工人网络

  • 772

docker bridge实际上,我希望我的容器在创建的容器和服务器上都可用localhost。

version: "3"
services:
  server:
    image: zalgo
    build: .
    command: python3 ZalgoBot.py
    ports:
      - 8000:8000
    networks:
      - zalgo_net
    restart: always
    depends_on:
      - gpl
    container_name: zalgo

  gpl:
    image: gpl
    build: GPL
    ports:
      - 7865:7865
    command: python3 GPL.py
    restart: always
    networks:
      zalgo_net:
        ipv4_address: 192.168.127.3
    container_name: gpl
networks:
  zalgo_net:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.127.0/24

zalgo_net需要服务之间的数据交换,localhost我需要访问服务器才能通过代理直接nginx暴露服务zalgo。

它在网络中很弱,所以我不能说它是否bridge正常工作。我很高兴对网络发表任何评论。

docker-compose
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Senior Pomidor
    2020-07-17T23:10:18Z2020-07-17T23:10:18Z

    在docker-compose 版本 3中,当您加注时,构图会自动bridge-network为您的作曲加注。您的服务将开始按名称相互查看。

    您也不需要container_name,因为它会阻止您进行链接。
    如果你想提升 2 个基本实例或 api 怎么办?没门。

    这是一个例子:

    version: '3.7'
    
    services:
      app1:
        image: alpine
        command: ping -c 100 app2
      app2:
        image: alpine
        command: ping -c 100 app1
      app3:
        image: alpine
        command: ping -c 100 app2
        depends_on:
          - app2
    

    我们执行docker-compose up

    ✗ docker-compose up
    Creating network "app_default" with the default driver
    Creating app_app1_1 ... done
    Creating app_app2_1 ... done
    Creating app_app3_1 ... done
    Attaching to app_app1_1, app_app2_1, app_app3_1
    app2_1  | PING app1 (172.21.0.2): 56 data bytes
    app2_1  | 64 bytes from 172.21.0.2: seq=0 ttl=64 time=0.117 ms
    app1_1  | PING app2 (172.21.0.3): 56 data bytes
    app1_1  | 64 bytes from 172.21.0.3: seq=0 ttl=64 time=0.090 ms
    app3_1  | PING app2 (172.21.0.3): 56 data bytes
    app3_1  | 64 bytes from 172.21.0.3: seq=0 ttl=64 time=0.267 ms
    app1_1  | 64 bytes from 172.21.0.3: seq=1 ttl=64 time=0.209 ms
    app2_1  | 64 bytes from 172.21.0.2: seq=1 ttl=64 time=0.567 ms
    app3_1  | 64 bytes from 172.21.0.3: seq=1 ttl=64 time=0.259 ms
    app1_1  | 64 bytes from 172.21.0.3: seq=2 ttl=64 time=0.243 ms
    app2_1  | 64 bytes from 172.21.0.2: seq=2 ttl=64 time=0.179 ms
    app3_1  | 64 bytes from 172.21.0.3: seq=2 ttl=64 time=0.369 ms
    app1_1  | 64 bytes from 172.21.0.3: seq=3 ttl=64 time=0.179 ms
    app2_1  | 64 bytes from 172.21.0.2: seq=3 ttl=64 time=0.144 ms
    app3_1  | 64 bytes from 172.21.0.3: seq=3 ttl=64 time=0.141 ms
    app1_1  | 64 bytes from 172.21.0.3: seq=4 ttl=64 time=0.245 ms
    app2_1  | 64 bytes from 172.21.0.2: seq=4 ttl=64 time=0.214 ms
    app3_1  | 64 bytes from 172.21.0.3: seq=4 ttl=64 time=0.129 ms
    app1_1  | 64 bytes from 172.21.0.3: seq=5 ttl=64 time=0.188 ms
    app2_1  | 64 bytes from 172.21.0.2: seq=5 ttl=64 time=0.189 ms
    app3_1  | 64 bytes from 172.21.0.3: seq=5 ttl=64 time=0.137 ms
    app1_1  | 64 bytes from 172.21.0.3: seq=6 ttl=64 time=0.395 ms
    app2_1  | 64 bytes from 172.21.0.2: seq=6 ttl=64 time=0.143 ms
    app3_1  | 64 bytes from 172.21.0.3: seq=6 ttl=64 time=0.219 ms
    app1_1  | 64 bytes from 172.21.0.3: seq=7 ttl=64 time=0.133 ms
    app2_1  | 64 bytes from 172.21.0.2: seq=7 ttl=64 time=0.188 ms
    

    在组合中,存在对彼此容器的 ping。app1和app2在启动时立即启动,而app3则等待app2启动,因为它是depends_on。这在日志中可见。

    正如我们发现的那样,容器可以相互协作,因为 docker 创建了network. 该名称取自文件夹名称。

    如何从容器访问主机?
    要从容器中引用主机,可以使用特殊别名docker.for.mac.localhost, docker.for.win.localhost。另一种方法是使用host network.

    关于使用此类容器的便利性,您可以docker kill app1使用代替docker-compose,

    docker-compose down- 删除所有网络、容器
    docker-compose up app3- 提升 app3 容器及其所指的所有内容。
    docker-compose stop app
    docker-compose kill app

    • 1

相关问题

  • 从互联网访问容器

  • 如何在容器和外部连接之间建立连接?

  • 如何在adminner中登录数据库

  • 容器停止后詹金斯不保存更改

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5