无法在主机网络上访问docker容器中的JHipster应用程序,无法与非主机网络上的其他容器进行通信 [英] JHipster application in docker container cannot be accessed on host network, cannot communicate with other containers on non-host network
问题描述
我正在尝试在OS X开发计算机上的docker容器中部署JHipster微服务和注册表.
I am trying to deploy my JHipster microservices and registry in docker containers on my OS X development machine.
我或多或少使用JHipster开箱即用的默认docker-compose配置来部署注册表:
I deploy the registry using more or less the default docker-compose configuration JHipster provides out of the box:
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
部署微服务时,会发生以下两种情况之一:
When I deploy my microservices using docker run
, however, one of two things happens:
如果发布端口,我想使用-p 8080:8080
启用微服务,以便可以通过浏览器访问它,则可以访问它,但是微服务找不到注册表.
If I publish the port I want to make the microservice available on using -p 8080:8080
so I can access it through the browser, I can reach it but the microservice cannot find the registry.
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)
与此同时,我可以查看注册表可以正常运行的页面.
Meanwhile, I can view the pages the registry serves fine.
我可以通过在启动微服务时添加"--network = host"来解决此问题.但是,当我这样做时,这显然会覆盖本机到主机的端口映射,并且无法从浏览器访问微服务.
I can fix this by adding "--network=host" when starting the microservice. However, when I do that this apparently overrides the native-to-host port mapping and the microservice cannot be reached from the browser.
更奇怪的是,大约一周前,我使用的是完全相同的配置,效果很好.
What's more bizarre is that about a week ago I was using the exact same configuration, which worked fine.
如果我在Docker容器之外运行我的应用程序,则它可以正常连接到注册表.如果我创建其他容器,或者连接到微服务容器并通过curl访问配置URL,则会收到响应.
If I run my application outside of a docker container, it connects to the registry fine. If I create a different container, or connect to the microservice container and access the configuration url via curl, I get a response.
推荐答案
您的应用正在尝试使用名称jhipster-registry
作为主机名来查找注册表.为此,您需要将注册表和应用添加到Docker网络.
Your app is trying to locate the registry using the name jhipster-registry
as a host name. To be able to do that, you need to have your registry and app added to a docker network.
首先使用以下方法创建网络:
First create the network using:
docker network create my-network
通过给容器命名并将其添加到网络创建中来更新撰写文件:
Update the compose file by giving the container a name and adding it to the network create:
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
运行应用程序时,请指定网络:
When running your application specify the network:
docker run --network=my-network ...
现在,您的应用程序可以使用jhipster-registry
作为主机名与注册表进行通信.
Now your application can communicate with the registry using jhipster-registry
as a hostname.
这篇关于无法在主机网络上访问docker容器中的JHipster应用程序,无法与非主机网络上的其他容器进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!