如何使用 docker 网络和两个包含容器设置的不同 docker-compose 文件从一个 docker 容器向另一个容器发出请求?
我有一个简单的 web api,应该使用这个控制台应用程序调用。Web api 使用以下 docker compose 文件托管:
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
应该使用这个简单的控制台应用程序调用 web api:
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 组成控制台应用程序:
version: '3.5'
services:
networksample:
image: ${DOCKER_REGISTRY-}networksample
build:
context: .
dockerfile: NetworkSample/Dockerfile
networks:
- sample_network
networks:
simplic_network:
name: sample_network
据我所知,通信应该在本地 docker 网络中完成。但我总是在控制台应用程序中收到以下错误消息:SocketException: Resource temporarily unavailable
Web api is available from outside: https://localhost:44334/api/Role
...
编辑我只有一个码头主机。