gpt4 book ai didi

docker - docker 容器中的 JHipster 应用程序无法在主机网络上访问,无法与非主机网络上的其他容器通信

转载 作者:行者123 更新时间:2023-12-02 20:12:16 24 4
gpt4 key购买 nike

我正在尝试在我的 OS X 开发机器上的 docker 容器中部署我的 JHipster 微服务和注册表。

我或多或少地使用 JHipster 开箱即用的默认 docker-compose 配置来部署注册表:

version: '2'
services:
jhipster-registry:
image: jhipster/jhipster-registry:v3.1.0
volumes:
- ./central-server-config:/central-config
# When run with the "dev" Spring profile, the JHipster Registry will
# read the config from the local filesystem (central-server-config directory)
# When run with the "prod" Spring profile, it will read the configuration from a Git repository
# See https://jhipster.github.io/microservices-architecture/#registry_app_configuration
environment:
- SPRING_PROFILES_ACTIVE=dev,native
- SECURITY_USER_PASSWORD=admin
- SPRING_CLOUD_CONFIG_SERVER_NATIVE_SEARCH_LOCATIONS=file:./central-config/localhost-config/
# - GIT_URI=https://github.com/jhipster/jhipster-registry/
# - GIT_SEARCH_PATHS=central-config
ports:
- 8761:8761

当我使用 docker run 部署我的微服务时,但是,会发生以下两种情况之一:

如果我发布端口,我希望使用 -p 8080:8080 使微服务可用所以我可以通过浏览器访问它,我可以访问它但是微服务找不到注册表。
Could not locate PropertySource: I/O error on GET request for "http://jhipster-registry:8761/config/clientaggregator/dev,twilio": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

同时,我可以查看注册表服务正常的页面。

我可以通过在启动微服务时添加“--network=host”来解决这个问题。但是,当我这样做时,这显然会覆盖 native 到主机的端口映射,并且无法从浏览器访问微服务。

更奇怪的是,大约一周前,我使用了完全相同的配置,效果很好。

enter image description here

如果我在 docker 容器之外运行我的应用程序,它会很好地连接到注册表。如果我创建一个不同的容器,或者连接到微服务容器并通过 curl 访问配置 url,我会收到响应。

最佳答案

您的应用程序正在尝试使用名称 jhipster-registry 查找注册表作为主机名。为此,您需要将注册表和应用程序添加到 docker 网络。

首先使用以下命令创建网络:

docker network create my-network

通过为容器命名并将其添加到网络创建来更新 compose 文件:
version: '2'
services:
jhipster-registry:
image: jhipster/jhipster-registry:v3.1.0
volumes:
- ./central-server-config:/central-config
# When run with the "dev" Spring profile, the JHipster Registry will
# read the config from the local filesystem (central-server-config directory)
# When run with the "prod" Spring profile, it will read the configuration from a Git repository
# See https://jhipster.github.io/microservices-architecture/#registry_app_configuration
environment:
- SPRING_PROFILES_ACTIVE=dev,native
- SECURITY_USER_PASSWORD=admin
- SPRING_CLOUD_CONFIG_SERVER_NATIVE_SEARCH_LOCATIONS=file:./central-config/localhost-config/
# - GIT_URI=https://github.com/jhipster/jhipster-registry/
# - GIT_SEARCH_PATHS=central-config
ports:
- 8761:8761
networks:
- my-network
container_name: jhipster-registry


networks:
my-network:
external: true

运行应用程序时指定网络:
docker run --network=my-network ...

现在您的应用程序可以使用 jhipster-registry 与注册表通信。作为主机名。

关于docker - docker 容器中的 JHipster 应用程序无法在主机网络上访问,无法与非主机网络上的其他容器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com