罗田县升平网络工作室,一家专业从事网站建设的工作室

资讯论坛

 找回密码
 加入论坛

快捷登录

回帖中禁止出现的内容,违者将被直接永久禁止访问,删除ID处理 :1.违反法律法规 ,包括但不限于出现带有政治、色情、暴恐信息等内容;2.恶意攻击内容,包括但不限于:恶意攻击党和政府、辱骂跟帖者、攻击主题发布者、不服从论坛管理、挑衅管理者、挑战版规等;3.广告、推广内容,尤其出现带有病毒、恶意代码、广告链接等内容,包括但不限于:QQ号、文字QQ号、微信号、手机号、文字手机号、第三方网址、单位公司名称、网站名称等;4.回帖贴出该主题隐藏资源链接或其它主题隐藏资源链接的行为。
查看: 314|回复: 0

docker内服务访问宿主机服务的实现

[复制链接]

647

主题

663

帖子

773

积分

社区达人

积分
773
发表于 2021-11-14 06:01:07 来自手机 | 显示全部楼层 |阅读模式
目录


  • 1. 场景
  • 2. 解决
  • 3. 总结
  • 4. 参考

1. 场景


使用windows, wsl2 进行日常开发测试工作。 但是wsl2经常会遇到网络问题。比如今天在测试一个项目,核心功能是将postgres 的数据使用开源组件synch 同步到clickhouse 这个工作。
测试所需组件
       
  • postgres   
  • kafka   
  • zookeeper   
  • redis   
  • synch容器
最开始测试时,选择的方案是, 将上述五个服务使用 docker-compose 进行编排, network_modules使用hosts模式, 因为考虑到kafka的监听安全机制,这种网络模式,无需单独指定暴露端口。
docker-compose.yaml 文件如下
  1. version: "3" services:  postgres:    image: failymao/postgres:12.7    container_name: postgres    restart: unless-stopped    privileged: true                                                      # 设置docker-compose env 文件    command: [ "-c", "config_file=/var/lib/postgresql/postgresql.conf", "-c", "hba_file=/var/lib/postgresql/pg_hba.conf" ]    volumes:      - ./config/postgresql.conf:/var/lib/postgresql/postgresql.conf      - ./config/pg_hba.conf:/var/lib/postgresql/pg_hba.conf    environment:      POSTGRES_PASSWORD: abc123      POSTGRES_USER: postgres      POSTGRES_PORT: 15432      POSTGRES_HOST: 127.0.0.1    healthcheck:      test: sh -c "sleep 5 && PGPASSWORD=abc123 psql -h 127.0.0.1 -U postgres -p 15432 -c '\q';"      interval: 30s      timeout: 10s      retries: 3    network_mode: "host"   zookeeper:    image: failymao/zookeeper:1.4.0    container_name: zookeeper    restart: always    network_mode: "host"   kafka:    image: failymao/kafka:1.4.0    container_name: kafka    restart: always    depends_on:      - zookeeper    environment:      KAFKA_ADVERTISED_HOST_NAME: kafka      KAFKA_ZOOKEEPER_CONNECT: localhost:2181      KAFKA_LISTENERS: PLAINTEXT://127.0.0.1:9092      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9092      KAFKA_BROKER_ID: 1      KAFKA_LOG_RETENTION_HOURS: 24      KAFKA_LOG_DIRS: /data/kafka-data  #数据挂载    network_mode: "host"   producer:    depends_on:      - redis      - kafka      - zookeeper    image: long2ice/synch    container_name: producer    command: sh -c "      sleep 30 &&      synch --alias pg2ch_test produce"    volumes:      - ./synch.yaml:/synch/synch.yaml    network_mode: "host"   # 一个消费者消费一个数据库  consumer:    tty: true    depends_on:      - redis      - kafka      - zookeeper    image: long2ice/synch    container_name: consumer    command: sh -c      "sleep 30 &&      synch --alias pg2ch_test consume --schema pg2ch_test"    volumes:      - ./synch.yaml:/synch/synch.yaml    network_mode: "host"   redis:    hostname: redis    container_name: redis    image: redis:latest    volumes:      - redis:/data    network_mode: "host" volumes:  redis:  kafka:  zookeeper:
