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

时间:2017-09-05 14:57:48

标签: docker docker-for-mac

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

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

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"来解决这个问题。在启动微服务时。但是,当我这样做时,这显然会覆盖本机到主机端口映射,并且无法从浏览器访问微服务。

大约一周前,我使用完全相同的配置更加奇怪,这种配置很好。

enter image description here

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

1 个答案:

答案 0 :(得分:-1)

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

首先使用以下方式创建网络:

docker network create my-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

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

docker run --network=my-network ...

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

相关问题