docker – add extra_hosts with name instead of IP

I’d like to add a resolved domain name to my /etc/hosts file on a Docker container. I see that with extra_hosts in docker-compose.yml file I can achieve this however I am not being permitted to add a domain name, only an IP address.

docker-compose.yml

version: '2'
services:
  web:
    build: .
    ports:
      - "8090:80"
    extra_hosts:
      - "192.168.65.2:api.test"
    volumes:
      - .:/var/www/html
    network_mode: bridge

However when I run docker-compose up I get the following error:

ERROR: for backoffice_web_1  Cannot create container for service web: b'invalid IP address in add-host: "api.test"'

ERROR: for web  Cannot create container for service web: b'invalid IP address in add-host: "api.test"'
ERROR: Encountered errors while bringing up the project.

I can manually add the entry to /etc/hosts and everything works as expected, so I’m unsure why Docker is not permitting what I’m trying to achieve.

Source: StackOverflow