# /etc/hosts entry linked to another container IP

**URL:** <https://forums.docker.com/t/etc-hosts-entry-linked-to-another-container-ip/54698>\
**Category:** General\
**Tags:** docker\
**Created:** [July 24, 2018, 9:44am UTC](https://forums.docker.com/t/etc-hosts-entry-linked-to-another-container-ip/54698 "2018-07-24T09:44:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelemen](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/kelemen/32/20469_2.png) [@kelemen](https://forums.docker.com/u/kelemen)\
**Post date:** [July 24, 2018, 9:44am UTC](https://forums.docker.com/t/etc-hosts-entry-linked-to-another-container-ip/54698/1 "2018-07-24T09:44:45Z")

</div>

Hi,  
i have 2 container, 1 for nginx and 1 for php-fpm. On host machine i have web application files. I have created some vhosts on nginx container (eg. a.localhost.sk, b.localhost.sk, …).

When i try to call curl (from my web application) to any vhost i got error (unresolved host). I need add new lines to /etc/hosts on my php container linked to nginx container IP.

```
NGINX_CONTAINER_IP a.localhost.sk
NGINX_CONTAINER_IP b.localhost.sk

```

It is possible to configure something like this in docker-compose file?

---

<div class="post-metadata">

**Author:** ![slonikkh](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/slonikkh/32/32598_2.png) [@slonikkh](https://forums.docker.com/u/slonikkh)\
**Post date:** [September 22, 2023, 8:51pm UTC](https://forums.docker.com/t/etc-hosts-entry-linked-to-another-container-ip/54698/2 "2023-09-22T20:51:28Z")

</div>

Good evening.

What do you see in the hosts file?

There’s something that looks like?

```auto
192.168.192.1 host.docker.internal

```

If not, you need to put that entry in there. But the problem is that every time you start the container it gets a random ip.

To overcome this, you need to use a script that will determine which ip is assigned to the container and write it to the hosts file.

entrypoint.sh

```auto
#!/bin/sh
set -e

HOST_DOMAIN="host.docker.internal"
if ! ping -q -c1 $HOST_DOMAIN > /dev/null 2>&1
then
 HOST_IP=$(ip route | awk 'NR==1 {print $3}')
 # shellcheck disable=SC2039
  echo -e "$HOST_IP\t$HOST_DOMAIN" >> /etc/hosts
fi

# first arg is `-f` or `--some-option`
if ["${1#-}" != "$1"]; then
set -- php-fpm "$@"
fi
exec "$@"

```

Then use the Dockerfile to copy it into the container

```auto
FROM ****

 ************

 COPY ./path_to_file/entrypoint.sh /usr/local/bin/docker-php-entrypoint
 RUN chmod +x /usr/local/bin/docker-php-entrypoint

```

You will then be able to access it from the container

```auto
 docker compose run --rm container_name curl -I host.docker.internal:8000

```

```auto
> HTTP/1.1 200 OK
> Server: nginx
> Date: Fri, 08 Sep 2023 18:38:56 GMT
> Content-Type: text/html
> Content-Length: 1310
> Connection: keep-alive
> Accept-Ranges: bytes
> Last-Modified: Fri, 08 Sep 2023 18:38:56 GMT
> X-Content-Type-Options: nosniff
> X-Frame-Options: DENY
> X-Xss-Protection: 1; mode=block

```
