[英]How can one Docker container call another Docker container
I have two Docker containers我有两个 Docker 容器
Now, on my local web api is local host and Console application has no problem calling the API.However, I have no idea when these two things are Dockerized, how can I possibly make the Url of Dockerized API available to Dockerized Console application?现在,在我的本地 web api 是本地主机,控制台应用程序调用 API 没有问题。但是,我不知道这两个东西何时被 Dockerized,我怎么可能使 Dockerized API 的 Url 可用于 Dockerized 控制台应用程序?
i don't think i need a Docker Compose because I am passing the Url of API as an argument of the API so its just the matter of making sure that the Dockerized API's
url is accessible by Dockerized Console
我认为我不需要 Docker Compose,因为我将 API 的 Url 作为 API 的参数传递,所以这只是确保
Dockerized API's
url 可通过Dockerized Console
访问的问题
Any ideas?有任何想法吗?
This is the best way I have found to connect multiple containers in a local machine / single cluster.这是我发现在本地机器/单个集群中连接多个容器的最佳方式。
Given: data-provider-service, data-consumer-service给定:数据提供者服务,数据消费者服务
docker network create data-network
docker run --name=data-provider-service --net=data-network -p 8081:8081 data-provider-image
docker run --name=data-consumer-service --net=data-network -p 8080:8080 data-consumer-image
Make sure to use URIs like: http://data-provider-service:8081/
inside your data-consumer-service
.确保在
data-consumer-service
中使用如下 URI: http://data-provider-service:8081/
。
You can define both the services in a docker-compose.yml file and use depends_on
property in data-provider-service
.您可以在 docker-compose.yml 文件中定义这两种服务,并在
data-provider-service
中使用depends_on
属性。 eg例如
data-consumer-service:
depends_on:
- data-provider-service
You can see more details here on my Medium post: https://saggu.medium.com/how-to-connect-nultiple-docker-conatiners-17f7ca72e67f您可以在我的 Medium 帖子上查看更多详细信息: https://saggu.medium.com/how-to-connect-nultiple-docker-conatiners-17f7ca72e67f
You can use the link
option with docker run
: 您可以在
docker run
使用link
选项:
Run the API: 运行API:
docker run -d --name api api_image
Run the client: 运行客户端:
docker run --link api busybox ping api
You should see that api
can be resolved by docker. 您应该看到
api
可以通过docker解决。
That said, going with docker-compose
is still a better option. 也就是说,使用
docker-compose
仍然是一个更好的选择。
The idea is not to pass the url, but the hostname of the other container you want to call. 我们的想法不是传递url,而是要传递的另一个容器的主机名。
See Networking in Compose 请参阅撰写网络
By default Compose sets up a single network for your app.
默认情况下,Compose为您的应用设置单个网络。 Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
服务的每个容器都加入默认网络,并且该网络上的其他容器都可以访问它们,并且它们可以在与容器名称相同的主机名上发现。
This is what replace the deprecated --link
option . 这就是取代已弃用的
--link
选项的原因 。
And if your containers are not running on a single Docker server node, Docker Swarm Mode would enable that discoverability across multiple nodes. 如果您的容器未在单个Docker服务器节点上运行,则Docker Swarm模式将在多个节点之间实现该可发现性。
The problem can be solved easily if using compose feature. 如果使用撰写功能,可以轻松解决问题。 With compose, you just create one configuration file (
docker-compose.yml
) like this : 使用compose,您只需创建一个配置文件(
docker-compose.yml
),如下所示:
version: '3'
services:
db:
image: postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
To make it run, just call up
like this : 为了使其运行,只需调用
up
是这样的:
docker-compose up
This is the best way to run all your stack, so, check this reference : https://docs.docker.com/compose/ 这是运行所有堆栈的最佳方式,因此,请查看以下参考: https : //docs.docker.com/compose/
Success! 成功!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.