复制代码
测试过程中因为要使用 postgres, wal2json组件,在容器里单独安装组件很麻烦, 尝试了几次均已失败而告终,所以后来选择了将 postgres 服务安装在宿主机上, 容器里面的synch服务 使用宿主机的 ip,port端口。
但是当重新启动服务后,synch服务一直启动不起来, 日志显示 postgres 无法连接. synch配置文件如下
  1. core:  debug: true # when set True, will display sql information.  insert_num: 20000 # how many num to submit,recommend set 20000 when production  insert_interval: 60 # how many seconds to submit,recommend set 60 when production  # enable this will auto create database `synch` in ClickHouse and insert monitor data  monitoring: true redis:  host: redis  port: 6379  db: 0  password:  prefix: synch  sentinel: false # enable redis sentinel  sentinel_hosts: # redis sentinel hosts    - 127.0.0.1:5000  sentinel_master: master  queue_max_len: 200000 # stream max len, will delete redundant ones with FIFO source_dbs:  - db_type: postgres    alias: pg2ch_test    broker_type: kafka # current support redis and kafka    host: 127.0.0.1    port: 5433    user: postgres    password: abc123    databases:      - database: pg2ch_test        auto_create: true        tables:          - table: pgbench_accounts            auto_full_etl: true            clickhouse_engine: CollapsingMergeTree            sign_column: sign            version_column:            partition_by:            settings: clickhouse:  # shard hosts when cluster, will insert by random  hosts:    - 127.0.0.1:9000  user: default  password: ''  cluster_name:  # enable cluster mode when not empty, and hosts must be more than one if enable.  distributed_suffix: _all # distributed tables suffix, available in cluster kafka:  servers:    - 127.0.0.1:9092  topic_prefix: synch
复制代码
这种情况很奇怪,首先确认 postgres, 启动,且监听端口(此处是5433) 也正常,使用localhost和主机eth0网卡地址均报错。

2. 解决


google 答案,参考 stackoverflow 高赞回答,问题解决,原答案如下
If you are using Docker-for-mac or Docker-for-Windows 18.03+, just connect to your mysql service using the host host.docker.internal (instead of the 127.0.0.1 in your connection string).
If you are using Docker-for-Linux 20.10.0+, you can also use the host host.docker.internal if you started your Docker
container with the --add-host host.docker.internal:host-gateway option.
Otherwise, read below
Use** --network="host" **in your docker run command, then 127.0.0.1 in your docker container will point to your docker host.
更多详情见 源贴
host 模式下 容器内服务访问宿主机服务
将postgres监听地址修改如下 host.docker.internal 报错解决。 查看宿主机 /etc/hosts 文件如下
  1. root@failymao-NC:/mnt/d/pythonProject/pg_2_ch_demo# cat /etc/hosts# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:# [network]# generateHosts = false127.0.0.1       localhost 10.111.130.24    host.docker.internal
复制代码
可以看到,宿主机 ip跟域名的映射. 通过访问域名,解析到宿主机ip, 访问宿主机服务。
最终启动 synch 服务配置如下
  1. core:  debug: true # when set True, will display sql information.  insert_num: 20000 # how many num to submit,recommend set 20000 when production  insert_interval: 60 # how many seconds to submit,recommend set 60 when production  # enable this will auto create database `synch` in ClickHouse and insert monitor data  monitoring: true redis:  host: redis  port: 6379  db: 0  password:  prefix: synch  sentinel: false # enable redis sentinel  sentinel_hosts: # redis sentinel hosts    - 127.0.0.1:5000  sentinel_master: master  queue_max_len: 200000 # stream max len, will delete redundant ones with FIFO source_dbs:  - db_type: postgres    alias: pg2ch_test    broker_type: kafka # current support redis and kafka    host: host.docker.internal    port: 5433    user: postgres    password: abc123    databases:      - database: pg2ch_test        auto_create: true        tables:          - table: pgbench_accounts            auto_full_etl: true            clickhouse_engine: CollapsingMergeTree            sign_column: sign            version_column:            partition_by:            settings: clickhouse:  # shard hosts when cluster, will insert by random  hosts:    - 127.0.0.1:9000  user: default  password: ''  cluster_name:  # enable cluster mode when not empty, and hosts must be more than one if enable.  distributed_suffix: _all # distributed tables suffix, available in cluster kafka:  servers:    - 127.0.0.1:9092  topic_prefix: synch    host: host.docker.internalcore:  debug: true # when set True, will display sql information.  insert_num: 20000 # how many num to submit,recommend set 20000 when production  insert_interval: 60 # how many seconds to submit,recommend set 60 when production  # enable this will auto create database `synch` in ClickHouse and insert monitor data  monitoring: true redis:  host: redis  port: 6379  db: 0  password:  prefix: synch  sentinel: false # enable redis sentinel  sentinel_hosts: # redis sentinel hosts    - 127.0.0.1:5000  sentinel_master: master  queue_max_len: 200000 # stream max len, will delete redundant ones with FIFO source_dbs:  - db_type: postgres    alias: pg2ch_test    broker_type: kafka # current support redis and kafka    host:     port: 5433    user: postgres    password: abc123    databases:      - database: pg2ch_test        auto_create: true        tables:          - table: pgbench_accounts            auto_full_etl: true            clickhouse_engine: CollapsingMergeTree            sign_column: sign            version_column:            partition_by:            settings: clickhouse:  # shard hosts when cluster, will insert by random  hosts:    - 127.0.0.1:9000  user: default  password: ''  cluster_name:  # enable cluster mode when not empty, and hosts must be more than one if enable.  distributed_suffix: _all # distributed tables suffix, available in cluster kafka:  servers:    - 127.0.0.1:9092  topic_prefix: synch
复制代码
3. 总结

以--networks="host" 模式下启动容器时,如果想在容器内访问宿主机上的服务, 将ip修改为`host.docker.internal`


4. 参考


https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
到此这篇关于docker内服务访问宿主机服务的实现的文章就介绍到这了,更多相关docker访问宿主机内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:http://www.jb51.net/article/225866.htm
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
打赏鼓励一下!
回复

使用道具 举报

回帖中禁止出现的内容,违者将被直接永久禁止访问,删除ID处理 :1.违反法律法规 ,包括但不限于出现带有政治、色情、暴恐信息等内容;2.恶意攻击内容,包括但不限于:恶意攻击党和政府、辱骂跟帖者、攻击主题发布者、不服从论坛管理、挑衅管理者、挑战版规等;3.广告、推广内容,尤其出现带有病毒、恶意代码、广告链接等内容,包括但不限于:QQ号、文字QQ号、微信号、手机号、文字手机号、第三方网址、单位公司名称、网站名称等;4.回帖贴出该主题隐藏资源链接或其它主题隐藏资源链接的行为。

浏览排行

(38463)2019-11-5 公共云钱包资金盘骗局揭秘: 网络传销+原始股骗局合体!

(22233)2019-12-20 12月17日 邓智天法院直播庭审疑问全解答!

(20722)2019-12-1 环保币GEC资金盘骗局最新消息: 即将崩盘!

(17244)2019-11-9 巨胸肥臀大长腿,嫩模糯美子真人COS不知火舞福利污图

(15868)2018-12-24 罗田县人民法院公布【第五批失信被执行人名单】 ...

(14972)2019-11-3 曝光!PTFX已经崩盘跑路,投资者血流成河!

(13018)2019-8-7 湖北电力网上缴费,支付宝绑定户号的初始密码是什么?

(12480)2018-10-17 罗田县人民政府“12345”市民服务热线服务指南

(11170)2019-12-11 公安定性了, 趣码是非法传销! 趣码怎么退回365元?

(11081)2019-12-15 满足你对女同事的幻想 风骚秘书阿朱销魂眼神勾魂摄魄

最新发表

[升平网络工作室]2025-8-23 [2025-08-23]罗田天气预报

[升平网络工作室]2025-8-23 西藏自治区成立60周年庆祝大会隆重举行 习近平出席大会

[升平网络工作室]2025-8-23 县委委员会召开查摆问题整改整治情况汇报会

[爱查小程序]2025-8-22 [爱查]在线听音乐操作说明

[升平网络工作室]2025-8-22 [2025-08-22]罗田天气预报

[升平网络工作室]2025-8-22 习近平率中央代表团抵达拉萨出席西藏自治区成立60周年庆祝活动

[升平网络工作室]2025-8-22 县关工委联合经济开发区开展“情系学子”助学活动 助力职工子女圆梦大学

[升平网络工作室]2025-8-21 2025年罗田县卫健系统赴高校公开招聘事业单位工作人员拟聘用人员公示公告

[升平网络工作室]2025-8-21 [2025-08-21]罗田天气预报

[升平网络工作室]2025-8-21 县安防委2025年度第三次全体(扩大)会召开

QQ|Archiver|手机版|小黑屋|资讯论坛BBS.SPW8.CN ( 鄂ICP备2021011341号-3 )|网站地图


手机扫一扫继续访问
[免责声明]
本站系本网编辑转载,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。
如涉及作品内容、版权和其它问题,请在30日内与本网联系,我们将在第一时间删除内容!
[声明]本站文章版权归原作者所有 内容为作者个人观点 本站只提供参考并不构成任何投资及应用建议。

进入社区 | 发表新帖 | 百度收录 |
技术提供:罗田县升平网络工作室
站长Email:admin@spw8.cn
投诉电话(刮开查看):15374567400

GMT+8, 2025-8-23 19:24 , Processed in 0.239115 second(s), 29 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表