简体   繁体   English

如何将 2 个独立的 docker 容器联网以相互通信?

[英]How to network 2 separate docker containers to communicate with eachother?

I'm pretty new to docker, and I've tried searching about networking but haven't found a solution that's worked.我对 docker 很陌生,我尝试过搜索网络,但没有找到有效的解决方案。

I have a Laravel app that is using Laradock.我有一个使用 Laradock 的 Laravel 应用程序。 I also have an external 3rd party API that runs in its own docker container.我还有一个外部第 3 方 API,它在自己的 docker 容器中运行。 I basically want to specify the container name of the api inside my laravel.env file, and have it dynamically resolve the container ip so I can make API calls from my Laravel app. I basically want to specify the container name of the api inside my laravel.env file, and have it dynamically resolve the container ip so I can make API calls from my Laravel app. I can already do this with services that are already part of laradock like mariadb/mysql, but since my API is located in an external container, it can't connect to it.我已经可以使用已经属于 laradock 的服务(如 mariadb/mysql)来执行此操作,但是由于我的 API 位于外部容器中,因此无法连接到它。

I tried making a network and attaching them with;我尝试建立一个网络并将它们连接起来;

docker network create my-network

Then inside my docker-compose.yml files for each of the containers, I specified;然后在每个容器的docker-compose.yml文件中,我指定了;

networks:
  my-network:
    name: "my-network"

But if I try and ping them with;但是,如果我尝试用 ping 它们;

docker exec -ti laradock-workspace-1 ping my-api

I can't connect and can't really figure out why.我无法连接,也无法真正弄清楚为什么。 Was hoping someone familiar with docker might be able to explain why since I'm sure it's something very obvious I'm missing.希望熟悉 docker 的人能够解释原因,因为我确信这是非常明显的东西我错过了。 Thanks!谢谢!

By default Docker Compose uses a bridge network to provision inter-container communication.默认情况下 Docker Compose 使用桥接网络来提供容器间通信。 Read this article for more info about inter-container networking. 阅读这篇文章,了解更多关于容器间网络的信息。

What matters for you, is that by default Docker Compose creates a hostname that equals the service name in the docker-compose.yml file.对您来说重要的是,默认情况下 Docker Compose 会创建一个与docker-compose.yml文件中的服务名称相同的主机名。 Consider the following docker-compose.yml :考虑以下docker-compose.yml

version: '3.9'
services:
  server:
    image: node:16.9.0
    container_name: server
    tty: true
    stdin_open: true
    depends_on:
       - mongo
    command: bash

  mongo:
    image: mongo
    environment:
      MONGO_INITDB_DATABASE: my-database

When you run docker-compose up , Docker will create a default network and assigns the service name as hostname for both mongo and server .当您运行docker-compose up , Docker 将创建一个默认网络并将服务名称分配为mongoserver的主机名。

You can now access the backend container via:您现在可以通过以下方式访问后端容器:

docker exec -it server bash

And now you can ping the mongo container using Dockers internal network (default on port 27017 in this case):现在您可以使用 Dockers 内部网络 ping mongo容器(在这种情况下默认使用端口 27017):

curl -v http://mongo:27017/my-database

That's it.而已。 The same applies for your setup.这同样适用于您的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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