如何使用Docker网络和包含容器设置的两个不同的docker-compose文件从一个Docker容器向另一个Docker容器发出请求?
我有一个简单的Web API,应该使用此控制台应用程序调用。使用以下docker compose文件托管Web API:
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:https://localhost:44334/api/Role
...编辑我只有一个docker主机。
最佳答案
似乎是SSL问题,请尝试用https
中的http
替换var url = "https://webapi_test:44334/api/Role";
关于docker - 在具有两个docker compose的容器之间的docker网络中调用Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53528665/