是否可以按主机而不是IP卷曲外部容器?

时间:2019-11-26 16:53:02

标签: docker curl stack containers bad-request

我有两个单独的Docker堆栈。从堆栈1,我想知道是否可以使用容器名称而不是IP地址来卷曲堆栈2中的容器?

当卷曲到IP地址时,我收到HTTP代码200的响应。 卷曲名称时,我收到HTTP 400错误请求。

我尝试了几种不同的docker-compose配置。这是最新消息:

堆栈A

String sourceFile = "C:\Users\xxxxxx\Pictures\D8McXhNVUAE7VFh.jpg"
String destinationFilePath = "\\resources\\" + generatedID + ".jpg" ;
File sourceFile = new File(imageSourcePath);
File destinationFile = new File(destinationFilePath);
Path sourcePath = sourceFile.toPath();
Path destinationPath = destinationFile.toPath();
try {
          Files.copy(sourcePath, destinationPath);
} catch (IOException ex) {
           ex.printStackTrace();
}

堆栈B

version: '3'
services:
    php:
        build:
            context: .
            dockerfile: ./dev.dockerfile
        container_name: arm_php
        ports:
          - "8000:8000"
        volumes:
            - ./:/app
        links:
            - maildev:maildev
            - redis:redis
        external_links:
            - a25_mysql_1:a25_mysql
        networks: 
            - default
            - a25dashboardad_default
    maildev:
        image: djfarrelly/maildev
        hostname: residencia-maildev
        container_name: arm_maildev
        ports:
          - "1025:25"
          - "1080:80"
    redis:
        image: "redis"
        container_name: arm_redis
        ports:
            - "6379:6379"
networks:
    a25dashboardad_default:
        external: true

来自arm_php:

作品:version: '3' services: php: image: armcanada/php7-mssql container_name: a25_php ports: - "8001:8000" volumes: - ./:/app links: - maildev:maildev - database:database database: image: mysql:5.7.18 container_name: a25_mysql environment: - "MYSQL_DATABASE=a25_db" - "MYSQL_USER=user" - "MYSQL_PASSWORD=password" - "MYSQL_ROOT_PASSWORD=password" ports: - "33060:3306" volumes: - db:/var/lib/mysql maildev: image: djfarrelly/maildev hostname: residencia-maildev container_name: a25_maildev ports: - "1026:25" - "1081:80" volumes: db: networks: default: driver: bridge

不起作用:curl 172.19.0.4:8000/api/dmt

1 个答案:

答案 0 :(得分:1)

我终于可以通过结合使用用户定义的网络和别名来使其工作。这是详细信息:

堆栈A

version: '3'
services:
    php:
        build:
            context: .
            dockerfile: ./dev.dockerfile
        container_name: arm_php
        ports:
          - "8000:8000"
        volumes:
            - ./:/app
        links:
            - maildev:maildev
            - redis:redis
        external_links:
            - a25_mysql_1:a25_mysql
        networks: 
            - default
            - a25dashboardad_default
            - a25dashboardad_armnet
    maildev:
        image: djfarrelly/maildev
        hostname: residencia-maildev
        container_name: arm_maildev
        ports:
          - "1025:25"
          - "1080:80"
    redis:
        image: "redis"
        container_name: arm_redis
        ports:
            - "6379:6379"
networks:
    a25dashboardad_default:
        external: true
    a25dashboardad_armnet:
        external: true

堆栈B

version: '3'
services:
    php:
        image: armcanada/php7-mssql
        container_name: a25_php
        networks:
          default:
            aliases:
                - default
          armnet:
            aliases:
              - a25php
        ports:
          - "8001:8000"
        volumes:
            - ./:/app
        links:
            - maildev:maildev
            - database:database
    database:
        image: mysql:5.7.18
        container_name: a25_mysql
        ports:
            - "33060:3306"
        volumes:
             - db:/var/lib/mysql
    maildev:
        image: djfarrelly/maildev
        hostname: residencia-maildev
        container_name: a25_maildev
        ports:
          - "1026:25"
          - "1081:80"
volumes:
    db:
networks:
  armnet:
    driver: bridge

然后我可以使用curl a25php:8000卷曲arm_php表格。

原因是,正如文档所说: Docker不支持默认网桥网络上的自动服务发现。如果希望容器能够按容器名称解析IP地址,则应改用用户定义的网络。 https://docs.docker.com/v17.09/engine/userguide/networking/#user-defined-networks