简体   繁体   中英

Call web api in docker network between container with two docker compose

How can I make a request from one docker container to another using the docker network and two different docker-compose files which contain the container settings?

I've a simple web api that should be called using this console application. The web api is hosted using the following docker compose file:

version: '3.5'

services:
  webapi_test:
    image: ${DOCKER_REGISTRY-}webapi_test
    build:
      context: .
      dockerfile: webapi_test/Dockerfile    
    links:
      - mongo
    networks:
      - sample_network
  mongo:
    image: mongo:latest
    container_name: "mongodb"
    ports:
      - "27017:27017"
    command: mongod --smallfiles --logpath=/dev/null # --quiet
    networks:
      - sample_network

networks:
  sample_network:
    name: sample_network

The web api should be called using this simple console application:

class Program
{
    static void Main(string[] args)
    {
        var url = "https://webapi_test:44334/api/Role";
        var client = new HttpClient();
        var result = client.GetStringAsync(url).Result;

        Console.WriteLine($"Result: {result}");
    }
}

Docker compose of the console application:

version: '3.5'

services:
  networksample:
    image: ${DOCKER_REGISTRY-}networksample
    build:
      context: .
      dockerfile: NetworkSample/Dockerfile
    networks:
      - sample_network

networks:
  simplic_network:
    name: sample_network

The communication should be done in the local docker network as far as I know. But I always get the following error message in the console application: SocketException: Resource temporarily unavailable The web api is available from outside: https://localhost:44334/api/Role ...

EDIT I just have one docker host.

似乎是SSL问题,请尝试使用var url = "https://webapi_test:44334/api/Role"; http替换https var url = "https://webapi_test:44334/api/Role";

make sample_network as external network:

version: '3.5'

services:
  networksample:
    image: ${DOCKER_REGISTRY-}networksample
    build:
      context: .
      dockerfile: NetworkSample/Dockerfile
    networks:
      - sample_network

networks:
  simplic_network:
    external:
       name: sample_network

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM