最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

docker內(nèi)服務(wù)訪問宿主機(jī)服務(wù)的實(shí)現(xiàn)

 更新時(shí)間:2021年10月21日 08:29:55   作者:failymao  
本文主要介紹了docker內(nèi)服務(wù)訪問宿主機(jī)服務(wù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1. 場(chǎng)景

使用windows, wsl2 進(jìn)行日常開發(fā)測(cè)試工作。 但是wsl2經(jīng)常會(huì)遇到網(wǎng)絡(luò)問題。比如今天在測(cè)試一個(gè)項(xiàng)目,核心功能是將postgres 的數(shù)據(jù)使用開源組件synch 同步到clickhouse 這個(gè)工作。

測(cè)試所需組件

  1. postgres
  2. kafka
  3. zookeeper
  4. redis
  5. synch容器

最開始測(cè)試時(shí),選擇的方案是, 將上述五個(gè)服務(wù)使用 docker-compose 進(jìn)行編排, network_modules使用hosts模式, 因?yàn)榭紤]到kafka的監(jiān)聽安全機(jī)制,這種網(wǎng)絡(luò)模式,無需單獨(dú)指定暴露端口。

docker-compose.yaml 文件如下

version: "3"
 
services:
  postgres:
    image: failymao/postgres:12.7
    container_name: postgres
    restart: unless-stopped
    privileged: true                                                      # 設(shè)置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  #數(shù)據(jù)掛載
    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"
 
  # 一個(gè)消費(fèi)者消費(fèi)一個(gè)數(shù)據(jù)庫
  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:

測(cè)試過程中因?yàn)橐褂?postgres, wal2json組件,在容器里單獨(dú)安裝組件很麻煩, 嘗試了幾次均已失敗而告終,所以后來選擇了將 postgres 服務(wù)安裝在宿主機(jī)上, 容器里面的synch服務(wù) 使用宿主機(jī)的 ip,port端口。

但是當(dāng)重新啟動(dòng)服務(wù)后,synch服務(wù)一直啟動(dòng)不起來, 日志顯示 postgres 無法連接. synch配置文件如下

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

這種情況很奇怪,首先確認(rèn) postgres, 啟動(dòng),且監(jiān)聽端口(此處是5433) 也正常,使用localhost和主機(jī)eth0網(wǎng)卡地址均報(bào)錯(cuò)。

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 模式下 容器內(nèi)服務(wù)訪問宿主機(jī)服務(wù)

將postgres監(jiān)聽地址修改如下 host.docker.internal 報(bào)錯(cuò)解決。 查看宿主機(jī) /etc/hosts 文件如下

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 = false
127.0.0.1       localhost
 
10.111.130.24    host.docker.internal

可以看到,宿主機(jī) ip跟域名的映射. 通過訪問域名,解析到宿主機(jī)ip, 訪問宿主機(jī)服務(wù)。

最終啟動(dòng) synch 服務(wù)配置如下

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.internal
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: 
    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. 總結(jié)

以--networks="host" 模式下啟動(dòng)容器時(shí),如果想在容器內(nèi)訪問宿主機(jī)上的服務(wù), 將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

