首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停靠容器中的JHipster应用程序无法在主机网络上访问,无法与非主机网络上的其他容器通信

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

Stack Overflow用户
提问于 2017-09-05 22:57:48
回答 1查看 1.1K关注 0票数 1

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

我或多或少使用了JHipster提供的默认docker-compose配置来部署注册表:

代码语言:javascript
复制
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使微服务可用,这样我就可以通过浏览器访问它,但微服务找不到注册表。

代码语言:javascript
复制
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“来解决这个问题。但是,当我这样做时,这显然会覆盖本机到主机的端口映射,并且无法从浏览器访问微服务。

更奇怪的是,大约一周前,我还在使用完全相同的配置,它工作得很好。

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

EN

回答 1

Stack Overflow用户

发布于 2017-09-05 23:08:57

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

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

代码语言:javascript
复制
docker network create my-network

通过为容器指定名称并将其添加到network create来更新撰写文件:

代码语言:javascript
复制
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

在运行应用程序时,请指定网络:

代码语言:javascript
复制
docker run --network=my-network ...

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46058052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档