1

I'm pretty new to docker, and I've tried searching about networking but haven't found a solution that's worked.

I have a Laravel app that is using Laradock. I also have an external 3rd party API that runs in its own docker container. 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.

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;

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

But if I try and ping them with;

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. Thanks!

Borassign
  • 621
  • 2
  • 7
  • 16
  • could you add your docker-compose.yml? – Lety Nov 29 '21 at 09:15
  • is this help?[example](https://docs.docker.com/engine/reference/commandline/network_create/#examples) – Vox Nov 29 '21 at 09:17
  • you should inspect which network are used by container and if you want to connect your container to one network created by other compose, use external in networks definition. this link could help you: https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects – Lety Nov 29 '21 at 10:06
  • have you tried https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects ? – Adam Oct 19 '22 at 06:34

1 Answers1

1

By default Docker Compose uses a bridge network to provision inter-container communication. 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. Consider the following 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.

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):

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

That's it. The same applies for your setup.

Robert-Jan Kuyper
  • 2,418
  • 12
  • 22
  • I have 2 docker compose files that spawn their own containers. 1 of them is Laradock which spawns microcontainers for php, nginx, etc... The issue is trying to get those 2 talking together. – Borassign Nov 29 '21 at 09:56