到此這篇關(guān)于docker內(nèi)服務(wù)訪問宿主機(jī)服務(wù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)docker訪問宿主機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • docker與firewalld沖突問題及解決過程

    docker與firewalld沖突問題及解決過程

    文章指出firewalld與Docker競爭iptables轉(zhuǎn)發(fā)鏈規(guī)則,導(dǎo)致互相覆蓋,解決方案一是先重啟firewalld再重啟Docker;二是針對(duì)Docker 20.10.0以上版本,將docker0接口移至docker區(qū)域以避免沖突
    2025-09-09
  • docker安裝RabbitMQ及安裝延遲插件的詳細(xì)過程

    docker安裝RabbitMQ及安裝延遲插件的詳細(xì)過程

    MQ(message queue)字面意思上來說消息隊(duì)列,是一種跨進(jìn)程的通信機(jī)制,用于上下游傳遞消息,本文給大家詳細(xì)介紹docker安裝RabbitMQ及安裝延遲插件的過程,感興趣的朋友一起看看吧
    2022-06-06
  • docker部署rustdesk遠(yuǎn)程控制服務(wù)器的實(shí)現(xiàn)

    docker部署rustdesk遠(yuǎn)程控制服務(wù)器的實(shí)現(xiàn)

    RustDesk是一款體驗(yàn)優(yōu)秀的遠(yuǎn)程控制軟件,本文主要介紹了docker部署rustdesk遠(yuǎn)程控制服務(wù)器的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了
    2024-05-05
  • 如何完全清理你的Docker數(shù)據(jù)

    如何完全清理你的Docker數(shù)據(jù)

    Docker 不會(huì)對(duì)你的系統(tǒng)進(jìn)行任何配置更改……但是它會(huì)占用大量的磁盤空間,那么如何完全清理你的數(shù)據(jù),本文就詳細(xì)的來介紹一下,感興趣的可以了解一下
    2021-07-07
  • docker-compose集成Jenkins部署,打包,發(fā)布方式

    docker-compose集成Jenkins部署,打包,發(fā)布方式

    在Docker環(huán)境中安裝配置Jenkins,需掛載JDK、Maven等至容器指定路徑,設(shè)置國內(nèi)鏡像提升下載速度,并添加插件與憑據(jù)完成環(huán)境配置
    2024-10-10
  • docker安裝mongo容器并掛載外部配置文件及目錄方式

    docker安裝mongo容器并掛載外部配置文件及目錄方式

    文章總結(jié):本文詳細(xì)介紹了如何使用Docker安裝MongoDB,并將外部配置文件和目錄掛載到容器中,首先,拉取MongoDB鏡像,然后創(chuàng)建配置文件和目錄,并將配置文件內(nèi)容復(fù)制到容器中,最后,創(chuàng)建容器并運(yùn)行MongoDB
    2025-11-11
  • docker-compose啟動(dòng)mysql雙機(jī)熱備互為主從的方法實(shí)現(xiàn)

    docker-compose啟動(dòng)mysql雙機(jī)熱備互為主從的方法實(shí)現(xiàn)

    本文主要介紹了docker-compose啟動(dòng)mysql雙機(jī)熱備互為主從的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Docker多階段構(gòu)建鏡像multi-stage詳解

    Docker多階段構(gòu)建鏡像multi-stage詳解

    多階段構(gòu)建是Docker 17.05及以上版本的新特性,允許在一個(gè)Dockerfile中使用多個(gè)階段,每個(gè)階段可以有一個(gè)獨(dú)立的基礎(chǔ)鏡像,并且可以通過COPY --from命令在階段之間傳遞文件,這樣可以有效地減少最終鏡像的大小,只保留必要的文件
    2025-10-10
  • 一次centos Docker網(wǎng)橋模式無法訪問宿主機(jī)Redis服務(wù)的故障排除經(jīng)歷

    一次centos Docker網(wǎng)橋模式無法訪問宿主機(jī)Redis服務(wù)的故障排除經(jīng)歷

    這篇文章主要給大家介紹了關(guān)于一次centos Docker網(wǎng)橋模式無法訪問宿主機(jī)Redis服務(wù)的故障排除經(jīng)歷,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Docker具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Docker 集成KingBase的詳細(xì)過程

    Docker 集成KingBase的詳細(xì)過程

    這篇文章主要介紹了Docker 集成KingBase的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-04-04

最新評(píng)論

乌兰浩特市| 南阳市| 张家界市| 南华县| 文安县| 太仆寺旗| 奈曼旗| 监利县| 永川市| 武邑县| 囊谦县| 韶关市| 博白县| 龙山县| 正安县| 两当县| 武平县| 名山县| 罗定市| 大冶市| 渭南市| 祁门县| 黄骅市| 湛江市| 浦江县| 淳化县| 昭通市| 清新县| 钦州市| 文成县| 玉林市| 仲巴县| 铁力市| 武隆县| 垦利县| 宝鸡市| 原平市| 上饶县| 攀枝花市| 孟州市| 巫山县|