Alexander Zeitler

Alexander Zeitler

Accessing local domains in local dev environment from Android emulator

Published on Friday, September 18, 2020

Let's consider you're using local domains in development and want access them from Android virtual devices using the Android emulator? Here's how to solve this on macOS using "serverless" pihole.

Given our local IP is 192.168.0.87 and we have defined a domain ui in /etc/hosts.

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
192.168.0.87    ui
127.0.0.1       localhost
::1             localhost

If you want to access http://ui from an virtual Android device running in the Android emulator, you get this error in Chrome:

To make it work, we need a DNS server which knows how to resolve http://ui to 192.168.0.87.

An easy way is to run the popular pihole in a Docker container (hence "serverless" in my post title 😉).

Just grab the sample docker-compose.yml from the Docker registry:

version: "3"

# More info at https://github.com/pi-hole/docker-pi-hole/ and https://docs.pi-hole.net/
services:
  pihole:
    container_name: pihole
    image: pihole/pihole:latest
    ports:
      - "53:53/tcp"
      - "53:53/udp"
      - "67:67/udp"
      - "80:80/tcp"
      - "443:443/tcp"
    environment:
      TZ: 'America/Chicago'
      # WEBPASSWORD: 'set a secure password here or it will be random'
    # Volumes store your data between container upgrades
    volumes:
      - './etc-pihole/:/etc/pihole/'
      - './etc-dnsmasq.d/:/etc/dnsmasq.d/'
    # Recommended but not required (DHCP needs NET_ADMIN)
    #   https://github.com/pi-hole/docker-pi-hole#note-on-capabilities
    cap_add:
      - NET_ADMIN
    restart: unless-stopped

Given http://ui runs on port 80, we need to change the port mapping for port 80 in the docker-compose.yml to another port, e.g. 8080:

ports:
  - "53:53/tcp"
  - "53:53/udp"
  - "67:67/udp"
  - "8080:80/tcp"
  - "443:443/tcp"

Next, we run docker-compose up -d.

Then we can browse to http://localhost:8080/admin.

The Browser should show something like this:

After this, we can click "Login" from the sidebar and login using the password (how to obtain the password is described on the Docker registry page linked above).

Next we can select "Local DNS records" from the sidebar:

Then we add a new DNS entry for ui with IP address 192.168.0.87.

Now it's time to start our emulator again, this time from the command line, as this is the easiest way to pass the DNS server setting:

cd ~/Library/Android/sdk/emulator
emulator -list-avds

Your output will look like this:

Pixel_XL_API_29

Now we can start the emulator again and pass the pihole DNS server running on 192.168.0.87:

./emulator -avd Pixel_XL_API_29 -dns-server 192.168.0.87

Pointing Chrome again to http://ui inside the virtual device should result in something like this now:

To me, local DNS has never been that easy. How about you?

What are your thoughts about "Accessing local domains in local dev environment from Android emulator"?
Drop me a line - I'm looking forward to your feedback! email
Imprint | Privacy