iis服务器助手广告广告
返回顶部
首页 > 资讯 > 服务器 >docker内服务访问宿主机服务的实现
  • 219
分享到

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

2024-04-02 19:04:59 219人浏览 泡泡鱼
摘要

目录1. 场景2. 解决3. 总结4. 参考1. 场景 使用windows, wsl2 进行日常开发测试工作。 但是wsl2经常会遇到网络问题。比如今天在测试一个项目,核心功能是将

1. 场景

使用windows, wsl2 进行日常开发测试工作。 但是wsl2经常会遇到网络问题。比如今天在测试一个项目,核心功能是将postgres 的数据使用开源组件synch 同步到clickhouse 这个工作。

测试所需组件

  1. postgres
  2. kafka
  3. ZooKeeper
  4. Redis
  5. synch容器

最开始测试时,选择的方案是, 将上述五个服务使用 Docker-compose 进行编排, network_modules使用hosts模式, 因为考虑到kafka的监听安全机制,这种网络模式,无需单独指定暴露端口。

docker-compose.yaml 文件如下


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配置文件如下


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 文件如下


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

可以看到,宿主机 ip跟域名的映射. 通过访问域名,解析到宿主机ip, 访问宿主机服务。

最终启动 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: 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. 总结

以--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访问宿主机内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: docker内服务访问宿主机服务的实现

