简体   繁体   English

一个Docker容器如何调用另一个Docker容器

[英]How can one Docker container call another Docker container

I have two Docker containers我有两个 Docker 容器

  1. A Web API一个 Web API
  2. A Console Application that calls Web API调用 Web API 的控制台应用程序

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给定:数据提供者服务,数据消费者服务

  • Option 1: Using Network选项 1:使用网络
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/

  • Option 2: Using Docker Compose选项 2:使用 Docker 撰写

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.

相关问题 如何将一个Docker容器与另一个Docker容器链接 - How to link one docker container with another docker container 如何将一个Docker容器链接到另一个Docker容器中的postgres数据库? - How can I have one Docker container link to a postgres db in another Docker container? 一个 Docker Container 如何找到另一个 Docker Container 的 IP 地址? - How can one Docker Container find the IP address of another Docker Container? 如何将端口从一个 docker 容器转发到另一个? - How can I forward a port from one docker container to another? 一个docker容器中的进程可以写入另一个容器中进程的stdin吗 - Can a process in one docker container write to stdin of a process in another container 一个 docker 容器是否可以访问另一个容器的 /tmp 文件夹? - Does one docker container can access the /tmp folder of another container? 从另一个容器调用 docker 容器 - call a docker container from another container 如何从运行在 docker 组合中不同端口上的另一个 flask 容器调用一个容器的端点? - How to call an endpoint of one container from another flask container running on different ports in docker compose? Docker:如何通过网络将一个容器连接到另一个容器 - Docker : How to connect one container to another container via a network 从一个 Docker 容器内部,如何连接到同一 Docker 主机上另一个 Docker 容器的端口? - From inside one Docker container, how to connect to the port of another Docker container on the same Docker host?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM