Can't communicate between certain services from same `docker-compose.yml`?

I’m running a single docker-compose.yml file that deploys multiple services;
- api_db
- api
- web_db
- php
- nginx

I also have all these services connected to a network defined below;

networks:
  my-network:
    name: my-network

On their own these containers work flawlessly. I can run docker and access the API from my host, as well as my web app, and the .env substitution works well for connecting web to web_db.

Where I’m having trouble is I want to get the IP/Host of the container api and substitute it inside my web app’s .env file so I can query the API from my web app. Currently when attempting this, my web app takes it literally as “api” and tries to send a request to “https://api:443/api/…”. The strange thing is, this is located in exactly the same .env file that I use to substitute web_db as the host, so I’m a bit confused as to why this is happening.

I’ve already confirmed with docker network inspect my-network that all containers are connected to my network correctly.

Not really sure what the issue is so hopefully someone can assist.

docker-compose.yml
version: "3.5"

networks:
  my-network:
    name: my-network

services:
  api_db:
    image: mariadb:10.5
    container_name: api_db
    ports:
      - "3307:3306"
    command:
      - "mysqld"
      - "--character-set-server=utf8mb4"
      - "--collation-server=utf8mb4_unicode_ci"
    volumes:
      - "./mnt/docker/api/db:/var/lib/mysql"
    restart: always
    networks:
      - my-network

  api:
    build:
      context: ./dockerfiles
      dockerfile: api.dockerfile
      args:
        - UID=${UID:-1000}
        - GID=${GID:-1000}
    container_name: api
    ports:
      - "8001:8000"
    depends_on:
      - api_db
    volumes:
      - "./mnt/docker/api/data:/data"
    restart: always
    networks:
      - my-network
      
  web_db:
    image: mariadb:10.5
    container_name: web_db
    ports:
      - "3308:3306"
    command:
      - "mysqld"
      - "--character-set-server=utf8mb4"
      - "--collation-server=utf8mb4_unicode_ci"
    volumes:
      - "./mnt/docker/web/db:/var/lib/mysql"
    restart: always
    networks:
      - my-network

  php:
    build:
      context: ./dockerfiles
      dockerfile: php.dockerfile
      args:
        - UID=${UID:-1000}
        - GID=${GID:-1000}
    container_name: php
    volumes:
      - ./src:/var/www/html:delegated
    networks:
      - my-network

  nginx:
    build:
      context: ./dockerfiles
      dockerfile: nginx.dockerfile
      args:
        - UID=${UID:-1000}
        - GID=${GID:-1000}
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/html:delegated
    depends_on:
      - php
      - web_db
    networks:
      - my-network

Please share the .env file as well - right now it is a pure guessing game what value you might have used there.

Though, you do use the service_name:container_port to access the service of the other container, right?
E.g. the php-service tries to access the api-service using http://api:8000/… (or if api offers https, then of course with https:// instead of http://)

@meyay
This is the relevant part of my env here;
It’s located at /src/.env

API_HOST=api:8001
API_KEY=

Since I’m using Laravel, here you can see the exception it gets when the API call from my web app fails.

If I swap out the “api” part of that exception above and access it from local host with 127.0.0.1:81 then the request works, so it’s very strange. I’m not sure why these containers refuse to talk to eachother when they are on the same network and even the same docker-compose file.

Something doesn’t add up here…

  1. you are using the publised host port for inter-container-network communication. Please re-read what I wrote about it in my last post.
  2. Your are using the https protocol to access the api-service. This only works if you did build your api image with https enabled. Did you add certificates and specificy enabled the https endpoint - or is this enabled with generic self signed certificates by default?
  3. So if you change API_HOST to 127.0.0.1:81 it works? Please elaborate why this would make sense.