本文链接: https://www.lsjlt.com/news/155008.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • docker内服务访问宿主机服务的实现
    目录1. 场景2. 解决3. 总结4. 参考1. 场景 使用windows, wsl2 进行日常开发测试工作。 但是wsl2经常会遇到网络问题。比如今天在测试一个项目,核心功能是将...
    99+
    2024-04-02
  • docker内部访问宿主机的方法是什么
    Docker容器可以通过宿主机的IP地址或者宿主机名进行访问。在Docker中,宿主机的IP地址通常是172.17.0.1(也可能是...
    99+
    2024-04-09
    docker
  • docker访问宿主机的方法是什么
    非常抱歉,由于您没有提供文章标题,我无法为您生成一篇高质量的文章。请您提供文章标题,我将尽快为您生成一篇优质的文章。...
    99+
    2024-05-14
  • Docker容器与宿主机相互访问更方便的方法
    近期公司在给客户安装部署项目时,由于客户电脑处于无任何网络下,因此需要宿主机与容器之间可以互相访问;但是,由于容器的特性,localhost和127.0.0.1在容器中都是指向容器内...
    99+
    2023-05-20
    docker容器与宿主机相互访问命令 docker容器和宿主机通信 docker容器与宿主机相互访问
  • Docker跨主机容器间相互访问的实现
    第一步:创建自定义网络 docker network create --subnet=172.18.0.0/24 docker-br0 备注:这里选取了172.18.0.0网段,也可...
    99+
    2023-01-04
    Docker 跨主机访问 Docker 容器相互访问
  • docker实现跨宿主机的容器之间网络互联
    目录一. 环境介绍二. docker跨主机互联实现说明三. 修改docker0网桥网段3.1 A宿主机10.1.10.1133.2 B宿主机10.1.10.114背景:最近闲来无事,...
    99+
    2023-01-04
    docker 跨宿主机互联 Docker跨宿主机
  • 阿里云服务器访问国内实现远程控制与访问
    在当今信息时代,远程访问服务器已成为企业业务运营中必不可少的一部分。特别是在国内,由于地理位置和网络环境的限制,企业可能需要通过阿里云服务器来访问国内的数据资源和业务系统。本文将详细介绍如何通过阿里云服务器访问国内,并提供相关的解决方案和建...
    99+
    2023-12-16
    阿里 远程控制 服务器
  • 搭建frp+OpenVPN实现公网服务器对内网服务器的访问
    搭建frp+OpenVPN实现公网服务器对内网服务器的访问 1.准备 1.1实验需求 本实验需求一台公网服务器,两台内网服务器 公网服务器:作为frp服务端以及openvpn客户端第一台内网服务器:作为frp客户端以及openvpn服务端第...
    99+
    2023-12-23
    服务器 运维 网络 linux
  • 云服务器访问内网
    云服务器访问内网是一种流行的网络应用,它允许用户通过云服务器访问互联网。但是,使用云服务器访问内网可能会出现一些安全问题,以下是一些需要了解的问题: 数据泄露和安全问题:当用户将数据存储在云服务器上时,可能会发生数据泄露和安全问题。这可...
    99+
    2023-10-26
    内网 服务器
  • 如何实现Windows宿主系统和虚拟机ubuntu系统文件互相访问
    今天就跟大家聊聊有关如何实现Windows宿主系统和虚拟机ubuntu系统文件互相访问,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。我的宿主操作系统是Windows 10,使用Ora...
    99+
    2023-06-06
  • Docker部署MinIO对象存储服务器结合内网穿透实现远程访问
    文章目录 前言1. Docker 部署MinIO2. 本地访问MinIO3. Linux安装Cpolar4. 配置MinIO公网地址5. 远程访问MinIO管理界面6. 固定MinIO公网地址 前言 MinIO是一个开源的对...
    99+
    2023-12-22
    docker 服务器 容器
  • 阿里云服务器怎么访问自己的主机
    如果您想访问阿里云服务器,可以通过以下步骤: 打开浏览器并登录阿里云帐户。 在网站首页点击“网站首页”。 点击“登录”并进入登录页面。 输入阿里云帐户密码,即可登录到阿里云服务器。如果忘记了密码,可以在登录界面的“忘记密码”按钮上输入正...
    99+
    2023-10-26
    自己的 阿里 主机
  • 云服务器端口映射:实现内网端口的访问
    什么是端口映射? 在云服务器中,端口映射是一种将外部网络请求转发到内部网络的技术。通过端口映射,可以将云服务器的公网IP地址和端口与内网服务器的IP地址和端口进行绑定,从而实现对内网服务的访问。 为什么需要端口映射? 在云服务器中,内网I...
    99+
    2023-10-28
    端口 内网 端口映射
  • 云服务器访问内网QNAP
    作为一个普通的计算机爱好者,我经常会在内网中访问QNAP。但是,我很少知道如何使用它。最近,我在一次技术分享会上遇到了QNAP,并被它的访问内网的方式所吸引。下面是我使用QNAP的方法,希望能为其他人提供一些参考。 首先,你需要在你的服务...
    99+
    2023-10-28
    内网 服务器 QNAP
  • redis服务器如何允许远程主机访问
    这篇文章主要介绍了redis服务器如何允许远程主机访问,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。若远程主机需要访问redis服务器,可以...
    99+
    2024-04-02
  • 云服务器怎么访问内网服务器
    在访问内网服务器时,首先需要确定访问内网服务器的目的地。通常情况下,企业的内部服务器可以通过网络访问到云服务器,而企业的网络访问服务器则需要通过互联网进行访问。因此,如果企业需要访问内网服务器,他们可以通过连接互联网,然后使用企业内部的网络...
    99+
    2023-10-28
    服务器 内网
  • 手机访问云服务器
    如果您要访问的是 Web 服务器或其他 Web 服务,您可以使用 Web 访问云服务器,例如 Amazon WebServices(AWS)或Google Cloud Cloud。以下是一些常用的步骤,可以帮助您开始访问云服务器: 选择...
    99+
    2023-10-26
    服务器 手机
  • vscode内网访问服务器的方法
    目录文章背景1.插件插件下载插件安装2.内网无网络安装ssh无法访问服务器下载vscode-server-linux-x64.tar.gz使用vscode-server-linux-...
    99+
    2024-04-02
  • 关于docker部署服务时ip无法访问服务正常的问题
    背景 今日,使用docker部署应用的时候,无法使用IP地址访问,防火墙已经关闭,可以ping通,应用已经配置0.0.0.0 解决 经过查阅资料。 学习网络传输中有一个 net.ip...
    99+
    2024-04-02
  • Docker和宿主机操作系统文件目录互相隔离的实现原理
    我们知道在Docker容器里是无法访问到宿主操作系统的文件目录的,但这种隔离是怎么实现的呢?其实一点也不神奇——利用了Linux系统的内部命令chroot。chroot能将进程的根目录设置成任意指定的目录。使用chroot我们能创建一个新的...
    99+
    2023-06-04
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作