【问题标题】:I can not access by public IP to my Docker Node.js container我无法通过公共 IP 访问我的 Docker Node.js 容器
【发布时间】:2020-02-02 15:01:57
【问题描述】:

我有一个 Docker Node.js 容器监听端口 4757 和 NET 库,使用我的公共 IP。

server listening on {"address":"144.xxx.xxx.66","family":"IPv4","port":4757}

当我从我的计算机访问时,我没有问题,但如果我尝试访问我的手机或其他设备,则连接被拒绝 (ERR_ADDRESS_UNREACHABLE)。

  • 我禁用了 Linux 防火墙。
  • 如果我对我的公共 IP 执行 ping 操作,它会正常工作。
  • 我在路由器 ZTE (Yoigo) 中打开了端口 4757,LAN 主机为 192.168.1.128 TCP 和 UPD

docker-composer 在其他笔记本电脑上正常运行该应用程序,问题出在我的新笔记本电脑上。这是我的 local.yml

version: '3'

volumes:
  local_postgres_data: {}
  local_postgres_data_backups: {}

services:
  django: &django
  build:
    context: .
    dockerfile: ./compose/local/django/Dockerfile
  image: hegeo_local_django
  depends_on:
    - postgres
  volumes:
    .:/app
  env_file:
    - ./.envs/.local/.django
    - ./.envs/.local/.postgres
  ports:
    - "8000:8000"
  command: /start
  networks:
    default:
      ipv4_address: 144.xxx.xxx.3

  node:
    build:
      context: .
      dockerfile: ./compose/production/nodejs/Dockerfile
    image: hegeo_node
    depends_on:
      - postgres
    links:
      - postgres
    ports:
      - "4757:4757"
    networks:
      default:
        ipv4_address: 144.xxx.xxx.66

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    image: hegeo_production_postgres
    volumes:
      - local_postgres_data:/var/lib/postgresql/data
      - local_postgres_data_backups:/backups
    env_file:
      - ./.envs/.local/.postgres
    networks:
      default:
        ipv4_address: 144.xxx.xxx.4

networks:
  default:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "false"
      ipam:
        driver: default
        config:
          - subnet: 144.xxx.xxx.0/24

我认为我必须使用“docker run”,但也不能正常工作。 Docker - Make docker containers use my public ip

编辑: 关键是,我使用了网络“驱动程序”,因为我从地理定位设备接收跟踪,我需要使用 Node.js 解析此跟踪并将其发送到 Django。如果我没有设置网络并将 ipv4_address 分配给 Django,则 Django 和 Node.js 之间的连接不起作用。

【问题讨论】:

  • 在您的新笔记本电脑中删除此ipv4_address: 144.xxx.xxx.66
  • 谢谢,它可以工作,但我需要设置 ipv4_address,我更新了我的帖子来解释它。

标签: node.js docker docker-compose ip


【解决方案1】:

您提到您已打开端口并将其从路由器转发到笔记本电脑。 您的 docker yaml 不需要知道公共 IP,因此您应该删除网络设置并进行以下操作:

Internet > (public IP) Router (NAT to Local Docker host machine IP) > Docker host laptop (with firewall off, or port open) > docker engine (port is mapped from host to container)  > your Node.js script listening to on the port.

【讨论】:

  • 另一个参考信息,您不能只分配公共 IP,除非您实际上是从您的 ISP 购买的,并且他们将它们正确路由到您的路由器。
  • 谢谢,它可以工作,但我需要这个网络设置,我更新了我的帖子来解释它。
  • 您可以分配 IP,但不能分配公共 IP。尝试一个简单的 10.x.x.x 范围,端口映射会处理传入的流量,您可以使用您设置的私有 IP 与 django 进行通信。
猜你喜欢
  • 2018-09-07
  • 2020-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
  • 1970-01-01
  • 2022-12-30
  • 1970-01-01
相关资源
最近更新 更多