selenoid icon indicating copy to clipboard operation
selenoid copied to clipboard

Use hosts file from aerokube/selenoid for browser containers

Open egormkn opened this issue 3 years ago • 9 comments

I am trying to set up selenoid to test an application started on localhost. I've tried using --net=host with -container-network host, but this did't work for me (4444 port was not bound for some reason). Docker also has --add-host="host.docker.internal:host-gateway" argument that adds the host IP to /etc/hosts in container, so it is possible to connect to host from within the aerokube/selenoid container started with this argument. However, it seems that browser containers do not know about this configuration and I didn't find a way to modify their launch arguments to pass it.

I suggest to add some way for browser containers to use the hosts defined in selenoid container.

egormkn avatar Dec 26 '21 22:12 egormkn

@egormkn this is implemented years ago:

  1. Selenoid has hosts section in browsers.json: https://aerokube.com/selenoid/latest/#_other_optional_fields
  2. Docker has built-in aliases for host machine. https://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container/45002996#45002996

vania-pooh avatar Dec 27 '21 07:12 vania-pooh

  1. Selenoid has hosts section in browsers.json: https://aerokube.com/selenoid/latest/#_other_optional_fields

It requires static IP, while I want to use the local IP of my laptop. It might change depending on the network I am connected to.

  1. Docker has built-in aliases for host machine. https://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container/45002996#45002996

That is exactly the thing that does not work: host.docker.internal is properly resolved to local IP for aerokube/selenoid container but not for browser containers (selenoid/firefox, selenoid/chrome etc.).

I am using Docker v20.10 on Ubuntu 20.04. Maybe for Windows and MacOS that alias is automatically resolved for all docker containers. On Linux it should be passed as command-line argument for docker run.

egormkn avatar Dec 27 '21 12:12 egormkn

@egormkn Docker is using a dedicated network adapter (docker0) and thus gateway IP address does no change.

vania-pooh avatar Dec 27 '21 12:12 vania-pooh

@vania-pooh hello I have the same problem. I try to run specs using selenoid. Selenoid runs locally in docker. The application runs locally too but not in docker (on 127.0.0.1:9887). What should I set in hosts to make access from browser containers to my app?

ifconfig docker0
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 100.64.0.1  netmask 255.255.0.0  broadcast 100.64.255.255
        inet6 fe80::42:ebff:fedd:8323  prefixlen 64  scopeid 0x20<link>
        ether 02:42:eb:dd:83:23  txqueuelen 0  (Ethernet)
        RX packets 1120728  bytes 715089890 (715.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1245247  bytes 418069136 (418.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ruslanshelar avatar Jan 05 '22 11:01 ruslanshelar

@ruslanshelar usually going to 100.64.0.1:9887 should help.

vania-pooh avatar Jan 05 '22 14:01 vania-pooh

@vania-pooh what do u mean "going"? I already have tried to open 100.64.0.1:9887 in the browser container and it does not work.

ruslanshelar avatar Jan 05 '22 14:01 ruslanshelar

@ruslanshelar going means "establishing TCP connection".

vania-pooh avatar Jan 05 '22 15:01 vania-pooh

@vania-pooh I was hoping to get a solution, that would not be tied to specific IP addresses so that it will be reproducible on an arbitrary local machine. As you see, @ruslanshelar has IP 100.64.0.1 assigned to his bridge interface while for me it is 172.17.0.1.

Passing hosts file from selenoid container to browser containers seems to me like a good solution. I couldn't find any other ways to establish TCP connection to localhost without hardcoding the IP.

egormkn avatar Jan 06 '22 17:01 egormkn

@egormkn how exactly did you pass the host file from the seleonid container to the browser containers? It seems for using the hosts section in browsers.json I would need to manually parse the /etc/hosts file of the selenoid container somehow.

@vania-pooh would it be possible to run the browser containers with the --add-host="host.docker.internal:host-gateway" command line option added?

More info about this option can be found in https://github.com/moby/moby/pull/40007

Perni1984 avatar Mar 21 '22 15:03 Perni1984

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Jan 19 '24 01:01 github-actions[bot]