Q:

Resolve container name in extra_hosts option in docker-compose

I need to curl my API from another container.
Container 1 is called nginx
Container 2 is called fpm
I need to by able to bash into my FPM container and curl the nginx container.
Config:
services:

  nginx:
    build:
      context: .
      dockerfile: ./docker/nginx/Dockerfile
    volumes:
      - ./docker/nginx/conf/dev/api.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8080:80
    links:
      - fpm

  fpm:
    build:
      context: .
      dockerfile: ./docker/fpm/Dockerfile
    volumes:
      - .:/var/www/html
      - ./docker/fpm/conf/dev/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
      - ./docker/fpm/conf/dev/api.ini:/usr/local/etc/php/conf.d/api.ini
    env_file:
      - ./docker/mysql/mysql.env
      - ./docker/fpm/conf/dev/fpm.env
    links:
      - mysql
    shm_size: 256M
    extra_hosts:
      - myapi.docker:nginx

My initial thought was to slap it in the extra_hosts option like:
extra_hosts:
  - myapi.docker:nginx

But docker-compose up fails with:

ERROR: for apiwip_fpm_1  Cannot create container for service fpm: invalid IP address in add-host: "nginx"

I have seen some examples of people using docker's network configuration but it seems over the top to just resolve an address.
How can I resolve/eval the IP address of the container rather than just passing it literally?

A:

I solved this kind of issue by using links instead of extra_hosts.
In that case, just set link alias can do your favor.
service fpm setting
links
  - nginx:myapi.docker

See docker-compose links documentation, The alias name can be the domain which appears in your code.

A:

Add network aliases in the default network.
version: "3.7"
services:

  nginx:
    # ...
    networks:
      default:
        aliases:
          - example.local

  browser-sync:
    # ...
    depends_on:
      - nginx
    command: "browser-sync start --proxy http://example.local"

A:

    services:
    
      nginx:
        build:
          context: .
          dockerfile: ./docker/nginx/Dockerfile
        volumes:
          - ./docker/nginx/conf/dev/api.conf:/etc/nginx/conf.d/default.conf
        ports:
          - 8080:80
        networks:
          my_network:
            aliases:
              - myapi.docker
              - docker_my_network
    
      fpm:
        build:
          context: .
          dockerfile: ./docker/fpm/Dockerfile
        volumes:
          - .:/var/www/html
          - ./docker/fpm/conf/dev/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
          - ./docker/fpm/conf/dev/api.ini:/usr/local/etc/php/conf.d/api.ini
        env_file:
          - ./docker/mysql/mysql.env
          - ./docker/fpm/conf/dev/fpm.env
        links:
          - mysql
        shm_size: 256M
        networks:
          - my_network
    
      networks:
        my_network:
          driver: bridge

add custom network and add container to that network
by default if you curl nginx container from fpm you will curl localhost, so we need to add alias to be the same as your servername in the nginx configuration
with this solution you can curl myapi.docker from the fpm container

@edward let me know if this solution worked for you
Edit:
Jack_Hu is right, I removed extra_hosts. Network alias is enough.

